我有一个使用下面的命令看到的某些文件的列表,但是如何将列出的这些文件复制到另一个文件夹中,例如〜/ test?
find . -mtime 1 -exec du -hc {} +
添加到Eric Jablow的答案,这是一个可能的解决方案(它对我有用 - linux mint 14 /nadia)
find /path/to/search/ -type f -name "glob-to-find-files" | xargs cp -t /target/path/
您可以参考“https://stackoverflow.com/questions/143171/how-can-i-use-xargs-to-copy-files-that-have-spaces-and-quotes-in-their-names/ 149026#149026" 还有。
实际上,您可以通过两种方式处理复制命令中的
find
命令输出:
如果
find
命令的输出不包含任何空格,即文件名中不包含空格,那么您可以使用:
Syntax:
find <Path> <Conditions> | xargs cp -t <copy file path>
Example:
find -mtime -1 -type f | xargs cp -t inner/
但是我们的生产数据文件可能包含空格,所以大多数时候这个命令是有效的:
Syntax:
find <path> <condition> -exec cp '{}' <copy path> \;
Example
find -mtime -1 -type f -exec cp '{}' inner/ \;
在第二个示例中,最后一部分,分号也被视为
find
命令的一部分,应在按 Enter 之前转义。否则你会得到类似的错误:
find: missing argument to `-exec'
find /PATH/TO/YOUR/FILES -name NAME.EXT -exec cp -rfp {} /DST_DIR \;
如果您使用 GNU find,
find . -mtime 1 -exec cp -t ~/test/ {} +
这与将输出通过管道传输到
xargs
一样有效,同时避免了这样做的陷阱(它可以处理嵌入的空格和换行符,而无需使用 find ... -print0 | xargs -0 ...
)。
这对我来说是最好的方法:
cat filename.tsv |
while read FILENAME
do
sudo find /PATH_FROM/ -name "$FILENAME" -maxdepth 4 -exec cp '{}' /PATH_TO/ \; ;
done