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?
By default, the cp command runs in the same directory you are working in. However, the same file cannot exist twice in the same directory. You'll need to change the name of the target file to copy in the same location.
--no-clobber Do not overwrite an existing file. If -i/--interactive was previously specified, this option overrides it. This option cannot be specified with -b/--backup, because backups are only created when a file would be overwritten.
Syntax: cp [OPTION] Source Destination cp [OPTION] Source Directory cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory First and second syntax is used to copy Source file to Destination file or Directory. Third syntax is used to copy multiple Sources(files) to Directory.
You use the cp command for copying files from one location to another. This command can also copy directories (folders). The syntax of this command is: cp [... file/directory-sources] [destination] [file/directory-sources] specifies the sources of the files or directories you want to copy.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With