Linux 使用 Bash 查找和复制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1562102/
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 file using Bash
提问by Wadih M.
Anybody has an alternate way of finding and copying files in bash than:
任何人都有在 bash 中查找和复制文件的替代方法,而不是:
find . -ctime -15 | awk '{print "cp " " ../otherfolder/"}' | sh
I like this way because it's flexible, as I'm building my command (can by any command) and executing it after.
我喜欢这种方式,因为它很灵活,因为我正在构建我的命令(可以通过任何命令)并在之后执行它。
Are there other ways of streamlining commands to a list of files?
是否有其他方法可以将命令简化为文件列表?
Thanks
谢谢
采纳答案by asveikau
I would recommend using find
's -exec
option:
我建议使用find
's-exec
选项:
find . -ctime 15 -exec cp {} ../otherfolder \;
find . -ctime 15 -exec cp {} ../otherfolder \;
As always, consult the manpage for best results.
与往常一样,请查阅联机帮助页以获得最佳结果。
回答by tangens
I usually use this one:
我通常使用这个:
find . -ctime -15 -exec cp {} ../otherfolder/ \;
回答by Norman
-exec is likely the way to go, unless you have far too many files. Then use xargs.
-exec 可能是要走的路,除非你有太多的文件。然后使用 xargs。
回答by Andrey Vlasovskikh
You can do it with xargs
:
你可以这样做xargs
:
$ find . -ctime 15 -print0 | xargs -0 -I{} cp {} ../otherfolder
See also grep utility in shell script.
另请参阅shell 脚本中的 grep 实用程序。
回答by Idelic
If your cp
is GNU's:
如果您cp
是 GNU 的:
find . -ctime 15 -print0 | xargs --no-run-if-empty -0 cp --target-directory=../otherfolder
回答by hardihood07
Use this for copy and many other things:
将此用于复制和许多其他事情:
for f in $(find /apps -type f -name 'foo'); do cp ${f} ${f}.bak; cmd2; cmd3; done;