cp, trailing slash, and copying to the same vs. to another directory
3
votes
1
answer
715
views
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)
Asked by jsx97
(1347 rep)
Mar 10, 2024, 06:25 PM
Last activity: Mar 14, 2024, 11:38 PM
Last activity: Mar 14, 2024, 11:38 PM