Nov 1, 2018

Useful subset of Linux band commands

Linux band commands


  • !! --Will execute the last command. Same as !-1 or "up-arrow + return"
  • !n --Will execute the line n of the history record. 
  • !-n --Will execute the command n lines back. 
  • !gzip  -- will re-execute the most recent gzip command (history is searched  in reverse order). string specified is considered to be prefix of the necessary command.
  • !?etc.gz – same as above but the unique string doesn't have to be at the start of the command. That means that you can identify command by a unique string appearing anywhere in the command  (exactly like in Ctrl-R  mechanism)
What is more important and convenient is that you can extract arguments from previous commands  (see below). for example:
  • !!:1 designates the first argument of the last command.  This can be shortened to !1.
  • !!:$ designates the last argument of the preceding command. This may be shortened to !$.
 There are a couple of useful idioms:
  • sudo !! -- reexecute the last command with prefix sudo.
  • cd !$ -- cd to the directory which is the last argument of the previous command. This idiom is based on the ability to specify the last argument of the prev command. !!:$  designates the last word of the preceding command. This can be shortened to !$.  The addressing reminds vi and as in vi you can work with any command in history, not only the last. See below
  • Modification of the cd command when two long (deeply nested) directories differ in only one part, especially if this part is somewhere in the middle. You can modify the command on the fly using search-and-replace type modifier  s/old/new/ (or s^old^new^ -- the delimiter is actually arbitrary)
  • use :p suffix with the ! for repeating a command will cause the command to be displayed, but not run( See modifiers for more info)  Useful if you want to repeat a command, but want to double check the command you want to run. For example if it perform some action on system files or directories. In the example below !string  runs the most recent command beginning with the string you specify.  Using Ctrl-R might be a better deal
  • !tail:p