手动指定输入时,如何在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
在xargs中指定-0选项时如何获取清单?     

解决方法

例如-如
man xargs
所述   -0更改xargs以期望NUL(``\\ 0 \'\')   字符作为分隔符,而不是   空格和换行符。这是预期的   与-print0一起使用   在find(1)中起作用。 找 。 -print0 | xargs -0回声 -0告诉xargs一件事。 \“不要用空格分隔输入,但使用NULL char \”。当需要处理名称中包含“ 4”的文件和/或目录时,通常与find结合使用是很有用的。 还有更多可与-print0一起使用的命令,例如grep -z。 编辑-根据评论: 请参阅塞思的答案或此:
ls -1 | perl -pe \'s/\\n/\\0/;\' > null_padded_file.bin
xargs -0 < null_padded_file.bin
但是很奇怪,如果不需要,为什么要使用-0?就像\“为什么要删除文件,如果不存在?”。简而言之,如果输入为空值,则-0只需要与组合使用。期。 :)     ,xargs的工作方式与您想象的不同。它接受输入,并使用从输入中读取的数据运行作为参数提供的命令。例如:
find dir* -type f -print0 | xargs -0 ls -l

ls -d dir* | xargs \'-d\\n\' ls -l

look foo | xargs echo

look foo | perl -pe \'s/\\n/\\0/;\' | xargs -0 echo
如果您怀疑输入中可能包含空格或返回值,则通常使用-0,因此默认的参数定界符\“ \\ s \”(正则表达式\\ s,space,tab,换行符)不好。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...