How To Use (!) Symbol In Linux


Uses of (!) Symbol Operator in Linux Commands

The (!) operator in Linux can be used as Logical Negation operator to fetch commands. Uses of (!) operator in Linux is explained in this article.

To get started, find the command number by running ‘ history‘ command.

user1@linuxhelp:~$ history
    1  sudo su
    2  ping google.co.in
    3  sudo su
    4  cd
    5  history
    6  top
    7  Banshee
    8  sudo Banshee
    9  cal
   10  dat
   11  date
   12  calculator
   13  history

Run a command from history using the number.

user1@linuxhelp:~$ !2
ping google.co.in
PING google.co.in (216.58.220.35) 56(84) bytes of data.
64 bytes from maa03s18-in-f3.1e100.net (216.58.220.35): icmp_seq=1 ttl=57 time=12.1 ms
64 bytes from maa03s18-in-f3.1e100.net (216.58.220.35): icmp_seq=3 ttl=57 time=19.6 ms
64 bytes from maa03s18-in-f3.1e100.net (216.58.220.35): icmp_seq=4 ttl=57 time=8.77 ms
64 bytes from maa03s18-in-f3.1e100.net (216.58.220.35): icmp_seq=5 ttl=57 time=26.8 ms
64 bytes from maa03s18-in-f3.1e100.net (216.58.220.35): icmp_seq=6 ttl=57 time=11.1 ms
64 bytes from maa03s18-in-f3.1e100.net (216.58.220.35): icmp_seq=7 ttl=57 time=17.1 ms
64 bytes from maa03s18-in-f3.1e100.net (216.58.220.35): icmp_seq=8 ttl=57 time=13.5 ms
^C
--- google.co.in ping statistics ---
9 packets transmitted, 7 received, 22% packet loss, time 12052ms
rtt min/avg/max/mdev = 8.772/15.604/26.872/5.714 ms

The running sequence of repeated commands as shown below,

user1@linuxhelpmaster:~$ history
    1  sudo su
    2  ping google.co.in
    3  sudo su
    4  cd
    5  history
    6  top
    7  Banshee
    8  sudo Banshee
    9  cal
   10  date
   11  date
   12  calculator
   13  history
   14  ping google.co.in
   15  cal
   16  top
   17  history
   18  Banshee
   19  top
   20  history
user1@linuxhelp:~$ !9
cal
     June 2016        
Su Mo Tu We Th Fr Sa  
          1  2  3  4  
 5  6  7  8  9 10 11  
12 13 14 15 16 17 18  
19 20 21 22 23 24 25  
26 27 28 29 30        
user1@linuxhelp:~$ !11
date
Fri Jun 17 01:14:49 IST 2016
user1@linuxhelp:~$ !16
top
top - 01:13:15 up 40 min,  1 user,  load average: 0.07, 0.03, 0.05
Tasks: 244 total,   1 running, 243 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.0 us,  1.2 sy,  0.1 ni, 95.3 id,  2.4 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2030656 total,   716088 free,   702640 used,   611928 buff/cache
KiB Swap:  3905532 total,  3905532 free,        0 used.  1130252 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND     
  2805 user1     20   0  661512  37128  27748 S  6.2  1.8   0:02.24 gnome-term+ 
  3002 user1     20   0   49008   3696   3008 R  6.2  0.2   0:00.01 top         
     1 root      20   0  119712   5912   4036 S  0.0  0.3   0:02.06 systemd     
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd    
     3 root      20   0       0      0      0 S  0.0  0.0   0:00.08 ksoftirqd/0 
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:+ 
     7 root      20   0       0      0      0 S  0.0  0.0   0:00.46 rcu_sched   
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh      
     9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0

Passing arguments of last command without retyping

Now list the content of directory /home/$USER/ by using the following command.

user1@linuxhelp:~$ ls -l /home/$USER/
total 44
drwxr-xr-x 2 user1 user1 4096 Jun 17 00:08 Desktop
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Documents
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Downloads
-rw-r--r-- 1 user1 user1 8980 Jun  1 03:14 examples.desktop
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Music
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Pictures
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Public
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Templates
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Videos

If you run the ls -l command with !$ symbol, it will detect the last executed command.

user1@linuxhelp:~$ ls -l !$
ls -l /home/$USER/
total 44
drwxr-xr-x 2 user1 user1 4096 Jun 17 00:08 Desktop
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Documents
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Downloads
-rw-r--r-- 1 user1 user1 8980 Jun  1 03:14 examples.desktop
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Music
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Pictures
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Public
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Templates
drwxr-xr-x 2 user1 user1 4096 Jun  1 03:22 Videos

Handling two or more arguments

Create a text file sample.txt on the Desktop.

user1@linuxhelp:~$ touch /home/user1/Desktop/sample.txt

Copy it to ‘ /home/user1/Downloads‘ using complete path on either side with cp command.

user1@linuxhelp:~$ cp /home/user1/Desktop/sample.txt /home/user1/Downloads

Execute echo [arguments] to print both arguments separately. Here, first argument is ‘ /home/user1/Desktop/sample.txt‘ and second argument is ‘ /home/user1/Downloads‘

user1@linuxhelp:~$ echo “ 1st Argument is : !^” 
echo “ 1st Argument is : /home/user1/Desktop/sample.txt” 
“ 1st Argument is : /home/user1/Desktop/sample.txt” 
user1@linuxhelp:~$ echo “ 2nd Argument is : !cp:2” 
echo “ 2nd Argument is : /home/user1/Downloads” 
“ 2nd Argument is : /home/user1/Downloads” 

