Friday, December 5, 2014

Amazing bash tricks

1. Renaming/moving files with suffixes quickly:
cp /home/foo/realllylongname.cpp{,-old}
This expands to:
cp /home/foo/realllylongname.cpp /home/foo/realllylongname.cpp-old

2) Another favorite:

!!
Repeats your last command. Most useful in the form:

sudo !!

3) '^string^string2' which takes the last command, replaces string with string2 and executes it

$ ehco foo bar baz
bash: ehco: command not found
$ ^ehco^echo
foo bar baz

4)rename

Example:

$ ls
this_has_text_to_find_1.txt
this_has_text_to_find_2.txt
this_has_text_to_find_3.txt
this_has_text_to_find_4.txt

$ rename 's/text_to_find/been_renamed/' *.txt
$ ls
this_has_been_renamed_1.txt
this_has_been_renamed_2.txt
this_has_been_renamed_3.txt
this_has_been_renamed_4.txt
So useful


5)When running commands, sometimes I'll want to run a command with the previous ones arguments. To do that, you can use this shortcut:

$ mkdir /tmp/new
$ cd !!:*

6)How to list only subdirectories in the current one ?

ls -d */
It's a simple trick, but you wouldn't know how much time I needed to find that one !

7)ESC.

Inserts the last arguments from your last bash command. It comes in handy more than you think.

cp file /to/some/long/path
cd ESC.

8)Top 10 commands used:
$ history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -nr | head

9)I often have aliases for vi, ls, etc. but sometimes you want to escape the alias. Just add a back slash to the command in front:

9) To escape the alias
$ alias vi=vim
$ \vi # This doesn't open VIM


No comments:

Post a Comment