Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I sometimes end up doing things like:


from ~/blah

$ 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?

share|improve this question
You can refer something here: unix.stackexchange.com/questions/136599/… – cuonglm Jul 17 '14 at 14:04
@Gnouc you should make it as an answer to this question – fduff Jul 17 '14 at 14:19

You mean like this?

mkdir ~/test-tmp && cp * ~/test-tmp && cd ~/test-tmp


function mm() {
  local dir=$1
  if [ ! -z "$dir" ]
    mkdir ~/${dir} && cp * ~/${dir} && cd ~/${dir}
share|improve this answer

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"
share|improve this answer
nice one! although the export isn't required in this case. – fduff Jul 17 '14 at 14:20
@fduff You are right. From my bash history I see I first tried A=1 echo $A , when that did not work (because of the missing semi-colon and echo being build-in), I both added the export and the semi-colon in the next try – Anthon Jul 17 '14 at 14:33

In bash, the argument to the last command you ran is saved as !$. This is documented in man bash:

   !      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 !$

Or, simply

$ mkdir ~/test-tmp
$ cp * !$
$ cd !$
share|improve this answer

Your Answer


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.