Ubuntu – How to use some of the arguments of a previous command without retyping

command line

Back to Run a command with the argument from the last command. This post explained how to use previous commands argument in the new command.

My question is how to iterate among those arguments?

For example: just example not this needed

suppose I create two directories dir1 and dir2

mkdir dir1 dir2

I want to move dir1 to dir2 without retyping the arguments again. This is very handful when dealing with complex arguments or long paths.

The simplest command is needed

UPDATE

Suppose I have a command

mkdir arg1 arg2 arg3 arg4 ... argn

I want to move arg2 to argn

Best Answer

I've found some interesting option of using !.

To get the arguments of previous command you can use:

!^        gives the first argument
!:n       gives the nth argument
!*        gives all the arguments

So in case of

mkdir arg1 arg2 arg3 arg4 ... argn

To move arg2 to arg5 for example use :

mv !:2  !:5