Linux 查找和复制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5241625/
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
Find and copy files
提问by shantanuo
Why does the following does not copy the files to the destination folder?
为什么下面没有将文件复制到目标文件夹?
# find /home/shantanu/processed/ -name '*2011*.xml' -exec cp /home/shantanu/tosend {} \;
cp: omitting directory `/home/shantanu/tosend'
cp: omitting directory `/home/shantanu/tosend'
cp: omitting directory `/home/shantanu/tosend'
采纳答案by malcolmpdx
If your intent is to copy the found files into /home/shantanu/tosend you have the order of the arguments to cp reversed:
如果您的目的是将找到的文件复制到 /home/shantanu/tosend 中,则 cp 的参数顺序颠倒了:
find /home/shantanu/processed/ -name '*2011*.xml' -exec cp "{}" /home/shantanu/tosend \;
Note: find command use {}as placeholder for matched file
注意:find 命令使用{}作为匹配文件的占位符
回答by Ignacio Vazquez-Abrams
You need to use cp -t /home/shantanu/tosend
in order to tell it that the argument is the target directory and not a source. You can then change it to -exec ... +
in order to get cp
to copy as many files as possible at once.
您需要使用cp -t /home/shantanu/tosend
来告诉它参数是目标目录而不是源。然后,您可以将其更改为-exec ... +
,以便cp
一次复制尽可能多的文件。
回答by Thiyagu ATR
i faced an issue something like this...
我遇到了这样的问题......
Actually, in two ways you can process find
command output in copy
command
实际上,可以通过两种方式find
在copy
命令中处理命令输出
If
find
command's output doesn't contain any space i.e if file name doesn't contain space in it then you can use below mentioned command:Syntax:
find <Path> <Conditions> | xargs cp -t <copy file path>
Example:
find -mtime -1 -type f | xargs cp -t inner/
But most of the time our production data files might contain space in it. So most of time below mentioned command is safer:
Syntax:
find <path> <condition> -exec cp '{}' <copy path> \;
Example
find -mtime -1 -type f -exec cp '{}' inner/ \;
如果
find
命令的输出不包含任何空格,即如果文件名中不包含空格,那么您可以使用下面提到的命令:句法:
find <Path> <Conditions> | xargs cp -t <copy file path>
例子:
find -mtime -1 -type f | xargs cp -t inner/
但大多数情况下,我们的生产数据文件中可能包含空间。所以大部分时间下面提到的命令更安全:
句法:
find <path> <condition> -exec cp '{}' <copy path> \;
例子
find -mtime -1 -type f -exec cp '{}' inner/ \;
In the second example, last part i.e semi-colon is also considered as part of find
command, that should be escaped before press the enter button. Otherwise you will get an error something like this
在第二个例子中,最后一部分,即分号也被视为find
命令的一部分,应该在按下回车按钮之前转义。否则你会得到类似这样的错误
find: missing argument to `-exec'
In your case, copy command syntax is wrongin order to copy find file into /home/shantanu/tosend
. The following command will work:
在你的情况下,复制命令语法是错误的,以便找到文件复制到/home/shantanu/tosend
。以下命令将起作用:
find /home/shantanu/processed/ -name '*2011*.xml' -exec cp {} /home/shantanu/tosend \;
回答by Sebin John
The reason for that error is that you are trying to copy a folder which requires -r option also to cp Thanks
该错误的原因是您正在尝试将需要 -r 选项的文件夹复制到 cp 谢谢
回答by Robert A
for i in $(ls); do cp -r "$i" "$i"_dev; done;