`find | 的不同输出xargs ls` 在不同的 Ubuntu 版本上使用相同的命令

`find | 的不同输出xargs ls` 在不同的 Ubuntu 版本上使用相同的命令

我有两个 Ubuntu 安装:

A:14.04.3 LTS (GNU/Linux 3.13.0-77-generic x86_64)

乙:14.04.2 LTS (GNU/Linux 3.13.0-61-generic x86_64)

我正在运行以下命令:

find . -name "nosuch.file" -print0 | xargs -0 ls

(真正的命令更复杂,但我设法将其范围缩小到这个)。nosuch.file当前目录或子目录中没有文件。

在服务器 A 上,该命令按照我的预期产生空输出。

在服务器 B 上,该命令生成当前目录(我在其中运行该命令)的列表。

在两台服务器上运行只会find . -name "nosuch.file" -print0产生空输出。

在两台服务器上安装了相同版本的find,xargsls

$ find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)

$ xargs --version
xargs (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b

$ ls --version
ls (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

为什么命令会有不同的行为?我该如何实现与服务器 A 上一致的行为(未找到文件则为空输出)?

答案1

“我该怎么做才能实现与服务器 A 上一致的行为(未找到文件则为空输出)?”

要使用 实现此行为xargs,您可以使用-r/--no-run-if-empty选项,以便在没有向其传递文件名的情况下不会运行指定的命令。来自手册页:

如果标准输入不包含任何非空白,则不要运行该命令。通常,即使没有输入,该命令也会运行一次。

请注意,此选项是 GNU 的一项功能xargs,不是标准的,即由 POSIX 指定的。

相关内容