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?
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
install
worked perfectly in a Makefile context with docker - thanks!
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
尝试使用rsync
而不是cp
:
find "$HOME" -name "*.txt" -exec rsync {} "$HOME"/newdir \;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.