Home » 2016 » March » 17 » Some amazing command shortcuts in Linux

11:43 PM
Some amazing command shortcuts in Linux

Hello Linuxmen

Today in tis article we shall learn cool command execution tricks in Linux/Unix.

Many times we have to run the same command or last command or any one command in recent history in our lovely Terminal/Putty. We press up button several times and search.

Lets try and implement some easier methods.

1. Execute the last command.

To execute last command we can simply press up button and press enter. Or We can use "!!" and press enter. This will do the same.

Prompt:~> date
Thu Mar 17 19:38:55 CET 2016

Prompt:~> !!
date
Thu Mar 17 19:38:58 CET 2016

 

2.  Execute a matching command from history

You can execute a last matching command using start with characters. For example last commad starting with 'ho' :

Prompt:~> hostname
shankysportal

Prompt:~> date
Thu Mar 17 19:43:19 CET 2016

Prompt:~> !ho
hostname
shankysportal

 

3. Execute 2nd last command or nth last command. 

See the comamnd below to understand it. You can execute any nth last command using "!-n" . For example, !-2 for 2nd last !-3 for 3rd last command.

Prompt:~> history|tail
 1006  2016-03-17 19:43:23 hostname
 1007  2016-03-17 19:47:42 host kwomn1
 1008  2016-03-17 19:48:01 hostname
 1009  2016-03-17 19:51:53 cal
 1010  2016-03-17 19:52:03 uname -a
 1011  2016-03-17 19:52:28 date
 1012  2016-03-17 19:52:31 uname -a
 1013  2016-03-17 19:52:40 hostname
 1014  2016-03-17 19:53:10 history|head
 1015  2016-03-17 19:53:23 history|tail

Prompt:~> !-3
hostname
shankysportal

Prompt:~> !-5
uname -a
Linux shankysportal3.0.101-0.40-xen #1 SMP Thu Sep 18 13:09:38 UTC 2014 (44b8c95) x86_64 x86_64 x86_64 GNU/Linux

Prompt:~> !-7
date
Thu Mar 17 19:53:47 CET 2016

 

 4. See the most recent comamnd using history

You can use history command to see your last commands. It shows 1000 last commands. You can see last ten command using history|head.

Prompt:~> history|wc -l
1000

Prompt:~> history|wc -l
1000

Prompt:~> history|tail
 1011  2016-03-17 19:52:28 date
 1012  2016-03-17 19:52:31 uname -a
 1013  2016-03-17 19:52:40 hostname
 1014  2016-03-17 19:53:10 history|head
 1015  2016-03-17 19:53:23 history|tail
 1016  2016-03-17 19:53:32 hostname
 1017  2016-03-17 19:53:43 uname -a
 1018  2016-03-17 19:53:47 date
 1019  2016-03-17 20:01:26 history|wc -l
 1020  2016-03-17 20:02:46 history|tail

 

5. Access the argument of last comaand

You can access the argument of last command using !$. See the below example to understand it.

Prompt:~> echo "Hi"
Hi

Prompt:~> echo !$
echo "Hi"
Hi

 

6. Search and excute a command using CTRL +r

Here comes the best one. This is my favourite and most commonly used. Just press ctrl+r at the terminal/Putty anf enter starting character of your command, The prompt shows you complete command, press enter to execute it.

Press ctrl+r ,you get below:

(reverse-i-search)`pi': ping shankys
 

If you like, it share it!!

 
 

Category: Open System-Linux | Views: 1484 | Added by: shanky | Tags: history of commands in Linux, Command tricks in linux, Last Command, command shortcut | Rating: 0.0/0

Related blogs


You may also like to see:


[2016-01-23][Open System-Linux]
Some useful examples of netstat command in Linux for network statistics
[2015-03-23][Open System-Linux]
FSCK : Checking and reparing filesystems in Linux
[2014-12-24][Open System-Linux]
XARGS command in Linux with examples
[2016-12-25][Open System-Linux]
Guide for working with zipped and archived files in Linux/AIX
[2014-11-25][Open System-Linux]
DMESG command in Linux : to display or control kernel ring buffer

Total comments: 1
avatar
0 Spam
1 panchajanyakumarkali • 0:53 AM, 2016-03-18
Page up and down are also useful, e.g grep and press page up
ComForm">
avatar