Abd*_*red 14 command-line pipe xargs
当 xargs 将第一个命令输出重定向到第二个命令的参数并且无法选择输出的哪个元素的哪个参数时,那么只有一种方法,例如:
ls | xargs file # there are as many arguments as files in the listing,
# but the user does not have too choose himself
Run Code Online (Sandbox Code Playgroud)
现在如果需要选择:
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.
Run Code Online (Sandbox Code Playgroud)
您如何告诉 xargs 将第一个命令的标准输出重定向到您选择的第二个命令的参数中?

Ste*_*itt 35
您可以使用-I来定义一个占位符,该占位符将被提供给 的参数的每个值替换xargs。例如,
ls -1 | xargs -I '{}' echo '{}'
Run Code Online (Sandbox Code Playgroud)
将从的输出中echo每行调用一次ls。您会经常看到'{}'used,大概是因为它与find的占位符相同。
在您的情况下,您还需要预处理file的输出以提取匹配的文件名;因为那里有一个grep我们可以awk用来做这两个,并简化file调用:
file * | awk -F: '/image/ { print $1 }' | xargs -I '{}' mv '{}' destination
Run Code Online (Sandbox Code Playgroud)
如果你有 GNU,mv你可以-t用来传递多个源文件:
file * | awk -F: '/image/ { print $1 }' | xargs mv -t destination
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16161 次 |
| 最近记录: |