29

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?

2
  • 3
    your_command 2>/dev/null =) Commented Aug 9, 2013 at 19:09
  • 1
    Yes, this works but not quite the solution I was looking for :-) With this I get a rid of this warning but cannot avoid it still Commented Aug 9, 2013 at 19:27

5 Answers 5

24

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" 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but this generates the same warning in Ubuntu.
4

Try using install instead. This replaces by removing the file first.

install -v target/release/dynnsd-client target/ 

Output:

removed 'target/dynnsd-client' 'target/release/dynnsd-client' -> 'target/dynnsd-client' 

And then remove the source files.

Comments

1

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 

1 Comment

Thanks, but both of these solutions generate the same warning message in Ubuntu bash.
1

Try using rsync instead of cp:

find "$HOME" -name "*.txt" -exec rsync {} "$HOME"/newdir \; 

1 Comment

rsync worked, nice!
0

Install

It worked perfectly in a Makefile context with Docker:

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.