To copy a directory to another directory, the first directory should be written without the trailing slash:
# example 1
# this command will copy dir1 to dir2
# (dir2 is preexisting)
cp -Rip dir1 dir2/
Otherwise, the command will copy directory contents and not the directory itself:
# example 2
# this command will copy dir1 contents to dir2
# (dir2 is preexisting)
cp -Rip dir1/ dir2/
I do understand the difference between dir1
and dir1/
here, and the difference between how these two commands behave doesn't confuse me.
But to copy the directory not to another directory but to the same directory where it is currently located, the trailing slash won't make any difference. Why?
# example 3
# any of these commands will make a dir1 copy
# (dir1-copy isn't preexisting)
cp -Rip dir1 dir1-copy/
cp -Rip dir1/ dir1-copy/
And another, closely related question. Why there is no difference between how mv dir1/ dir2/
and mv dir1 dir2/
work? In other words, why, in respect of trailing slash in the end of the source directory, mv
follows the logic of the third cp
example, and not the logic of examples one and two?
macOS 14.3.1, zsh 5.9 (x86_64-apple-darwin23.0)
dir1
intodir2
? On what system? – Gilles 'SO- stop being evil' Mar 11 '24 at 10:35rsync
wherersync -a dir1 dir2/
andrsync -a dir1/ dir2/
give different results. AFAIKcp
shall not do this. Please redo your tests and confirm yourcp
behaves like you described. – Kamil Maciorowski Mar 11 '24 at 10:56type -a cp
? – Kamil Maciorowski Mar 11 '24 at 19:01cp is /bin/cp
– jsx97 Mar 11 '24 at 19:10cp -R
in macOS is supposed to behave likersync
in this matter: "If thesource_file
ends in a/
, the contents of the directory are copied rather than the directory itself". Quite a surprise for me. – Kamil Maciorowski Mar 11 '24 at 19:17