小编for*_*tyj的帖子

xargs -I 替换-str 选项区别

根据我的理解,以下含义应该完全相同:

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{} 后,文件名中的空格会按预期处理。

bash xargs

19
推荐指数
1
解决办法
2万
查看次数

标签 统计

bash ×1

xargs ×1