I need to make a copy of my home directory and place that copy in the same home directory. The exit code of the command must be 0. Currently, my home directory does not contain any other directories.

Is there a better way than the following? (pwd is the home directory)

mkdir /tmp/temp && cp * /tmp/temp && mv /tmp/temp .

Call rsync and exclude the directory where you're putting the copy.

mkdir copy
rsync -a --exclude=copy . copy

Copying * excludes dot files (files whose name begins with a .), which are common and important in a home directory.


That will neglect to copy any .* files that may exist in your home directory. I'd probably use rsync like so:

$ mkdir /tmp/temp && rsync -av . /tmp/temp/. && rsync -av /tmp/temp/ .

I'm not that thrilled about doing this as a chain of cmd1 && cmd2 && cmd3, since problems can occur, but if you're careful it should be OK.

  • Nice use of rsync! – Barun Dec 29 '13 at 19:18

How about creating a temporary directory in your home directory itself and then copying?

mkdir tmp
for item in *
    if [[ "$item" != tmp ]]
        cp "$item" tmp

This is an alternative, but can't say if better though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.