Hack 62. Substitute a specific argument for a specific command

by Ramesh

In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.

  1. # cp ~/longname.txt /really/a/very/long/path/long-filename.txt
  2.  
  3. # ls -l !cp:2
  4. ls -l /really/a/very/long/path/long-filename.txt

In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.

  1. # ls -l !cp:$
  2. ls -l /really/a/very/long/path/long-filename.txt