How to make efficient use of Linux command line history using !! and ! ?

Double exclamation ie ‘!!’ represents the last run command on the shell. Here is an example :
$ uname -a 
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux

$ !!
uname -a
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
So what best can we do with !! ?
Well, firstly, you can extend the command easily. Here is an example :



$ !! | grep Linux
uname -a | grep Linux
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
 
Also, it so happens many times that you run a command and you get an error that the command requires root privileges. Then you press the ‘up arrow’ key + home key + write ‘sudo’ . Well all this can be avoided using !!.
Here is an example :

$ touch new_binary
touch: cannot touch `new_binary': Permission denied

$ sudo !! 
sudo touch new_binary
[sudo] password for himanshu:

$ ls new_binary
new_binary
Sometimes you would like to append a command to existing shell script or would like to create a new shell script, then you can use ‘!!’ to the task easily. Here is an example :

$ ls -lart /home/himanshu/practice/*.py

-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py

$ echo !! > script.sh echo ls -lart /home/himanshu/practice/*.py > script.sh

$ cat script.sh ls -lart /home/himanshu/practice/firstPYProgram.py
So we see that this way !! proves to be easy and time saving.

Now, lets come to single exclamation ie ‘!’ . Unlike double exclamation ie ‘!!’, through single exclamation ‘!’, we can access any previously run command that exists in command line history.

Here are some examples :

Use serial number from output of history command to run a particular command

$ history
...
...
...
2039 uname -a | grep Linux 
 2040 dmesg 
 2041 clear 
 2042 cd bin 
 2043 clear 
 2044 pwd 
 2045 touch new_binary 
 2046 sudo touch new_binary
 2047 ls new_binary 
 2048 history

$ !2039
uname -a | grep Linux Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux

So we see that command number 2039 was run through single exclamation ‘!’ without having to type or copy paste the command again.



You can use negative integer values with ‘!’ to run second last command, third last command, fourth last command…and so on.


Here is an example :
  $history
...
...
...
  2049 ! 2039 
  2050 uname -a | grep Linux 
  2051 history

$ !-2
uname -a | grep Linux 
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
Run a new command with argument of previous command

Here is an example :

$ ls /home/himanshu/practice/*.py 
/home/himanshu/practice/firstPYProgram.py

$ ls -lart !$ 
ls -lart /home/himanshu/practice/*.py 
 -rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py

So we see that ‘!$’ can be used to fetch argument from previous command and use it with the current command.

In case of two arguments, use carrot ‘!^’ to access first argument

Here is an example :

$ ls /home/himanshu/practice/*.py /home/himanshu/practice/*.txt
/home/himanshu/practice/file.txt /home/himanshu/practice/output.txt /home/himanshu/practice/sort.txt
/home/himanshu/practice/firstPYProgram.py /home/himanshu/practice/sort1.txt /home/himanshu/practice/test.txt
/home/himanshu/practice/input.txt /home/himanshu/practice/sort2.txt

$ ls -lart !^ 
ls -lart /home/himanshu/practice/*.py 
-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py

So we see that through ‘!^’ we can access the first argument of the previous run command.

To access the any other argument (of previous run command) in current command, ‘![prev command name]:[argument number]‘ can be used.

Here is an example :

$ ls !ls:2 
ls /home/himanshu/practice/*.txt
/home/himanshu/practice/file.txt /home/himanshu/practice/sort1.txt /home/himanshu/practice/test.txt
/home/himanshu/practice/input.txt /home/himanshu/practice/sort2.txt
/home/himanshu/practice/output.txt /home/himanshu/practice/sort.txt

So this way, the second argument (of the previous command) was accessed.

To access all the arguments of a previously run command, use ‘!*’

Here is an example :

$ ls -lart !* 
ls -lart /home/himanshu/practice/*.py /home/himanshu/practice/*.txt
-r--r--r-- 1 himanshu himanshu 50 Oct 24 2012 /home/himanshu/practice/output.txt
-r--r--r-- 1 himanshu himanshu 7 Nov 10 13:46 /home/himanshu/practice/input.txt
-r--r--r-- 1 himanshu himanshu 8 Dec 7 20:38 /home/himanshu/practice/sort1.txt
-r--r--r-- 1 himanshu himanshu 8 Dec 7 20:39 /home/himanshu/practice/sort2.txt
-r--r--r-- 1 himanshu himanshu 14 Dec 14 20:45 /home/himanshu/practice/file.tx
-r--r--r-- 1 himanshu himanshu 41 Jan 23 20:42 /home/himanshu/practice/sort.txt
-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py -rw-rw-r-- 1 himanshu himanshu 0 Mar 10 15:31 /home/himanshu/practice/test.tx

Use ‘![keyword]‘ to run the last command starting with [keyword]

Here is an example :

$ !ls 
ls -lart /home/himanshu/practice/*.py 
-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py

So we see that the last ls command was executed. This way you can just write the first keyword of the command (which is command name usually) and you do not need to write the complete command. Single exclamation ‘!’ will do it for you.

Related product you might see:

Share this product :

Post a Comment

Blogger Tips and TricksLatest Tips And TricksBlogger Tricks
 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Blogger Hacker - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger