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)

Peter Boughton
  • 594
  • 2
  • 7
  • 19

5 Answers5

12

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

thepocketwade
  • 1,545
  • 5
  • 17
  • 27
5

Try

cd !$
Christian Lescuyer
  • 3,144
  • 1
  • 17
  • 6
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>+.
katriel
  • 4,477
  • 23
  • 20
  • Thanks, also helpful since sometimes I might not want the immediately preceding command. – Peter Boughton Aug 03 '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 03 '09 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)

chicks
  • 3,793
  • 10
  • 27
  • 36
Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
2

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

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253