How to avoid 'are the same file' warning message when using cp in Linux?
43,333
Solution 1
The problem is that you try to copy a file to itself. You can avoid it by excluding the destination directory from the results of the find command like this:
find "$HOME" -name '*.txt' -type f -not -path "$HOME/newdir/*" -print0 | xargs -0 cp -t "$HOME/newdir"
Solution 2
try using install
instead, this replaces by removing the file first.
install -v target/release/dynnsd-client target/
removed 'target/dynnsd-client'
'target/release/dynnsd-client' -> 'target/dynnsd-client'
and then remove the source files
Solution 3
Make it unique in the process. But this require sorting
find "$HOME" -name '*.txt' -type f -print0 | sort -zu | xargs -0 cp -t "$HOME/newdir"
Or if it's not about the generated files, try to use the -u
option of cp
.
find "$HOME" -name '*.txt' -type f -print0 | xargs -0 cp -ut "$HOME/newdir"
-u copy only when the SOURCE file is newer than the destination file or when the destination file is missing
Solution 4
install
worked perfectly in a Makefile context with docker - thanks!
copy:
@echo ''
bash -c 'install -v ./docker/shell .'
bash -c 'install -v ./docker/docker-compose.yml .'
bash -c 'install -v ./docker/statoshi .'
bash -c 'install -v ./docker/gui .'
bash -c 'install -v ./docker/$(DOCKERFILE) .'
bash -c 'install -v ./docker/$(DOCKERFILE_SLIM) .'
bash -c 'install -v ./docker/$(DOCKERFILE_GUI) .'
bash -c 'install -v ./docker/$(DOCKERFILE_EXTRACT) .'
@echo ''
build-shell: copy
docker-compose build shell
Author by
Matkrupp
Updated on July 11, 2020Comments
-
Matkrupp almost 4 years
I'm trying to copy certain files from one directory to another. Using this command
find "$HOME" -name '*.txt' -type f -print0 | xargs -0 cp -t $HOME/newdir
I get an warning message saying
cp: '/home/me/newdir/logfile.txt' and '/home/me/newdir/logfile.txt' are the same file
How to avoid this warning message?