Is there a way to check if cp -r
would overwrite anything, without actually copying anything?
-n
seems to copy files that don't collide, which is not what I want.
Is there a way to check if cp -r
would overwrite anything, without actually copying anything?
-n
seems to copy files that don't collide, which is not what I want.
The easiest way is to use the rsync
command there you have a lot of parameters to override the target or delete files on the target if you want.
https://linux.die.net/man/1/rsync
or like mentioned in the comments you could use cp -n
for --no-clobber
. But in general i would suggest rsync there you have a lot more possibilities.
cp has the -i --interactive option and the -n, --no-clobber option. See `man 1 cp'.
To only check if the file exists in the target location, but not actually perform any copying you can use the diff
command with the -r
option.
From man diff
:
-r, --recursive
recursively compare any subdirectories found
diff -r --brief --report-identical-files /source-folder/ /target-folder/ | grep -v '^Only in'
--brief
is listing files that differ (same as -q
)--report-identical-files
does the same for files that don't differ (same as -s
)grep -v '^Only in'
removes files that are only in one directory tree, because those will not be overwritten.The output will look something like this:
Files /source-folder/file1.txt and /target-folder/file1.txt are identical
Files /source-folder/file2.jpg and /target-folder/file2.jpg differ