{"id":12701,"date":"2019-03-28T13:58:18","date_gmt":"2019-03-28T13:58:18","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12701"},"modified":"2019-03-28T13:58:18","modified_gmt":"2019-03-28T13:58:18","slug":"10-amazing-and-mysterious-uses-of-symbol-or-operator-in-linux-commands","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/10-amazing-and-mysterious-uses-of-symbol-or-operator-in-linux-commands\/","title":{"rendered":"10 Amazing and Mysterious Uses of (!) Symbol or Operator in Linux Commands"},"content":{"rendered":"<p>The\u00a0<code>'!'<\/code>\u00a0symbol or operator in Linux can be used as\u00a0<strong>Logical Negation<\/strong>\u00a0operator as well as to fetch commands from history with tweaks or to run previously run command with modification. All the commands below have been checked explicitly in bash Shell. Though I have not checked but a major of these won\u2019t run in other shell. Here we go into the amazing and mysterious uses of\u00a0<code>'!'<\/code>\u00a0symbol or operator in Linux commands.<\/p>\n<h6>1. Run a command from history by command number.<\/h6>\n<p>You might not be aware of the fact that you can run a command from your\u00a0<strong>history<\/strong>\u00a0command (already\/earlier executed commands). To get started first find the command number by running \u2018<strong>history<\/strong>\u2018 command.<\/p>\n<pre>$ history\r\n<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/history-Command.gif\"><img decoding=\"async\" class=\"size-medium wp-image-13426\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/history-Command.gif\" alt=\"Find Last Executed Commands with History Command\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>Now run a command from\u00a0<strong>history<\/strong>\u00a0just by the number at which it appears, in the output of\u00a0<strong>history<\/strong>. Say run a command that appears at number\u00a0<strong>1551<\/strong>\u00a0in the output of \u2018<strong>history<\/strong>\u2018 command.<\/p>\n<pre>$ !1551\r\n<\/pre>\n<p><center><ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-2601749019656699\" data-ad-slot=\"5590002574\" data-ad-format=\"auto\" data-adsbygoogle-status=\"done\"><ins id=\"aswift_4_expand\"><ins id=\"aswift_4_anchor\"><iframe id=\"aswift_4\" name=\"aswift_4\" width=\"780\" height=\"90\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\" data-mce-fragment=\"1\"><\/iframe><\/ins><\/ins><\/ins><\/center><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/Run-Commands-By-number.gif\"><img decoding=\"async\" class=\"size-medium wp-image-13427\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/Run-Commands-By-number.gif\" alt=\"Run Last Executed Commands by Number ID\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>And, it runs the command (<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">top command<\/a>\u00a0in the above case), that was listed at number\u00a0<strong>1551<\/strong>. This way to retrieving already executed command is very helpful specially in case of those commands which are long. You just need to call it using\u00a0<strong>![Number at which it appears in the output of history command]<\/strong>.<\/p>\n<h6>2. Run previously executed command as 2nd last command, 7th last command,etc.<\/h6>\n<p>You may run those commands which you have run previously by their running sequence being the last run command will be represented as\u00a0<strong>-1<\/strong>, second last as\u00a0<strong>-2<\/strong>, seventh last as\u00a0<strong>-7<\/strong>,\u2026.<\/p>\n<p>First run\u00a0<strong>history<\/strong>\u00a0command to get a list of last executed command. It is necessary to run\u00a0<strong>history<\/strong>\u00a0command, so that you can be sure that there is no command like\u00a0<code>rm command &gt; file<\/code>\u00a0and others just to make sure you do not run any dangerous command accidentally. And then check Sixth last command, Eight last command and Tenth last command.<\/p>\n<pre>$ history\r\n$ !-6\r\n$ !-8\r\n$ !-10\r\n<\/pre>\n<div id=\"attachment_13428\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/3.gif\"><img decoding=\"async\" class=\"size-medium wp-image-13428\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/3.gif\" alt=\"Run Last Executed Commands By Numbers\" aria-describedby=\"caption-attachment-13428\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-13428\" class=\"wp-caption-text\">Run Last Executed Commands By Numbers<\/p>\n<\/div>\n<h6>3. Pass arguments of last command that we run to the new command without retyping<\/h6>\n<p>I need to list the content of directory \u2018<strong>\/home\/$USER\/Binary\/firefox<\/strong>\u2018 so I fired.<\/p>\n<pre>$ ls \/home\/$USER\/Binary\/firefox\r\n<\/pre>\n<p>Then I realized that I should have fired \u2018<strong>ls -l<\/strong>\u2018 to see which file is executable there? So should I type the whole command again! No I don\u2019t need. I just need to carry the last argument to this new command as:<\/p>\n<pre>$ ls -l !$\r\n<\/pre>\n<p>Here\u00a0<code>!$<\/code>\u00a0will carry arguments passed in last command to this new command.<\/p>\n<div id=\"attachment_13430\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/4.gif\"><img decoding=\"async\" class=\"size-medium wp-image-13430\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/4.gif\" alt=\"Pass Arguments of Last Executed Command to New \" aria-describedby=\"caption-attachment-13430\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-13430\" class=\"wp-caption-text\">Pass Arguments of Last Executed Command to New<\/p>\n<\/div>\n<h6>4. How to handle two or more arguments using (!)<\/h6>\n<p>Let\u2019s say I created a text file\u00a0<strong>1.txt<\/strong>\u00a0on the Desktop.<\/p>\n<pre>$ touch \/home\/avi\/Desktop\/1.txt\r\n<\/pre>\n<p>and then copy it to \u2018<strong>\/home\/avi\/Downloads<\/strong>\u2018 using complete path on either side with\u00a0<strong>cp<\/strong>\u00a0command.<\/p>\n<pre>$ cp \/home\/avi\/Desktop\/1.txt \/home\/avi\/downloads\r\n<\/pre>\n<p>Now we have passed two arguments with\u00a0<strong>cp<\/strong>\u00a0command. First is \u2018<strong>\/home\/avi\/Desktop\/1.txt<\/strong>\u2018 and second is \u2018<strong>\/home\/avi\/Downloads<\/strong>\u2018, lets handle them differently, just execute\u00a0<code>echo [arguments]<\/code>\u00a0to print both arguments differently.<\/p>\n<pre>$ echo \u201c1st Argument is : !^\u201d\r\n$ echo \u201c2nd Argument is : !cp:2\u201d\r\n<\/pre>\n<p>Note 1st argument can be printed as\u00a0<code>\u201c!^\u201d<\/code>\u00a0and rest of the arguments can be printed by executing\u00a0<code>\u201c![Name_of_Command]:[Number_of_argument]\u201d<\/code>.<\/p>\n<p>In the above example the first command was \u2018<strong>cp<\/strong>\u2018 and 2nd argument was needed to print. Hence\u00a0<code>\u201c!cp:2\u201d<\/code>, if any command say\u00a0<strong>xyz<\/strong>\u00a0is run with 5 arguments and you need to get 4th argument, you may use\u00a0<code>\u201c!xyz:4\u201d<\/code>, and use it as you like. All the arguments can be accessed by\u00a0<code>\u201c!*\u201d<\/code>.<\/p>\n<div id=\"attachment_13431\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/5.gif\"><img decoding=\"async\" class=\"size-medium wp-image-13431\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/5.gif\" alt=\"Handle Two or More Arguments\" aria-describedby=\"caption-attachment-13431\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-13431\" class=\"wp-caption-text\">Handle Two or More Arguments<\/p>\n<\/div>\n<h6>5. Execute last command on the basis of keywords<\/h6>\n<p>We can execute the last executed command on the basis of keywords. We can understand it as follows:<\/p>\n<pre>$ ls \/home &gt; \/dev\/null\t\t\t\t\t\t[Command 1]\r\n$ ls -l \/home\/avi\/Desktop &gt; \/dev\/null\t\t                [Command 2]\t\r\n$ ls -la \/home\/avi\/Downloads &gt; \/dev\/null\t                [Command 3]\r\n$ ls -lA \/usr\/bin &gt; \/dev\/null\t\t\t\t        [Command 4]\r\n<\/pre>\n<div class=\"google-auto-placed ap_container\">\n<p>Here we have used same command\u00a0<strong>(ls)<\/strong>\u00a0but with different switches and for different folders. Moreover we have sent to output of each command to \u2018<strong>\/dev\/null<\/strong>\u2018 as we are not going to deal with the output of the command also the console remains clean.<\/p>\n<p>Now Execute last run command on the basis of keywords.<\/p>\n<pre>$ ! ls\t\t\t\t\t[Command 1]\r\n$ ! ls -l\t\t\t\t[Command 2]\t\r\n$ ! ls -la\t\t\t\t[Command 3]\r\n$ ! ls -lA\t\t\t\t[Command 4]\r\n<\/pre>\n<p>Check the output and you will be astonished that you are running already executed commands just by\u00a0<code>ls<\/code>keywords.<\/p>\n<div id=\"attachment_13434\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/6.gif\"><img decoding=\"async\" class=\"size-medium wp-image-13434\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/6.gif\" alt=\"Run Commands Based on Keywords\" aria-describedby=\"caption-attachment-13434\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-13434\" class=\"wp-caption-text\">Run Commands Based on Keywords<\/p>\n<\/div>\n<h6>6. The power of !! Operator<\/h6>\n<p>You can run\/alter your last run command using\u00a0<code>(!!)<\/code>. It will call the last run command with alter\/tweak in the current command. Lets show you the scenario<\/p>\n<p>Last day I run a one-liner script to get my private IP so I run,<\/p>\n<pre>$ ip addr show | grep inet | grep -v 'inet6'| grep -v '127.0.0.1' | awk '{print $2}' | cut -f1 -d\/\r\n<\/pre>\n<p>Then suddenly I figured out that I need to redirect the output of the above script to a file\u00a0<strong>ip.txt<\/strong>, so what should I do? Should I retype the whole command again and redirect the output to a file? Well an easy solution is to use\u00a0<code>UP<\/code>\u00a0navigation key and add\u00a0<code>'&gt; ip.txt'<\/code>\u00a0to redirect the output to a file as.<\/p>\n<pre>$ ip addr show | grep inet | grep -v 'inet6'| grep -v '127.0.0.1' | awk '{print $2}' | cut -f1 -d\/ &gt; ip.txt\r\n<\/pre>\n<div class=\"google-auto-placed ap_container\">\n<p>Thanks to the life Savior\u00a0<code>UP<\/code>\u00a0navigation key here. Now consider the below condition, the next time I run below one-liner script.<\/p>\n<pre>$ ifconfig | grep \"inet addr:\" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d:\r\n<\/pre>\n<p>As soon as I run script, the bash prompt returned an error with the message\u00a0<code>\u201cbash: ifconfig: command not found\u201d<\/code>, It was not difficult for me to guess I run this command as user where it should be run as root.<\/p>\n<p>So what\u2019s the solution? It is difficult to login to root and then type the whole command again! Also (<strong>UP Navigation Key<\/strong>) in last example didn\u2019t came to rescue here. So? We need to call\u00a0<code>\u201c!!\u201d<\/code>\u00a0without quotes, which will call the last command for that user.<\/p>\n<pre>$ su -c \u201c!!\u201d root\r\n<\/pre>\n<p>Here\u00a0<strong>su<\/strong>\u00a0is switch user which is root,\u00a0<code>-c<\/code>\u00a0is to run the specific command as the user and the most important part\u00a0<code>!!<\/code>\u00a0will be replaced by command and last run command will be substituted here. Yeah! You need to provide root password.<\/p>\n<div id=\"attachment_13435\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/7.gif\"><img decoding=\"async\" class=\"size-medium wp-image-13435\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/7.gif\" alt=\"The Power of !! Key\" aria-describedby=\"caption-attachment-13435\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-13435\" class=\"wp-caption-text\">The Power of !! Key<\/p>\n<\/div>\n<p>I make use of\u00a0<code>!!<\/code>\u00a0mostly in following scenarios,<\/p>\n<p>1. When I run\u00a0<strong>apt-get<\/strong>\u00a0command as normal user, I usually get an error saying you don\u2019t have permission to execute.<\/p>\n<pre>$ apt-get upgrade &amp;&amp; apt-get dist-upgrade\r\n<\/pre>\n<p>Opps error\u2026don\u2019t worry execute below command to get it successful..<\/p>\n<pre>$ su -c !!\r\n<\/pre>\n<p>Same way I do for,<\/p>\n<pre>$ service apache2 start\r\nor\r\n$ \/etc\/init.d\/apache2 start\r\nor\r\n$ systemctl start apache2\r\n<\/pre>\n<p>OOPS User not authorized to carry such task, so I run..<\/p>\n<pre>$ su -c 'service apache2 start'\r\nor\r\n$ su -c '\/etc\/init.d\/apache2 start'\r\nor\r\n$ su -c 'systemctl start apache2'\r\n<\/pre>\n<h6>7. Run a command that affects all the file except ![FILE_NAME]<\/h6>\n<p>The\u00a0<code>!<\/code>\u00a0(<strong>Logical NOT<\/strong>) can be used to run the command on all the files\/extension except that is behind\u00a0<code>'!'<\/code>.<\/p>\n<p><strong>A.<\/strong>\u00a0Remove all the files from a directory except the one the name of which is\u00a0<strong>2.txt<\/strong>.<\/p>\n<pre>$ rm !(2.txt)\r\n<\/pre>\n<p><strong>B.<\/strong>\u00a0Remove all the file type from the folder except the one the extension of which is \u2018<strong>pdf<\/strong>\u2018.<\/p>\n<pre>$ $ rm !(*.pdf)\r\n<\/pre>\n<h6>8. Check if a directory (say \/home\/avi\/Tecmint)exist or not? Printf if the said directory exist or not.<\/h6>\n<p>Here we will use\u00a0<code>'! -d'<\/code>\u00a0to validate if the directory exist or not followed by Logical AND Operator\u00a0<code>(&amp;&amp;)<\/code>\u00a0to print that directory does not exist and Logical OR Operator\u00a0<code>(||)<\/code>\u00a0to print the directory is present.<\/p>\n<p>Logic is, when the output of\u00a0<code>[ ! -d \/home\/avi\/Tecmint ]<\/code>\u00a0is\u00a0<strong>0<\/strong>, it will execute what lies beyond Logical\u00a0<strong>AND<\/strong>else it will go to Logical OR\u00a0<code>(||)<\/code>\u00a0and execute what lies beyond Logical\u00a0<strong>OR<\/strong>.<\/p>\n<pre>$ [ ! -d \/home\/avi\/Tecmint ] &amp;&amp; printf '\\nno such \/home\/avi\/Tecmint directory exist\\n' || printf '\\n\/home\/avi\/Tecmint directory exist\\n'\r\n<\/pre>\n<h6>9. Check if a directory exist or not? If not exit the command.<\/h6>\n<p>Similar to the above condition, but here if the desired directory doesn\u2019t exist it will exit the command.<\/p>\n<pre>$ [ ! -d \/home\/avi\/Tecmint ] &amp;&amp; exit\r\n<\/pre>\n<h6>10. Create a directory (say test) in your home directory if it does not exist.<\/h6>\n<p>A general implementation in Scripting Language where if the desired directory does not exist, it will create one.<\/p>\n<pre>[ ! -d \/home\/avi\/Tecmint ] &amp;&amp; mkdir \/home\/avi\/Tecmint\r\n<\/pre>\n<p>That\u2019s all for now. If you know or come across any other use of\u00a0<code>'!'<\/code>\u00a0which is worth knowing, you may like to provide us with your suggestion in the feedback. Keep connected!<\/p>\n<p><a style=\"font-size: 1rem;\" href=\"https:\/\/www.tecmint.com\/mysterious-uses-of-symbol-or-operator-in-linux-commands\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0&#8216;!&#8217;\u00a0symbol or operator in Linux can be used as\u00a0Logical Negation\u00a0operator as well as to fetch commands from history with tweaks or to run previously run command with modification. All the commands below have been checked explicitly in bash Shell. Though I have not checked but a major of these won\u2019t run in other shell. Here &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/10-amazing-and-mysterious-uses-of-symbol-or-operator-in-linux-commands\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;10 Amazing and Mysterious Uses of (!) Symbol or Operator in Linux Commands&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12701","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12701"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12701\/revisions"}],"predecessor-version":[{"id":12704,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12701\/revisions\/12704"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}