4

Let me explain my problem with a simpler scenario. We have these two directories: ~/Foo and ~/Bar. Foo contains the files test1.txt and test2.txt. If I run the command cp -R Foo Bar, the Bar directory now contains a Foo folder containing the two text files. However, if I run the command cp -R Foo Bar when the Bar directory does not yet exist before the command is run, the newly created Bar directory will not contain a Foo subdirectory but only the two .txt files.

Why is it when I run the command in the first situation that it creates a subdirectory with the two files but in the second situation it just copies the two files into the new directory? Is the cp program supposed to function in this way?

4

The command cp Foo Bar says "Copy the Foo directory to Bar". If it already exists, it will put Foo inside of Bar. If you want explicitly to copy the contents of Foo, use cp -r Foo/ Bar/ or, better, use rsync -a. To elaborate: if Bar does not exist, it will be created wtih cp; if it does not, it will act as the destination into which you want to copy files.

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.