如何在Linux中同时运行find和cp命令? [英] How do I run find and cp commands concurrently in Linux?

查看:220
本文介绍了如何在Linux中同时运行find和cp命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何同时运行 find cp 命令?我试过这个:

  find -name* pdf *| xargs cp $ 1 ./ 

但是不行。


<使用 -exec 选项:

  find ./ -name* pdf *-exec cp -t。 {



$ {} 用$ 找到的手册页:


-exec命令{} +

...命令行是通过将每个选定的文件名添加到末尾来构建的。
命令行的构建方式与 xargs 构建方式大致相同它的命令行。

使用 -t (目标目录)选项(这是一个GNU扩展)。我们不能使用 -exec cp {}。 + ,因为匹配的文件名被附加到命令行的末尾,而目的地必须被最后指定。另一个解决方法是调用 sh



$ p $ find $ .c -name * pdf *-exec sh -c'cp$ @。'''{} +

我习惯性的逃避了 + 字符。请注意,您应该转义 find 语法的特殊字符,以防止它们被shell扩展。特别是,在 + 之前可能不需要反斜杠,因为大多数shell将它解释为一个字符串(它不会被扩展为不同的东西)。然而,你一定要转义/引用; (它将命令按顺序应用于每个文件):

  find -name* pdf *-exec cp -f {}。 ';'


How do I run find and cp commands concurrently? I have tried this:

find -name "*pdf*" | xargs cp $1 ./

But it doesn't work.

解决方案

Use -exec option:

find ./ -name "*pdf*" -exec cp -t . {} \+

The {} is replaced with the current file names being processed.

From the man page for find:

-exec command {} +

...the command line is built by appending each selected file name at the end.. The command line is built in much the same way that xargs builds its command lines.

Note the use of -t (target directory) option (which is a GNU extension). We cannot use -exec cp {} . +, because the matched file names are appended to the end of the command line, while destination would have to be specified last. Another workaround is to invoke sh:

find ./ -name "*pdf*" -exec sh -c 'cp "$@" .' '' {} +

I have habitually escaped the + character. Note, you should escape special characters of the find syntax to protect them from expansion by the shell. In particular, there is likely no need in backslash before +, because most shells will interpret it as a string (it will not be expanded to something different). However, you will definitely have to escape/quote the ; (which applies the command to each file sequentially):

find -name "*pdf*" -exec cp -f {} . ';'

这篇关于如何在Linux中同时运行find和cp命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