Hi Guys,
Let me share a cool bash tip that I recently found.
You may already know that I can use "!!" to excute the last command that I executed and "!(history number)" to execute a particular command from history.
Eg:
jithinp ~ $ history
11 clear
12 ls
13 ls -alh
14 w
15 top
16 top -cd1
17 history
This is my history list and suppose you want to execute the last command that I typed. ( That is "history" ) You can do it by executing "!!"
jithinp ~ $ !!
history
11 clear
12 ls
13 ls -alh
14 w
15 top
16 top -cd1
17 history
18 history
Now I want to execute the 14th command that I executed.
jithinp ~ $ !14
w
17:36:50 up 1:21, 1 user, load average: 0.05, 0.07, 0.07
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
jithinp pts/0 :0 16:15 1:21m 0.00s 0.00s /usr/bin/kwrited
Suppose you don't have to execute the command stored in history but want to print it instead. That is possible using ":p".
Eg.
jithinp ~ $ history
11 clear
12 ls
13 ls -alh
14 w
15 top
16 top -cd1
17 history
18 history
19 w
20 w
21 history
I want to print the last command that I executed.
jithinp ~ $ !:p
history
I want to print the 19th command in the history.
jithinp ~ $ !19:p
w
Now I want to find the last "ls" commands in the history.
jithinp ~ $ !ls:p
ls -alh
Suppose you want to search the history and print the most recent command that contains the string "ls" and execute it instead or printing.
jithinp ~ $ !?ls
ls -alh
total 1M
drwx------ 56 jithinp jithinp 3.5K 2013-11-01 17:50 .
drwxr-xr-x 78 root nogroup 1.5K 2013-10-19 14:12 ..
drwxrwxr-x 3 jithinp jithinp 512 2012-02-15 08:09 .abrt
drwx------ 3 jithinp jithinp 512 2012-01-20 09:23 .adobe
-rw-r--r-- 1 jithinp jithinp 7 2013-11-01 16:33 .at.txt
Hope you learned something new.
Thank you for stopping by.