linuxbashcopycp

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


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 can I avoid this warning message?


Solution

  • 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"