为什么 cat 不能在 xargs 中使用参数 -0?
Why cat does not work with parameter -0 in xargs?
我正在尝试实施此解决方案:
Make xargs handle filenames that contain spaces
到cat
几个要使用find
选择的文件。
因此,我已尝试实施 post 中提供的 BSD 解决方案,但如果我这样做:
$ find "/tmp/database" -name "FOO 03-11*"
/tmp/database/01/FOO 03-11.txt
/tmp/database/03/FOO 03-11.txt
/tmp/database/02/FOO 03-11.txt
/tmp/database/05/FOO 03-11.txt
/tmp/database/04/FOO 03-11.txt
$ find "/tmp/database" -name "FOO 03-11*" | sort
/tmp/database/01/FOO 03-11.txt
/tmp/database/02/FOO 03-11.txt
/tmp/database/03/FOO 03-11.txt
/tmp/database/04/FOO 03-11.txt
/tmp/database/05/FOO 03-11.txt
$ find "/tmp/database" -name "FOO 03-11*" | sort | xargs -0
/tmp/database/01/FOO 03-11.txt
/tmp/database/02/FOO 03-11.txt
/tmp/database/03/FOO 03-11.txt
/tmp/database/04/FOO 03-11.txt
/tmp/database/05/FOO 03-11.txt
$ find "/tmp/database" -name "FOO 03-11*" | sort | xargs -0 cat
cat: /tmp/database/01/FOO 03-11.txt
/tmp/database/02/FOO 03-11.txt
/tmp/database/03/FOO 03-11.txt
/tmp/database/04/FOO 03-11.txt
/tmp/database/05/FOO 03-11.txt
: No such file or directory
$
我仍然无法使 cat
命令正常工作。为了使 cat
与 xargs -0
一起工作,所提供的 post 的公认答案中缺少什么?
我正在使用 FreeBSD 12 及其 shell /bin/sh
据我所知是 POSIX.
我会 - 显然错误地 - 期望这个 find/sort/xargs/cat
序列输出文件的内容,如:
$ cat "/tmp/database/01/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/01/FOO 03-11.txt
$ cat "/tmp/database/02/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/02/FOO 03-11.txt
$ cat "/tmp/database/03/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/03/FOO 03-11.txt
$ cat "/tmp/database/04/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/04/FOO 03-11.txt
$ cat "/tmp/database/05/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/05/FOO 03-11.txt
所以预期的输出是:
$ find "/tmp/database" -name "FOO 03-11*" | sort | xargs -0 cat
CONTENT OF FILE /tmp/database/01/FOO 03-11.txt
CONTENT OF FILE /tmp/database/02/FOO 03-11.txt
CONTENT OF FILE /tmp/database/03/FOO 03-11.txt
CONTENT OF FILE /tmp/database/04/FOO 03-11.txt
CONTENT OF FILE /tmp/database/05/FOO 03-11.txt
FreeBSD xargs
不支持已删除答案中建议的 -d
,但答案很有用,因为它阐明了 -0
用法并给出了处理换行符的提示作为分隔符,所以包含 tr
将 \n
变成 [=17=]
可以为 FreeBSD 解决问题:
$ find "/tmp/database" -name "FOO 03-11*" | sort | tr '\n' '[=10=]' | xargs -0 cat
CONTENT OF FILE /tmp/database/01/FOO 03-11.txt
CONTENT OF FILE /tmp/database/02/FOO 03-11.txt
CONTENT OF FILE /tmp/database/03/FOO 03-11.txt
CONTENT OF FILE /tmp/database/04/FOO 03-11.txt
CONTENT OF FILE /tmp/database/05/FOO 03-11.txt
作为旁注,在 GNU Linux 中可以使用 -d '\n'
:
find "/tmp/database" -name "FOO 03-11*" | sort | xargs -d '\n' cat
我正在尝试实施此解决方案:
Make xargs handle filenames that contain spaces
到cat
几个要使用find
选择的文件。
因此,我已尝试实施 post 中提供的 BSD 解决方案,但如果我这样做:
$ find "/tmp/database" -name "FOO 03-11*"
/tmp/database/01/FOO 03-11.txt
/tmp/database/03/FOO 03-11.txt
/tmp/database/02/FOO 03-11.txt
/tmp/database/05/FOO 03-11.txt
/tmp/database/04/FOO 03-11.txt
$ find "/tmp/database" -name "FOO 03-11*" | sort
/tmp/database/01/FOO 03-11.txt
/tmp/database/02/FOO 03-11.txt
/tmp/database/03/FOO 03-11.txt
/tmp/database/04/FOO 03-11.txt
/tmp/database/05/FOO 03-11.txt
$ find "/tmp/database" -name "FOO 03-11*" | sort | xargs -0
/tmp/database/01/FOO 03-11.txt
/tmp/database/02/FOO 03-11.txt
/tmp/database/03/FOO 03-11.txt
/tmp/database/04/FOO 03-11.txt
/tmp/database/05/FOO 03-11.txt
$ find "/tmp/database" -name "FOO 03-11*" | sort | xargs -0 cat
cat: /tmp/database/01/FOO 03-11.txt
/tmp/database/02/FOO 03-11.txt
/tmp/database/03/FOO 03-11.txt
/tmp/database/04/FOO 03-11.txt
/tmp/database/05/FOO 03-11.txt
: No such file or directory
$
我仍然无法使 cat
命令正常工作。为了使 cat
与 xargs -0
一起工作,所提供的 post 的公认答案中缺少什么?
我正在使用 FreeBSD 12 及其 shell /bin/sh
据我所知是 POSIX.
我会 - 显然错误地 - 期望这个 find/sort/xargs/cat
序列输出文件的内容,如:
$ cat "/tmp/database/01/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/01/FOO 03-11.txt
$ cat "/tmp/database/02/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/02/FOO 03-11.txt
$ cat "/tmp/database/03/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/03/FOO 03-11.txt
$ cat "/tmp/database/04/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/04/FOO 03-11.txt
$ cat "/tmp/database/05/FOO 03-11.txt"
CONTENT OF FILE /tmp/database/05/FOO 03-11.txt
所以预期的输出是:
$ find "/tmp/database" -name "FOO 03-11*" | sort | xargs -0 cat
CONTENT OF FILE /tmp/database/01/FOO 03-11.txt
CONTENT OF FILE /tmp/database/02/FOO 03-11.txt
CONTENT OF FILE /tmp/database/03/FOO 03-11.txt
CONTENT OF FILE /tmp/database/04/FOO 03-11.txt
CONTENT OF FILE /tmp/database/05/FOO 03-11.txt
FreeBSD xargs
不支持已删除答案中建议的 -d
,但答案很有用,因为它阐明了 -0
用法并给出了处理换行符的提示作为分隔符,所以包含 tr
将 \n
变成 [=17=]
可以为 FreeBSD 解决问题:
$ find "/tmp/database" -name "FOO 03-11*" | sort | tr '\n' '[=10=]' | xargs -0 cat
CONTENT OF FILE /tmp/database/01/FOO 03-11.txt
CONTENT OF FILE /tmp/database/02/FOO 03-11.txt
CONTENT OF FILE /tmp/database/03/FOO 03-11.txt
CONTENT OF FILE /tmp/database/04/FOO 03-11.txt
CONTENT OF FILE /tmp/database/05/FOO 03-11.txt
作为旁注,在 GNU Linux 中可以使用 -d '\n'
:
find "/tmp/database" -name "FOO 03-11*" | sort | xargs -d '\n' cat