Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 .
share|improve this question

3 Answers 3

up vote 4 down vote accepted

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

cd
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.

share|improve this answer

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

mkdir tmp
for item in *
do
    if [[ "$item" != tmp ]]
    then 
        cp "$item" tmp
    fi
done

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

share|improve this answer

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.

share|improve this answer
    
Nice use of rsync! –  Barun Dec 29 '13 at 19:18

Your Answer

 
discard

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.