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?
Best Answer
The command
cp Foo Bar
says "Copy theFoo
directory toBar
". If it already exists, it will putFoo
inside ofBar
. If you want explicitly to copy the contents ofFoo
, usecp -r Foo/ Bar/
or, better, usersync -a
. To elaborate: ifBar
does not exist, it will be created wtihcp
; if it does not, it will act as the destination into which you want to copy files.