Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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)

share|improve this question

5 Answers 5

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.

share|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

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

share|improve this answer

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)

share|improve this answer

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>+.
share|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

Try

cd !$
share|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

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.