我有一份文件列表,可以通过以下命令查看。但是,如何将列出的这些文件复制到另一个文件夹中,例如~/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/
-name
使用通配符匹配,而非正则表达式。这是一个重要的区别... - sanmiguel事实上,您可以以两种方式在复制命令中处理find
命令的输出:
If the find
command's output doesn't contain any space, i.e if the filename doesn't contain a space in it, then you can use:
Syntax:
find <Path> <Conditions> | xargs cp -t <copy file path>
Example:
find -mtime -1 -type f | xargs cp -t inner/
But our production data files might contain spaces, so most of time this command is effective:
Syntax:
find <path> <condition> -exec cp '{}' <copy path> \;
Example
find -mtime -1 -type f -exec cp '{}' inner/ \;
find
命令的一部分,在按下Enter之前应该进行转义。否则会出现类似以下的错误:find: missing argument to `-exec'
cp: missing file operand
。如何忽略这个错误? - Lei Yangfind /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
xargs
。这样,您就可以分批复制文件。 - Eric Jablow+;
运行-exec
命令,它将会批量复制文件,而如果你使用\;
,它将会为每个找到的文件运行一个cp
命令。干杯! - domdambrogia