Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 !!
share|improve this question
add comment

3 Answers

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

http://www.gnu.org/software/bash/manual/bashref.html

share|improve this answer
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 '13 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. –  galegosimpatico Oct 31 '13 at 10:11
    
Depends on where you live. Thank you anyway! –  aldorado Oct 31 '13 at 10:20
add comment

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.

share|improve this answer
add comment

All of these will accomplish what you want:

rm !$

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

rm!:1

!: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.

^cat^rm^

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.