Find in history
Don’t search history by grepping ~/.bash_history, or repeatedly hitting the up arrow, instead use CTRL+r (or ‘/’ in vi-mode) for search-as-you type. You can immediately run the command by pressing Enter.
Changing file extensions
Rename replaces string X in a set of file names with string Y.
rename 's/.html$/.php/' *.html
This will change the extension of every .html file in your CWD to .php.Selected Keystrokes:
Ctrl-U – Cuts everything to the left
Ctrl-W – Cuts the word to the left
Ctrl-Y – Pastes what’s in the buffer
Ctrl-A – Go to beginning of line
Ctrl-E – Go to end of line
Use && to run a second command if and only if a first command succeeds:
cd tmp/a/b/c && tar xvf ~/archive.tar
Use || to run a second command if and only if a first command fails:
cd /tmp/a/b || mkdir -p /tmp/a/b
See your favorite commands
Use the following to see the commands you use most often based on your shell history:
history | awk '{print $2}' | sort | uniq -c | sort -rn | head
Sum up your HDD space
Longish oneliner (I actually wrote it in one line first) for giving you somewhat (mount list is never good enough) accurate sum of your file systems’ totals.
df | egrep -v “(Filesystem|\/dev$|shm$|dvd|cdrom)” | awk ‘{totalu += $2 ; totalf += $4} END {print “Total space in devices: ” (totalu/1024/1024) ” GB\nFree space total: ” (totalf/1024/1024) ” GB”}’
Argument list too long
ls | xargs rm
Sometime there are so many files in a directory than the rm command doesn’t work
[root@server logs]# rm *
bash: /bin/rm: Argument list too long
On this case the best option is to use ls in conjuntion with xargs
[root@server logs]# ls | xargs rm
http://en.wikipedia.org/wiki/Xargs
Get your IP address
lynx -dump http://whatismyip.com | awk '/^Your/ {print $5}'
Run commands on logout
If a file named $HOME/.logout (a file named .logout in your home directory) exists, and the following trap statement is in your .profile, .logout is executed when you logout.
Add this to .profile:
trap "$HOME/.logout" 0
Remove comments and blank lines
sed ‘/ *#/d; /^ *$/d’ file
Remove comments and blank lines from file
Remove empty directories
To remove empty directories (even if filenames or dirnames contain spaces or weird characters) from a tree you can do:
find . -type d -empty -print0 | xargs -0 rmdir
Duplicate directory tree
The following command creates in the /usr/project directory, a copy of the current working directory structure:
find . -type d -print|sed ‘s@^\.\{0,1\}@/usr/project@’ | sed ‘s/ /\\ /’ | xargs mkdir -p