Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

copy

cp

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?

like image 233
Matkrupp Avatar asked Aug 09 '13 19:08

Matkrupp


People also ask

Are the same file Linux cp?

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.

Which option is used with cp command to avoid over writes?

--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.

Which is the correct format for using cp command?

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.

What does cp * mean in Linux?

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.


2 Answers

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"  
like image 61
user000001 Avatar answered Oct 06 '22 11:10

user000001


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

like image 24
teknopaul Avatar answered Oct 06 '22 12:10

teknopaul