I know that there is the !! command to reuse the previous command. How would one reuse the file or directory specified in the previous command but use a different command on it?

Example: I want to see what is the content of a file, and if necessary delete it. Something like:

$ cat example.txt

"output indicates deletion"

$ rm !!

3 Answers 3

$ echo foo
$ bar !!:$
bar foo
The program 'bar' is currently not installed...


  • 2
    Nice one! I read the manual you linked below your answer and suggest you add to your answer that the command can even be shortened to !$
    – aldorado
    Oct 31, 2013 at 10:07
  • It's past 11 AM but it's even too early in the morning to have a full-fledged brain blood pressure.
    – 41754
    Oct 31, 2013 at 10:11
  • Depends on where you live. Thank you anyway!
    – aldorado
    Oct 31, 2013 at 10:20

You can use:

  • rm Esc. - type rm followed by Space and after press Esc followed by . (not in the same time). If the previous command has no arguments, you will get the previous command itself.
  • rm Alt+. - type rm followed by Space and after press Alt and . (in the same time). In fact, using this way and continuing to press . (without to release Alt), you will get the last argument for every command from history. If a command has no arguments, you will get the command itself.
  • rm !* - in this case you will get all arguments from the previuos command. If the previous command has no arguments, you will get nothing.

In general: <command> Esc. or <command> Alt+. or <command> !*.

Source: Run a command with the argument from the last command.


All of these will accomplish what you want:

rm !$

The !$ means the last argument of the last command.


!:1 means argument 1 of the last command, where the command itself is argument 0. You can pick out individual arguments from the last command this way.


Replace the string cat with the string rm in the last command and run the result. You can actually leave off the last ^ in this simple case.

rm !*

This runs rm on all of the non-command arguments in the last command. So arguments from 1 to $. This variant and the last one are useful if you had more than one argument to the last command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .