7

When using the command I know a friend of mine who used to re-use last part of previous command. He unfortunately moved far away and I can't get hold of him.

First:

cp -r folder ~/folder

Then he was re-using ~/folder in a very quick way.

Anyone who knows how to do this?

2 Answers 2

8

The variable $_ is used to substitute the most recent parameter.

So, in the example you mentioned, you'd do something like:

cp -r folder ~/folder
cd $_

cd $_ will change directory to the most recent parameter i.e. ~/folder.

For more such variables, have a look at https://stackoverflow.com/a/5163260/1626345.

3
  • beautiful if you ask me
    – don.joey
    Mar 7, 2013 at 15:07
  • 2
    Is there any difference between $_ and !$?
    – Yamaneko
    Mar 7, 2013 at 15:49
  • No, normally, they function the same.
    – green
    Mar 7, 2013 at 16:06
4

With the arrow up key you can quickly access the last command you used.

With Ctrl+R you can do a 'reverse-i-search'. That means you just type in any letter or word of a command you already used and it will show up when enough identifying input is given.

Typing !$ will insert the last word (or a whole path if it's one) of the last command you used. See here for a lot of useful tips. I just bookmarked it. :)

If you are planning to do this quite often with the same paths, this tutorial might help.

You must log in to answer this question.

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