在手动指定输入时,如何对xargs使用-0选项?

Ank*_*wal 6 bash xargs

我有

~/bashpractice$ ls
dir3 dir1   
Run Code Online (Sandbox Code Playgroud)

我明白了

~/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
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我收到了一个错误

~/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
Run Code Online (Sandbox Code Playgroud)

如何在为xargs指定-0选项时获取列表?

jm6*_*666 11

例如 - 如在中所述 man xargs

-0将xargs更改为期望NUL(``\ 0'')字符作为分隔符,而不是空格和换行符.预计这将与find(1)中的-print0函数一起使用.

找 .-print0 | xargs -0 echo

-0告诉xargs的一两件事."不要用空格分隔输入,而是使用NULL char".当您需要处理space名称中包含的文件和/或目录时,它通常与find结合使用很有用.

有更多的命令可以使用-print0 - 例如grep -z.

编辑 - 基于评论:

请参见塞思的答案或:

ls -1 | perl -pe 's/\n/\0/;' > null_padded_file.bin
xargs -0 < null_padded_file.bin
Run Code Online (Sandbox Code Playgroud)

但是是一个奇怪的,为什么要使用-0如果你不需要使用它?.比如"为什么要删除文件,如果不存在?".简单地说,如果输入为空填充,则-0只需要与组合一起使用.期.:)