How to avoid 'are the same file' warning message when using cp in Linux? How to avoid 'are the same file' warning message when using cp in Linux? linux linux

How to avoid 'are the same file' warning message when using cp in Linux?


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" 


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


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