Some awesome linux command line tricks on bash

1. Runing the last command as Root

 sudo !! 

(!! repeats the last command)

2. Run your previous command with replacing “foo” with “bar”



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

Or alternatively you can use the below command:


3. the command-line equivalent of the back button

cd -

(It’s worth mentioning that ‘cd’ takes you to your home directory)

4. Insert arguments from you last bash command

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 + '.'

5. bulk rename


$ ls

$ rename 's/text_to_find/been_renamed/' *.txt
$ ls

6. 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

7. List only subdirectories in the current one

 ls -d */

8. Reverse search

^R reverse search. Hit ^R, type a fragment of a previous command you want to match, and hit ^R until you find the one you want. Then you don’t have to remember recently used commands that are still in history.

9. Selected bash keyboard shortcuts

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

10. Running a second command with the same arguments as the previous command

use ‘!*’ to repeat all arguments or ‘!:2’ to use the second argument. ‘!$’ uses the final argument.

$ cd /home/user/foo

cd: /home/user/foo: No such file or directory

$ mkdir !*

mkdir /home/user/foo

11. Get an ordered list of subdirectory sizes

This piece of code lists the size of every file and subdirectory of the current directory, much like du -sch ./* except the output is sorted by size, with larger files and directories at the end of the list. Useful to find where all that space goes.

du -sk ./* | sort -n | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

12. Use ALT+. to insert last parameter

In bash (or anything using libreadline, such as mysql) press ALT+. to insert the last used parameter from the previous line.


$ vim some/file.c
$ svn commit ALT+.


1. top 25 commands
2. all-time greats
3. public snipts

One thought on “Some awesome linux command line tricks on bash

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s