Running last command on the basis of keywords

Run the last executed command on the basis of keywords.

user1@linuxhelp~$ ls /home >  /dev/null                        
user1@linuxhelp~$ ls -l /home/user1/Desktop >  /dev/null                           
user1@linuxhelp~$ ls -la /home/user1/Downloads >  /dev/null                            
user1@linuxhelp~$ ls -lA /usr/bin >  /dev/null                       

Same command (ls) is executed, but with different switches and for different folders.

Now, execute the ls command with ! symbol. It will give the output of the corresponding last executed command.

user1@linuxhelp:~$ ! ls
Desktop    Downloads         Music     Public     Videos
Documents  examples.desktop  Pictures  Templates
user1@linuxhelp:~$ ! ls -l
total 9
drwxr-xr-x 2 user1 user1 4096 Jun 17 01:21 Desktop
drwxr-xr-x 2 user1 user1 4096 Jun 1 03:22 Documents
drwxr-xr-x 2 user1 user1 4096 Jun 17 01:23 Downloads
-rw-r--r-- 1 user1 user1 8980 Jun 1 03:14 examples.desktop
drwxr-xr-x 2 user1 user1 4096 Jun 1 03:22 Music
drwxr-xr-x 2 user1 user1 4096 Jun 1 03:22 Pictures
drwxr-xr-x 2 user1 user1 4096 Jun 1 03:22 Public
drwxr-xr-x 2 user1 user1 4096 Jun 1 03:22 Templates
drwxr-xr-x 2 user1 user1 4096 Jun 1 03:22 Videos
user1@linuxhelp:~$ ! ls -la
total 108
drwxr-xr-x 15 user1 user1 4096 Jun 17 00:34 .
drwxr-xr-x 3 root root 4096 Jun 1 03:14 ..
-rw------- 1 user1 user1 34 Jun 15 01:14 .bash_history
.
.
.
-rw------- 1 user1 user1 114 Jun 17 00:33 .Xauthority
-rw------- 1 user1 user1 82 Jun 17 00:33 .xsession-errors
-rw------- 1 user1 user1 82 Jun 17 00:04 .xsession-errors.old
user1@linuxhelp:~$ ! ls -lA
total 100
-rw------- 1 user1 user1 34 Jun 15 01:14 .bash_history
-rw-r--r-- 1 user1 user1 220 Jun 1 03:14 .bash_logout
-rw-r--r-- 1 user1 user1 3771 Jun 1 03:14 .bashrc
.
.
.
-rw------- 1 user1 user1 114 Jun 17 00:33 .Xauthority
-rw------- 1 user1 user1 82 Jun 17 00:33 .xsession-errors
-rw------- 1 user1 user1 82 Jun 17 00:04 .xsession-errors.old

Check the output with already executed commands using the same ls keywords.

The power of !! Operator

It will call the last run command with alter current command. The next time it will run one-liner script.

user1@linuxhelp:~$  ifconfig
ens38     Link encap:Ethernet  HWaddr 00:0c:29:cc:cd:61  
          inet addr:192.168.7.126  Bcast:192.168.7.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fecc:cd61/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1121 errors:0 dropped:0 overruns:0 frame:0
          TX packets:168 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:77962 (77.9 KB)  TX bytes:15199 (15.1 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:349 errors:0 dropped:0 overruns:0 frame:0
          TX packets:349 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:27082 (27.0 KB)  TX bytes:27082 (27.0 KB)

Execute previous command with the help of “ !!” symbols.

user1@linuxhelp:~$ su -c !! user1
su -c ifconfig user1
Password: 
ens38     Link encap:Ethernet  HWaddr 00:0c:29:cc:cd:61  
          inet addr:192.168.7.126  Bcast:192.168.7.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fecc:cd61/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1121 errors:0 dropped:0 overruns:0 frame:0
          TX packets:168 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:77962 (77.9 KB)  TX bytes:15199 (15.1 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:349 errors:0 dropped:0 overruns:0 frame:0
          TX packets:349 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:27082 (27.0 KB)  TX bytes:27082 (27.0 KB)

Check if a directory exist or not?

user1@linuxhelp:~$  [ ! -d /home/user1/linuxhelp ] & &  printf ' 
no such /home/user1/linuxhelp directory exist
'  || printf ' 
/home/user1/linuxhelp directory exist
' 

/home/user1/linuxhelp directory exist
Logic is, when the output of [ ! -d /home/user1/linuxhelp ] is 0, it will execute what lies beyond Logical AND else it will go to Logical OR (||) and execute what lies beyond Logical OR.

! -d    to validate if the directory exist or not
AND (& & )    to print that directory does not exist
OR (||)    to print that directory exist

Check if a directory exist or not? If not execute another command

The following condition checks the desired directory exist or not. Once it is confirmed that it doesn' t exist, then it will exit the command terminal.

user1@linuxhelp:~$ [ ! -d /home/user1/linuxhelp ] & &  exit
FAQ
Q
What is the use of (!) Symbol In Linux?
A
The (!) operator in Linux can be used as Logical Negation operator to fetch commands.
Q
How to run a command from History?
A
First simply type "history" and then from the list type "!history number" say "!2" for executing the second command from history.
Q
How to execute previous command with negation?
A
To execute previous command use "su -c !!Username".
Q
How to undo a certain process with history?
A
Use "yum history undo history number" for attaining it.
Q
How to see your new history file?
A
"yum history new" can be used.