根据我的理解,以下含义应该完全相同:
ls -1 | xargs file {}
ls -1 | xargs -I{} file {}
Run Code Online (Sandbox Code Playgroud)
如果未指定 -I 选项,则默认为 -I{}。
我想列出当前目录中的所有文件file并对每个文件运行命令。有些名字中有空格。但是,我注意到了差异。见下文:
$ ls -1
Hello World
$ ls -1 | xargs file {}
{}: ERROR: cannot open `{}' (No such file or directory)
Hello: ERROR: cannot open `Hello' (No such file or directory)
World: ERROR: cannot open `World' (No such file or directory)
$ ls -1 | xargs -I{} file {}
Hello World: directory
Run Code Online (Sandbox Code Playgroud)
明确指定 -I{} 后,文件名中的空格会按预期处理。
joh*_*nyB 25
的-I需要定义占位符。该-i选项将假定{}为占位符。这是我man xargs至少在 Cygwin 和 CentOS 上发现任何 {} 假设的地方。
不带任何一个选项调用的 xargs 不需要占位符,它只是将 STDIN 附加到参数的末尾。
只需添加echo到您的示例中即可查看 xargs 正在做什么:
$ ls -1
Hello World/
Run Code Online (Sandbox Code Playgroud)
您的示例错误地使用了{}:
$ ls -1 | xargs echo file {}
file {} Hello World/
Run Code Online (Sandbox Code Playgroud)
所以filecmd 会看到 args{} Hello World和错误。
如果要{}在 xargs 调用中显式使用:
$ ls -1 | xargs -I{} echo file {}
file Hello World/
Run Code Online (Sandbox Code Playgroud)
或者没有占位符:
$ ls -1 | xargs echo file
file Hello World/
Run Code Online (Sandbox Code Playgroud)
上面调用的 xargs 不需要 {}。它在没有占位符的情况下将 STDIN 附加到命令的末尾。{} 的使用通常意味着您希望在 cmd 中间某处执行 STDIN,如下所示:
$ ls -1 | xargs -i mv {} /path/to/someplace/.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15473 次 |
| 最近记录: |