在 Linux 中使用 cp 时如何避免“是同一个文件”警告消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18153878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to avoid 'are the same file' warning message when using cp in Linux?
提问by Matkrupp
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
cp: '/home/me/newdir/logfile.txt' 和 '/home/me/newdir/logfile.txt' 是同一个文件
How to avoid this warning message?
如何避免此警告消息?
采纳答案by user000001
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 命令的结果中排除目标目录来避免它,如下所示:
find "$HOME" -name '*.txt' -type f -not -path "$HOME/newdir/*" -print0 | xargs -0 cp -t "$HOME/newdir"
回答by konsolebox
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
.
或者,如果它不是生成的文件,尝试使用-u
的选项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
回答by teknopaul
try using install
instead, this replaces by removing the file first.
尝试install
改用,这会通过先删除文件来替换。
install -v target/release/dynnsd-client target/
removed 'target/dynnsd-client'
'target/release/dynnsd-client' -> 'target/dynnsd-client'
and then remove the source files
然后删除源文件