手动指定输入时如何对 xargs 使用 -0 选项?
我得到
~/bashpractice$ ls
dir3 dir1
了
~/bashpractice$ xargs ls -l
dir1 dir3
dir1:
total 0
-rw-r--r-- 1 abc abc 0 2011-05-23 10:19 file1
-rw-r--r-- 1 abc abc 0 2011-05-23 10:19 file2
dir3:
total 0
-rw-r--r-- 1 abc abc 0 2011-05-23 10:20 file1
-rw-r--r-- 1 abc abc 0 2011-05-23 10:20 file2
但是当我这样做时出现错误
~/bashpractice$ xargs -0 ls -l
dir1 dir3
ls: cannot access dir1 dir3
: No such file or directory
abc@us-sjc1-922l:~/bashpractice$ xargs -0 ls -l
dir1
dir3
ls: cannot access dir1
dir3
: No such file or directory
How to get alisting when指定 -0 选项到 xargs ?
I have
~/bashpractice$ ls
dir3 dir1
I get
~/bashpractice$ xargs ls -l
dir1 dir3
dir1:
total 0
-rw-r--r-- 1 abc abc 0 2011-05-23 10:19 file1
-rw-r--r-- 1 abc abc 0 2011-05-23 10:19 file2
dir3:
total 0
-rw-r--r-- 1 abc abc 0 2011-05-23 10:20 file1
-rw-r--r-- 1 abc abc 0 2011-05-23 10:20 file2
But I get an error when I do
~/bashpractice$ xargs -0 ls -l
dir1 dir3
ls: cannot access dir1 dir3
: No such file or directory
abc@us-sjc1-922l:~/bashpractice$ xargs -0 ls -l
dir1
dir3
ls: cannot access dir1
dir3
: No such file or directory
How to get a listing when specifying -0 option to xargs ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例如 - 如
man xargs
中所述-0
告诉 xargs 一件事:“不要用空格分隔输入,而是用 NULL 字符分隔输入”。当您需要处理名称中包含空格的文件和/或目录时,它通常与 find 结合使用很有用。还有更多命令可以使用
-print0
- 例如grep -z
。编辑(基于评论)
请参阅Seth的答案或此:
但很奇怪,如果不需要使用它,为什么要使用
-0
?。就像“为什么要删除文件,如果不存在?”。简而言之,如果输入是空填充的,则仅需要与组合一起使用-0
。时期。 :)For example - as told in the
man xargs
The
-0
tells xargs one thing: "Don't separate input with spaces but with NULL char". It is useful usually in combination with find, when you need handle files and/or directories that containspace
in their name.There are more commands what can play with
-print0
- for examplegrep -z
.Edit (based on comments)
See Seth's answer or this:
But it is strange, why want use
-0
if you don't need to use it?. Like "Why want remove a file, if does not exist?". Simply, the-0
is needed to use only with combination, if the input is null-padded. Period. :)xargs 的工作方式与您想象的不同。它接受输入并运行作为参数提供的命令以及从输入中读取的数据。例如:
如果您怀疑输入中可能嵌入了空格或回车符,则经常使用 -0,因此默认参数分隔符“\s”(正则表达式 \s、空格、制表符、换行符)不好。
xargs works differently than you think. It takes input and runs the commands provided as arguments with the data it reads from the input. For example:
You often use -0 if you suspect the input might have spaces or returns embedded in it, so the default argument delimiter of "\s" (regular expression \s, space, tab, newline) isn't good.