Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I saw a blog post awhile back about a unix time-saving trick that for the life of me I have not been able to remember and the man page does not list.

Basically it was a way to cd into the directory you just copied or moved something to. Example:

$pwd
/home/stedy
$mv temp /projects/trunk
$cd something
$pwd
/projects/trunk

Has anyone ever seen this before? What am I forgetting after the cd command?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

One option is to use history commands:

$pwd
/home/stedy
$mv temp /projects/trunk
$cd !:2
$pwd
/projects/trunk

!:2 takes the second argument of the previous command. You can vary pleasures immensely with modifiers, your best friend is man bash or info bash and look for the history expansion (and modifiers).

share|improve this answer
1  
!$ gives the last argument of the previous command and is more likely to consistently do what is intended without having to think about how many arguments there were, particularly in the case of something like cp foo bar baz dest/ –  Dennis Williamson Dec 7 '10 at 2:06

You can press Alt-. to get the last argument of the previous command in Bash, ksh and zsh. The keystroke is actually designated as Meta-. which usually means pressing Esc then ., but the Alt combination is commonly available. Note that an underscore will work in place of the dot. See the Readline section of man bash for more information. The readline function is called yank-last-arg.

share|improve this answer

In most shells the last parameter you gave on the previous command is in $_

mkdir /some/dir
cd $_
# now in /some/dir
share|improve this answer

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.