如何告诉xargs选择哪个参数?

阿卜杜勒·哈兹雷德(Abdul Al Hazred)|

当xargs将第一个命令输出重定向到第二个命令的参数时,并且对于输出的哪个元素没有选择哪个参数,则只有一种方法,例如:

ls | xargs file  # there are as many arguments as files in the listing,
                 # but the user does not have too choose himself

现在,如果需要选择:

ls | xargs file | grep image | xargs mv.....   # here the user has to 
                                               # deal  with two arguments of mv, first for source second for destination, suppose your destination argument is set already by yourself and you have to put the output into the source argument. 

您如何告诉xargs将第一个命令的标准输出重定向到您选择的第二个命令的参数中?

在此处输入图片说明

斯蒂芬·基特

您可以使用-I定义一个占位符,该占位符将替换为馈给的参数的每个值xargs例如,

ls -1 | xargs -I '{}' echo '{}'

echols的输出中每行调用一次您经常会看到已'{}'用过,大概是因为它与find的占位符相同。

在您的情况下,您还需要预处理file输出以提​​取匹配的文件名;因为那里有一个grep,所以我们可以只使用awk它们来做,并简化file调用:

file * | awk -F: '/image/ { print $1 }' | xargs -I '{}' mv '{}' destination

如果您拥有GNU mv,则可以使用它-t来传递多个源文件:

file * | awk -F: '/image/ { print $1 }' | xargs mv -t destination

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章