https://stackoverflow.com/questions/51736614/piping-echo-output-into-xargs
How to tail all the log files inside a folder and subfolders?
https://askubuntu.com/questions/45332/how-to-copy-one-file-to-multiple-filenames
http://man7.org/linux/man-pages/man1/find.1.html
-print True; print the full file name on the standard output,
followed by a newline. If you are piping the output of find
into another program and there is the faintest possibility
that the files which you are searching for might contain a
newline, then you should seriously consider using the -print0
option instead of -print. See the UNUSUAL FILENAMES section
for information about how unusual characters in filenames are
handled.
-print0
True; print the full file name on the standard output,
followed by a null character (instead of the newline character
that -print uses). This allows file names that contain
newlines or other types of white space to be correctly
interpreted by programs that process the find output. This
option corresponds to the -0 option of xargs.
https://en.wikibooks.org/wiki/Grep
[Find multiple patterns across multiple lines](https://stackoverflow.com/questions/7422743/grep-for-multiple-patterns-over-multiple-files)
- find . | xargs grep 'pattern1' -sl | xargs grep 'pattern2' -sl
https://www.cyberciti.biz/faq/how-to-zip-a-folder-in-ubuntu-linux/
zip -r data.zip data/
http://www.washington.edu/computing/unix/history.html
https://stackoverflow.com/questions/3371294/how-can-i-recall-the-argument-of-the-previous-bash-command
Extract a
https://shapeshed.com/unix-fc/
The
https://unix.stackexchange.com/questions/291285/view-a-range-of-bash-history
View a range of bash history
It even accepts negative numbers to count from the end, like
https://alvinalexander.com/blog/post/linux-unix/how-grep-search-compressed-gzip-gz-text-file
zgrep 'GET /blog' access_log.gz | more
https://stackoverflow.com/questions/14373788/linux-cmd-to-search-for-a-class-file-among-jars-irrespective-of-jar-path
How to determine the “age” of a linux system since installation?
https://stackoverflow.com/questions/284662/how-do-you-normalize-a-file-path-in-bash
https://www.howtogeek.com/50093/unzip-bunzip2-and-untar-those-tar-gz-or-tar-bz2-files-in-one-step/
http://osxdaily.com/2007/03/05/manipulating-the-clipboard-from-the-command-line/
pbcopy: takes standard input and places it in the clipboard buffer
http://www.pellegrino.link/2015/03/07/brace-expansion-with-unix-shells.html
https://beyondgrep.com/why-ack/
alias grep="grep --color"
https://stackoverflow.com/questions/11392526/how-to-sort-the-output-of-grep-l-chronologically-by-newest-modification-date
https://unix.stackexchange.com/questions/384700/what-is-the-difference-between-su-username-and-sudo-su-username
https://superuser.com/questions/1063152/curl-response-hangs
https://unix.stackexchange.com/questions/148922/set-timeout-for-web-page-response-with-curl
https://stackoverflow.com/questions/5731234/how-to-get-the-start-time-of-a-long-running-linux-process
lsof -p 351
https://stackoverflow.com/questions/83329/how-can-i-extract-a-predetermined-range-of-lines-from-a-text-file-on-unix
https://stackoverflow.com/questions/268680/how-can-i-monitor-the-thread-count-of-a-process-on-linux
https://unix.stackexchange.com/questions/892/is-there-a-way-to-see-details-of-all-the-threads-that-a-process-has-in-linux
https://www.tecmint.com/run-repeat-linux-command-every-x-seconds/
https://stackoverflow.com/questions/24503494/run-curl-command-every-5-seconds
https://unix.stackexchange.com/questions/48215/watch-ing-curl-yields-unexpected-output
https://unix.stackexchange.com/questions/62247/how-do-i-know-what-service-is-running-on-a-particular-port-in-linux
https://www.linuxquestions.org/questions/linux-security-4/su-incorrect-password-451653/
https://stackoverflow.com/questions/1342894/find-a-class-somewhere-inside-dozens-of-jar-files/6543245
https://superuser.com/questions/40281/how-do-i-get-an-entire-directory-in-sftp
https://unix.stackexchange.com/questions/94357/find-out-current-working-directory-of-a-running-process
ldapsearch -x -h $host -b cn=users,dc=$company,dc=com "uid=$username"
https://bitbucket.org/birkenfeld/pygments-main/pull-requests/165/added-s-option-to-support-use-with-tail-f/diff
http://pygments.org/
https://www.cyberciti.biz/faq/turn-on-or-off-color-syntax-highlighting-in-vi-or-vim/
:syntax on
https://www.tecmint.com/view-multiple-files-in-linux/
https://stackoverflow.com/questions/10382141/temporarily-change-current-working-directory-in-bash-to-run-a-command
https://unix.stackexchange.com/questions/94357/find-out-current-working-directory-of-a-running-process
https://unix.stackexchange.com/questions/163145/how-to-get-whole-command-line-from-a-process/163146
http://tldp.org/LDP/abs/html/randomvar.html
$RANDOM
https://superuser.com/questions/246837/how-do-i-add-text-to-the-beginning-of-a-file-in-bash
https://www.lifewire.com/linux-command-wtmp-4092304
utmp, wtmp - login records
https://www.mkyong.com/linux/how-to-check-reboots-history-in-linux/
last reboot
last shutdown
https://www.cyberciti.biz/tips/linux-last-reboot-time-and-date-find-out.html
To display last shutdown date and time use the following command:
https://stackoverflow.com/questions/6712437/find-duplicate-lines-in-a-file-and-count-how-many-time-each-line-was-duplicated
http://www.brianstorti.com/stop-using-tail/
I still see a lot of people using tail -f to monitor files that are changing, mostly log files. If you are one of them, let me show you a better alternative: less +F
Simply put, it allows you to switch between navigation and watching mode. We all have been there: You are watching a file with tail -f, and then you need to search for something in this file, or just navigate up and down. Now you need to exit tail (or open a new shell), and ack this file or open it with vim to find what you are looking for. After that, you run tail again to continue watching the file. There’s no need to do that when you are using less.
You can just hit
When you need to watch multiple files at the same time,
https://stackoverflow.com/questions/24182950/how-to-get-hostname-from-iplinux
https://stackoverflow.com/questions/17534840/sed-throws-bad-flag-in-substitute-command
https://superuser.com/questions/509601/grep-multiple-exclude-extension
https://unix.stackexchange.com/questions/42901/how-to-do-nothing-forever-in-an-elegant-way
https://stackoverflow.com/questions/2935183/bash-infinite-sleep-infinite-blocking/22100106
ps -ef | grep PROCESS | grep -v grep | awk '{print $2}' | xargs kill -9
https://medium.com/@hithacker/tail-f-with-line-numbers-7c39dd0f92f9
I was sending sms to thousands of users and was logging the result to a log file so I wanted see the live tail of the log but I was also interested in knowing the number of users I have sent the sms. I found this nice solution on StackOverFlow
https://www.lifewire.com/install-linux-command-4091911
https://unix.stackexchange.com/questions/94831/cp-no-target-directory-explained
https://askubuntu.com/questions/230476/when-using-sudo-with-redirection-i-get-permission-denied
https://askubuntu.com/questions/830766/getting-permission-denied-even-when-using-sudo-command
mount
http://www.thegeekstuff.com/2013/01/mount-umount-examples
http://www.tutorialspoint.com/unix_commands/mountpoint.htm
https://superuser.com/questions/193918/what-does-mounting-a-root-file-system-mean-exactly
Under
-I
, man xargs
saysunquoted blanks do not terminate input items; instead the separator is the newline character
You can specify a different delimiter (at least in GNU xargs):
printf 'Hello Hola Bonjour' | xargs -d' ' -I _ echo _ Landon
More portably, use
\0
as the delimiter and -0
to use it:printf '%s\0' Hello Hola Bonjour | xargs -0 -I _ echo _ Landon
https://stackoverflow.com/questions/18321336/how-to-tail-all-the-log-files-inside-a-folder-and-subfoldersHow to tail all the log files inside a folder and subfolders?
To add the subfolders to the tailf command, use
tailf **/*.log
Instead of
tailf
you can also use tail -f
. Of course, the regular expression can be improved to match only specific file names.
Combine
cat
(retrieves the contents of a file) with tee
(writes the content away to the files specified in the arguments):cat 1.ods | tee {jan,feb,mar}-{Rep,graph}.ods >/dev/null
Alternative using shell redirection:
tee {jan,feb,mar}-{Rep,graph}.ods >/dev/null < 1.ods
In both cases,
> /dev/null
is a redirection that discards the duplicated contents (tee
writes its input to each parameter and standard output).for file in {jan,feb,mar}-{Rep.xls,graph.xls} do cp 1.ods $file donehttps://askubuntu.com/questions/639990/what-is-the-group-id-of-this-group-name
$ cut -d: -f3 < <(getent group sudo)
27
getent group sudo
will get the line regarding sudo
group from /etc/group
file :$ getent group sudo
sudo:x:27:foobar
-print True; print the full file name on the standard output,
followed by a newline. If you are piping the output of find
into another program and there is the faintest possibility
that the files which you are searching for might contain a
newline, then you should seriously consider using the -print0
option instead of -print. See the UNUSUAL FILENAMES section
for information about how unusual characters in filenames are
handled.
-print0
True; print the full file name on the standard output,
followed by a null character (instead of the newline character
that -print uses). This allows file names that contain
newlines or other types of white space to be correctly
interpreted by programs that process the find output. This
option corresponds to the -0 option of xargs.
- -e pattern
- -i: Ignore uppercase vs. lowercase.
- -v: Invert match.
- -c: Output count of matching lines only.
- -l: Output matching files only.
- -n: Precede each matching line with a line number.
- -b: A historical curiosity: precede each matching line with a block number.
[Find multiple patterns across multiple lines](https://stackoverflow.com/questions/7422743/grep-for-multiple-patterns-over-multiple-files)
- find . | xargs grep 'pattern1' -sl | xargs grep 'pattern2' -sl
https://www.cyberciti.biz/faq/how-to-zip-a-folder-in-ubuntu-linux/
zip -r data.zip data/
zip -r filename.zip /path/to/folder1 /path/to/file2
less file.zip
https://henrikwarne.com/2018/08/11/my-favorite-command-line-shortcuts/
If I want to modify the command before running it, I type !456:p instead. Then I use arrow-up and then modify it before running it.
- ctrl-a Move to the beginning of the line
- ctrl-e Move to the end of the line
- ctrl-u Clear the line (before the cursor position)
- ctrl-w Delete the word before the cursor position
https://stackoverflow.com/questions/3371294/how-can-i-recall-the-argument-of-the-previous-bash-command
!!:n
where n
is the 0-based position of the argument you want.
For example:
echo 'one' 'two'
# "one two"
echo !!:2
# "two"
The
!
prefix is used to access previous commands.
Other useful commands:
!$
- last argument from previous command!^
- first argument (after the program/built-in/script) from previous command!!
- previous command (often pronounced "bang bang")!n
- command numbern
fromhistory
!pattern
- most recent command matchingpattern
!!:s/find/replace
- last command, substitutefind
withreplace
There are 3 methods that I'm aware of:
pwdx
$ pwdx <PID>
lsof
$ lsof -p <PID> | grep cwd
/proc
$ readlink -e /proc/<PID>/cwd
https://askubuntu.com/questions/499807/how-to-unzip-tgz-file-using-the-terminal
To extract a
.tgz
file with tar you need to use,tar -xvzf /path/to/yourfile.tgz
where,
x
for extractv
for verbosez
for gnuzipf
for file, should come at last just before file name.
You can use the following command in a terminal to unzip the file in your case,
tar -xvzf /media/towhid/Amra/Software/Developing\ Soft/mongodb-linux-x86_64-2.6.3.tgz
Extract a .tgz
file in different directory:
One can use
-C
option to extract archive contents to a different directory as following,tar -xvzf /path/to/yourfile.tgz -C /path/where/to/extract/
The
fc
command is a command line utility for listing, editing and re-executing commands previously entered into an interactive shell. The fc
command is a shell builtin meaning the command comes from the shell rather than the operating systemhttps://unix.stackexchange.com/questions/291285/view-a-range-of-bash-history
View a range of bash history
If you must use history command, pipe it through sed or awk:
history | sed -n '10,20p'
fc -l -16 -10
.https://alvinalexander.com/blog/post/linux-unix/how-grep-search-compressed-gzip-gz-text-file
zgrep 'GET /blog' access_log.gz | more
https://stackoverflow.com/questions/14373788/linux-cmd-to-search-for-a-class-file-among-jars-irrespective-of-jar-path
find foo/ -name "*.jar" | xargs grep Hello.class
https://serverfault.com/questions/221377/how-to-determine-the-age-of-a-linux-system-since-installationHow to determine the “age” of a linux system since installation?
rpm -qi basesystem | grep "Install Date"
if you're wanting to chomp part of a filename from the path, "dirname" and "basename" are your friends, and "realpath" is handy too.
dirname /foo/bar/baz
# /foo/bar
basename /foo/bar/baz
# baz
dirname $( dirname /foo/bar/baz ))
# /foo
realpath ../foo
# ../foo: No such file or directory
realpath /tmp/../tmp/../tmp
# /tmp
Realpath appears not to be standard issue.
The closest you can get with the stock standard is
readlink -f /path/here/..
Realpath appears to come from debian, and is not part of coreutils:http://packages.debian.org/unstable/utils/realpath Which was originally part of the DWWW package.
( also available on gentoo as app-admin/realpath )
readlink -m /path/there/../../
https://www.howtogeek.com/50093/unzip-bunzip2-and-untar-those-tar-gz-or-tar-bz2-files-in-one-step/
To gunzip and untar a file in a single step, use the following—note that the z switch is the important one that tells tar to unzip it.
tar xvfz somefilename.tar.gz
To use bunzip2 to extract your tar.bz2 file in a single step, use the j switch instead.
https://stackoverflow.com/questions/3775377/how-do-you-diff-a-directory-for-only-files-of-a-specific-typetar xvfj somefilename.tar.bz2
diff -x '*.foo' -x '*.bar' -x '*.baz' /destination/dir/1 /destination/dir/2
Taken from ( a version of) the man page:
-x PAT --exclude=PAT
Exclude files that match PAT.
-X FILE --exclude-from=FILE
Exclude files that match any pattern in FILE.
pbcopy: takes standard input and places it in the clipboard buffer
$ pbcopy < file.txt
$ ps aux | pbcopy
pbpaste: takes data from the clipboard buffer and writes it to the standard output
$ pbpaste > pastetest.txt
$ pbpaste | grep rcp
$ echo I{like,love,hate}chocolate
$ mkdir -p ~/{Images,Movies,Music}
Nested brace expansion
Brace lists can be composed. For instance, the example above can be extended to create a hierarchy of folders quickly:
$ mkdir -p ~/{Images/{Cars,Family,House,Vacations},Movies,Music}
Using brace sequences, the writing is shorter and more readable:
The general syntax for a sequence expression is
{START..END..INCREMENT}
where START and END is a required integer or single character but INCREMENT an optional integer value (default to 1). Such an expression generates a sequence of integers or characters by INCREMENT step, starting from START to ENDincluded. This way, listing odd numbers between 9 and 17 is as simple as writing:- A valid brace expansion must contain at least a comma or a sequence expression.
- Variable expansion works inside a brace list but not inside a sequence expression.
file contents to your clipboard, pretty straightforward but very useful! Most seen when copying your
SSH
keys.$ pbcopy < text.md
https://beyondgrep.com/why-ack/
alias grep="grep --color"
$ grep --color -E '^|pattern1|pattern2' file_name
grep --color -E "pattern|$" file
ls -rt *.txt | xargs grep -l <pattern>
We first use
ls
to list *.txt
files and sort them by modification time (newest last), then for each entry run them through grep
so we only print out files that contain the pattern.
First of all using
su username
you have to provide the password of "username" to be able to switch into its account while with sudo su - username
for a second with your own password you will become root, then without using any other password you are running the su - uesrname
to switch into the "username".
The other difference is using
-
with su
it will switches to the user while running a login shell, without the -
you will get a non-login shell.
The su command switches to the super user – or root user – when you execute it with no additional options. You’ll have to enter the root account’s password. This isn’t all the su command does, though – you can use it to switch to any user account. If you execute the su bob command, you’ll be prompted to enter Bob’s password and the shell will switch to Bob’s user account.
Sudo runs a single command with root privileges. When you execute sudo command, the system prompts you for your current user account’s password before running command as the root user. By default, Ubuntu remembers the password for fifteen minutes and won’t ask for a password again until the fifteen minutes are up.
you could check the files
/proc/[pid]/task/[thread ids]/status
You can get more detailed timing information by using
time curl -vvv site1.dev/
.
I note that the server reply contains
https://unix.stackexchange.com/questions/94604/does-curl-have-a-timeout/94612Connection: keep-alive
, which means that the server is configured to use HTTP keep-alive :--speed-limit
specifies the minimum average speed which you are willing to accept, and --speed-time
specifies how long the transfer speed can remain below that limit before the transfer times out and is aborted.https://unix.stackexchange.com/questions/148922/set-timeout-for-web-page-response-with-curl
You can use
-m
option:-m, --max-time <seconds>
Maximum time in seconds that you allow the whole operation to
take. This is useful for preventing your batch jobs from hang‐
ing for hours due to slow networks or links going down. See
also the --connect-timeout option.
If this option is used several times, the last one will be used.
This includes time to connect, if you want to specify it separately, use
--connect-timeout
option.
On unix,
curl
will first look in the home directory of the user that's running curl for a file called .curlrc
. Create that file with the lineconnect-timeout = 10
to reduce the timeout to 10 seconds. Or you can set a max time for the entire operation, with the option
max-time
:max-time = 10
https://superuser.com/questions/486648/full-command-text-with-unix-psps
detects the size of your terminal window and clips to that.
Solution: don't output directly to the terminal!
ps -f | less
The ps command (at least the procps version used by many Linux distributions) has a number of format fields that relate to the process start time, including
lstart
which always gives the full date and time the process started:# ps -p 1 -wo pid,lstart,cmd
https://www.cyberciti.biz/faq/howto-linux-get-list-of-open-files/lsof -p 351
First you need to find out PID of process. Simply use any one of the following command to obtain process id:
OR
For example, find out PID of firefox web-browser, enter:
Output:
# ps aux | grep {program-name}
OR
$ ps -C {program-name} -o pid=
For example, find out PID of firefox web-browser, enter:
$ ps -C firefox -o pid=
Output:
7857
To list opne files for firefox process, enter:
$ ls -l /proc/7857/fd
https://stackoverflow.com/questions/83329/how-can-i-extract-a-predetermined-range-of-lines-from-a-text-file-on-unix
sed -n '16224,16482p;16483q' filename > newfile
From the sed manual:
p - Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.n - If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands.q - Exitsed
without processing any more commands or input. Note that the current pattern space is printed if auto-print is not disabled with the -n option.
Addresses in a sed script can be in any of the following forms:number Specifying a line number will match only that line in the input.An address range can be specified by specifying two addresses separated by a comma (,). An address range matches lines starting from where the first address matches, and continues until the second address matches (inclusively).
https://stackoverflow.com/questions/268680/how-can-i-monitor-the-thread-count-of-a-process-on-linux
ps huH p <PID_OF_U_PROCESS> | wc -l
H - Lists all the individual threads in a process
cat /proc/<PROCESS_PID>/status | grep Threads
grep Threads /proc/<PROCESS_PID>/status
ps -eLf
on the shell shall give you a list of all the threads and processes currently running on the system.
To get the number of threads for a given pid:
$ ps -o nlwp <pid>
Where
nlwp
stands for Number of Light Weight Processes (threads). Thus ps
aliases nlwp
to thcount
, which means that$ ps -o thcount <pid>
does also work.
If you want to monitor the thread count, simply use
watch
:
$ watch ps -o thcount
To get the sum of all threads running in the system:
$ ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'
There's also another option which is true CLI
ps -e -T | grep <application name or pid>
-e
shows all processes-T
lists all threads
You may try to use:
/usr/bin/pstree $PID
-b
– creates a beep sound if the exit of the command is non-zero.-c
– Interprets ANSI color sequences.-d
– highlights the changes in the command output.
You can run in while loop.
while sleep 5; do cmd; done
Edit:
If you don't want to use
while..loop
. you can use watch command.watch -n 5 cmd
watch --color -d "curl -I sandbox.dev | ccze"
, because I want colorized output. It seems that if I remove the ccze
pipe things are a little better, altough the download stats are still there.
It seems curl does print the progress stats only when stdout is not a terminal. (e.g.
curl -I sandbox.dev|cat
would give you these results, too) You can disable these, however.
From the manpage
-s/--silent
Silent or quiet mode. Don't show progress meter or
error messages. Makes Curl mute.
You can use
fuser
or lsof
i.e:fuser 8453/tcp
lsof -i TCP:8453
If you want more information from
fuser
you can also use the -v
flag, i.e: fuser -v 8453/tcp
https://www.linuxquestions.org/questions/linux-security-4/su-incorrect-password-451653/
'su -' only is for login in as root and you should supply your root password when it asks for one. you can do 'su - someuser' when you are root and login as someuser.
|
https://stackoverflow.com/questions/1342894/find-a-class-somewhere-inside-dozens-of-jar-files/6543245
grep -l "classname" *.jar
gives you the name of the jar
find . -name "*.jar" -exec jar -t -f {} \; | grep "classname"
gives you the package of the class
Use the
-r
(recursive) flag:get -r *
scp -r mpirocch@my-server:/home/mpirocch/Documents Documents
pwdx
$ pwdx <PID>
lsof
$ lsof -p <PID> | grep cwd
/proc
$ readlink -e /proc/<PID>/cwd
mount | column -t
cat /etc/passwd | column -t -s:
ps aux | sort -rnk 4
ps aux | sort -rnk 3
有时我们会忘记在需要root权限的命令前使用
sudo
,就要重写,比较麻烦,可以使用 sudo !!
命令来省去麻烦cat urls.txt | xargs wget
ps -ax | grep tomcat | grep -v grep | awk '{print $1}' | xargs kill -9
如果要传递的命令中需要多个参数,如 cp
有2个参数,xargs
要把之前命令的输出作为其中一个参数传给 cp
ls *.txt | xargs -i cp {} /tmp
tail -f sql.log | pygmentize -s -l sql
-s
was chosen for 'streaming'.pygmentize
supports the -g
option to automatically guess the lexer to be used which is useful for files read from STDIN
without checking any extension type.
Using that, you only need to set the following 2 exports in your
.bashrc
without any additional scripts:export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
Use
view
instead of less
. It opens the file with vim
in readonly mode.
It's practically a coloured
less
: a pager where you can search with / (and more). The only drawback is that you can't exit with q but you need :q
https://www.cyberciti.biz/faq/turn-on-or-off-color-syntax-highlighting-in-vi-or-vim/
:syntax on
https://www.tecmint.com/view-multiple-files-in-linux/
yum install ccze (Red Hat/CentOS)
tail -f -n 50 /var/log/syslog | ccze
Monitor '/applications' highlighted;
tail -f /var/log/system.log | grep /Applications --color
You can run the
cd
and the executable in a subshell by enclosing the command line in a pair of parentheses:(cd SOME_PATH && exec_some_command)
https://stackoverflow.com/questions/10856129/setting-an-environment-variable-before-a-command-in-bash-not-working-for-secondFOO=bar bash -c 'somecommand someargs | somecommand2'
How about exporting the variable, but only inside the subshell?:
(export FOO=bar && somecommand someargs | somecommand2)
Keith has a point, to unconditionally execute the commands, do this:
(export FOO=bar; somecommand someargs | somecommand2)
There are 3 methods that I'm aware of:
pwdx
$ pwdx <PID>
lsof
$ lsof -p <PID> | grep cwd
/proc
$ readlink -e /proc/<PID>/cwd
$ pgrep nautilus
12136
You could use the
-o
switch to specify your output format:$ ps -eo args
From the man page:
Command with all its arguments as a string. Modifications to the arguments may be shown. [...]
You may also use the
-p
switch to select a specific PID:$ ps -p [PID] -o args
pidof
may also be used to switch from process name to PID, hence allowing the use of -p
with a name:$ ps -p $(pidof dhcpcd) -o args
Of course, you may also use
grep
for this (in which case, you must add the -e
switch):$ ps -eo args | grep dhcpcd | head -n -1
https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file
Add newline regardless:
echo >> filename
Here is a way to check if a newline exists at the end before adding one, by using Python:
f=filename; python -c "import sys; sys.exit(open(\"$f\").read().endswith('\n'))" && echo >> $f
$RANDOM
https://superuser.com/questions/246837/how-do-i-add-text-to-the-beginning-of-a-file-in-bash
echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt
or
sed -i '1s/^/task goes here\n/' todo.txt
or
sed -i '1itask goes here' todo.txt
utmp, wtmp - login records
https://www.mkyong.com/linux/how-to-check-reboots-history-in-linux/
last reboot
last shutdown
https://www.cyberciti.biz/tips/linux-last-reboot-time-and-date-find-out.html
To display last shutdown date and time use the following command:
$ last -x|grep shutdown | head -1
https://stackoverflow.com/questions/6712437/find-duplicate-lines-in-a-file-and-count-how-many-time-each-line-was-duplicated
In order to sort the output with the most frequent lines on top, you can do the following (to get all results):
sort FILE | uniq -c | sort -nr
I still see a lot of people using tail -f to monitor files that are changing, mostly log files. If you are one of them, let me show you a better alternative: less +F
Simply put, it allows you to switch between navigation and watching mode. We all have been there: You are watching a file with tail -f, and then you need to search for something in this file, or just navigate up and down. Now you need to exit tail (or open a new shell), and ack this file or open it with vim to find what you are looking for. After that, you run tail again to continue watching the file. There’s no need to do that when you are using less.
You can just hit
Ctrl-c
to go to “normal” less
mode (as if you had opened the file without the +F
flag), and then you have all the normal less
features you’d expect, including the search with /foo
. You can go to the next or previous occurrence with n
or N
, up and down with j
and k
, create marks with m
and do all sort of things that less(1)
says you can do.When you need to watch multiple files at the same time,
tail -f
can actually give you a better output. https://stackoverflow.com/questions/24182950/how-to-get-hostname-from-iplinux
To find a hostname in your local network by IP address you can use:
nmblookup -A <ip>
To find a hostname on the internet you could use the
host
program:host <ip>
https://stackoverflow.com/questions/12696125/sed-edit-file-in-placesed -i.bak 's/foo/bar/g' sample
We are replacing foo with bar in sample file. Backup of original file will be saved in sample.bak
For editing inline without backup, use the following command
sed -i'' 's/foo/bar/g' sample
In your command
s/\/fonts/../fonts/
is being taken as the parameter to the -i
option (the suffix to use for the backup file), and the filename argument is being treated as the editing commands.
You need to specify to disable the backup file creation:
sed -i '' ...
In your example:
sed -i '' 's/\/fonts/../fonts/' /Users/sergeybasharov/WebstormProjects/snap/compiled/Content/stylesheets/style.css
Computers are dumb, they don't figure things out by context, so they can't tell that something beginning with
s/
is obviously an editing command, not a suffix.
You should escape the asterisk, not the curly brace. Your command should look like this:
grep -r --exclude=\*.{html,htm,js} "li" *
man grep *scroll scroll scroll*
--exclude=GLOB
Skip files whose base name matches GLOB (using wildcard matching).
A file-name glob can use *, ?, and [...] as wildcards, and \ to
quote a wildcard or backslash character literally.
Look up "shell globbing" for more info
Example:
$ grep -r --exclude=\*.{png,jpg} a .
./moo.txt:a
./moo.htm:a
./hai:a
$ ls
hai hai.png moo.htm moo.txt
https://unix.stackexchange.com/questions/31224/i-would-like-to-grep-all-files-except-some-file-types
If you have GNU grep
you can use the --exclude=GLOB
option, like
grep -r --exclude='*.sql' pattern dir/
grep pattern -r --include=\*.{cpp,h} rootdir
tail -f /dev/null
strace tail -f /dev/null
strace tail -f /dev/null
it seems that tail
uses inotify
and that wakeups occur in silly cases like sudo touch /dev/null
. It's sad that there seems to be no better solution... I wonder which would be the right syscall to use to implement a better solutiosleep infinity
is the clearest solution I know of.
You can use
infinity
because sleep
accepts a floating point number*, which may be decimal, hexadecimal, infinity, or NaN, according to man strtod
.
* This isn't part of the POSIX standard, so isn't as portable as
tail -f /dev/null
. However, it is supported in GNU coreutils (Linux) and BSD (used on Mac) (apparently not supported on newer versions of Mac — see comments).
If you look at it with
http://www.commandlinefu.com/commands/view/1138/ps-ef-grep-process-grep-v-grep-awk-print-2-xargs-kill-9strace tail -f /dev/null
you will notice, that this solution is far from blocking! It's probably even worse than the sleep
solution in the question, as it uses (under Linux) precious resources like the inotify
system. Also other processes which write to /dev/null
make tail
loop. (On my Ubuntu64 16.10 this adds several 10 syscalls per second on an already busy system.)ps -ef | grep PROCESS | grep -v grep | awk '{print $2}' | xargs kill -9
https://medium.com/@hithacker/tail-f-with-line-numbers-7c39dd0f92f9
I was sending sms to thousands of users and was logging the result to a log file so I wanted see the live tail of the log but I was also interested in knowing the number of users I have sent the sms. I found this nice solution on StackOverFlow
less -N +F <filename>
https://www.lifewire.com/install-linux-command-4091911
The install command on Linux systems is used to copy files, and it does this by combining several commands into one to make them easy to use. The install command utilizes the cp, chown, chmod, and strip commands.
https://superuser.com/questions/118781/copy-files-in-linux-avoid-the-copy-if-files-do-exist-in-destination
-n, --no-clobber
do NOT overwrite an existing file (overrides a previous -i option)
rsync -aq /src /dest
Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.
https://stackoverflow.com/questions/1529946/linux-copy-and-create-destination-dir-if-it-does-not-exist--parents
Form the name of each destination file by appending to the target
directory a slash and the specified name of the source file. The
last argument given to `cp' must be the name of an existing
directory. For example, the command:
cp --parents a/b/c existing_dir
copies the file `a/b/c' to `existing_dir/a/b/c', creating any
missing intermediate directories
https://unix.stackexchange.com/questions/94831/cp-no-target-directory-explained
By default,
cp
tests if its last argument is an existing directory. If this happens, cp
creates a link inside that directory, with the base name of the source. That is, given the commandcp foo/bar wibble
if
wibble
is an existing directory then cp
copies the source to wibble/bar
. If wibble
does not exist then cp
links the source to wibble
.
If you want to be sure that the copy is always
wibble
, then you can specify the --no-target-directory
(alias -T
) option. That way, if cp
succeeds, you can be sure that the copy is called wibble
. If wibble
already existed as a directory, then cp
will fail.
In tabular form:
The target is … Without -T With -T
existing directory copy in the directory error
existing file (not dir) overwrite overwrite
does not exist create create
The only difference is that with
-T
, in case the target is an existing directory, the command returns an error. This is useful when you expect the directory not to exist: you get an error message instead of something unpredicted happening.
The same applies to
mv
and ln
. If the target is an existing directory, with -T
, they signal an error rather than silently doing something different.
With
cp
, there's a different case. If you do a recursive copy and the source is a directory, then cp -T
copies the content of the source into the destination, rather than copying the source itself. That is, given$ tree source destination
source
└── foo
destination
└── bar
then
$ cp -rv source destination
`source' -> `destination/source'
`source/foo' -> `destination/source/foo'
whereas
% cp -rvT source destination
`source/foo' -> `destination/foo'
You would use
--no-target-directory
if you don't want a source directory copied underneathan existing destination directory, you want the source directory copied onto the destination directory.
So I tried to do a temporary change to disable polling by using:
sudo echo N> /sys/module/drm_kms_helper/parameters/poll
Yet again the system responded with:
bash: /sys/module/drm_kms_helper/parameters/poll: Permission denied
Output redirection (via the >
operator) is done by the shell, not by echo. You have to login as root
sudo -i
Then you can use redirection
echo N> /sys/module/drm_kms_helper/parameters/poll
Otherwise you can run bash string with sudo
sudo bash -c "echo N> /sys/module/drm_kms_helper/parameters/poll"
You are running the
cat
command as root, but the output redirection takes place in your current shell which runs as your normal user account.
You have at least three options to achieve your goal of adding lines to your
apt
sources:- Running the whole command including output redirection in a separate Bash root shell:
sudo bash -c 'cat >> /etc/apt/sources.list'
- Using the
tee
command that copies output to a file (-a
option to append to instead of overwrite existing files) and running that as root:cat | sudo tee -a /etc/apt/sources.list
- Using a terminal editor application like
nano
as root to modify the file:sudo nano /etc/apt/sources.list
mount
http://www.thegeekstuff.com/2013/01/mount-umount-examples
http://www.tutorialspoint.com/unix_commands/mountpoint.htm
-q | Be quiet - don’t print anything. |
-d | Print major/minor device number of the filesystem on stdout. |
-x | Print major/minor device number of the blockdevice on stdout. |
https://superuser.com/questions/193918/what-does-mounting-a-root-file-system-mean-exactly
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing