I sometimes end up doing things like:
$ mkdir ~/test-tmp $ cp * ~/test-tmp $ cd ~/test-tmp
using the destination dir 3 times in a row. Isn't there a way to turn these into a one-liner command?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
In bash, the argument to the last command you ran is saved as
!$. This is documented in
! Start a history substitution, except when followed by a blank, newline, carriage return, = or ( (when the extglob shell option is enabled using the shopt builtin). [...] $ The last argument.
So, you could do
$ mkdir ~/test-tmp ; cp * !$ ; cd !$
$ mkdir ~/test-tmp $ cp * !$ $ cd !$
If your concern includes the retyping of
~/test-tmp, you can do the following to shorten and combine the commands into a one-liner:
D=~/test-tmp; mkdir $D; cp * $D; cd $D
Please note that if your path includes spaces, you have to quote the assignment and where you use the variable:
D="~/test tmp"; mkdir "$D" ; cp * "$D"; cd "$D"