10

So lets assume I've just done:

mv ./myfile /to/some/other/place/

And I now decide I want to follow the file, and go into that directory.

Whilst I could head for the mouse, select the text, type 'cd ', then right-click to paste - I'd prefer a faster keyboard-based directory.

So, what's the best way to do that?
(In general, and if different, Centos+Bash specifically)

5 Answers 5

12

If you type "!$" it will print the last argument of the previous line. Which will be the directory you moved the file into.

3
  • Best thing I've learned all day. Aug 4, 2009 at 15:08
  • 1
    Rory's cd $_ is Posix, so it will work on ksh and bourne shell as well as bash.
    – kmarsh
    Aug 6, 2009 at 12:21
  • 1
    "cd $_" doesn't work in csh or tcsh, but it does work in zsh. csh and tcsh support !$, though. Just for the record. Aug 6, 2009 at 13:48
5

Try

cd !$
1
  • 1
    Thanks - this works, but thepocketwade gave a fuller answer so have accepted his one. Aug 3, 2009 at 21:06
4

Try "cd" and then "[Alt] + ." (can be used repeatedly) It will scroll all your previous commands last parameter. So it will look like:

mv ./myfile /to/some/other/place/
cd <Alt>+.
2
  • Thanks, also helpful since sometimes I might not want the immediately preceding command. Aug 3, 2009 at 21:03
  • Here's another useful bash trick for you: Using Ctrl+r will let you quickly search your history by typing in a partial command. Can also be used repeatedly to scroll back to older parts of your history
    – katriel
    Aug 3, 2009 at 21:08
4

Esc-. (Escape followed by Period) Gives you the last argument of the previous command, it is a readline shortcut. You can type it many times to cycle through the last arguments of previous commands. Readline is a command line entry library that is used by many shells (such as bash, same maintainer), irc clients, etc.

This is probably my favorite keyboard shortcut (followed by ctrl-a for start of line and ctrl-e for end of line), give it a try ;-)

Update: Oh, katriel posted Alt-. , this is the same thing, just different a key (Alt instead of Esc)

2

You can also use $_ as the last argument of the last command line

You must log in to answer this question.

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