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)

link|improve this question

50% accept rate
feedback

5 Answers

up vote 12 down vote accepted

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

link|improve this answer
Thanks. :) – Peter Boughton Aug 3 '09 at 21:00
Best thing I've learned all day. – markdrayton Aug 4 '09 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 '09 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. – thepocketwade Aug 6 '09 at 13:48
feedback

Try

cd !$
link|improve this answer
Thanks - this works, but thepocketwade gave a fuller answer so have accepted his one. – Peter Boughton Aug 3 '09 at 21:06
feedback

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>+.
link|improve this answer
Thanks, also helpful since sometimes I might not want the immediately preceding command. – Peter Boughton Aug 3 '09 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 '09 at 21:08
feedback

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 (Esc instead of Period)

link|improve this answer
feedback

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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