find 和有什么区别。并找到。-打印

far*_*oft 34 command-line find

有什么区别:

find .
Run Code Online (Sandbox Code Playgroud)

和

find . -print
Run Code Online (Sandbox Code Playgroud)

什么是-print真正做到?

$ find .
.
./hello.txt
./hello
./hello/txt
./hello/hello2
./hello/hello2/hello3
./hello/hello2/hello3/txt
./hello/hello2/txt
$ find . -print
.
./hello.txt
./hello
./hello/txt
./hello/hello2
./hello/hello2/hello3
./hello/hello2/hello3/txt
./hello/hello2/txt
Run Code Online (Sandbox Code Playgroud)

Ste*_*itt 45

从findutils find联机帮助页:

如果没有给出表达式,-print则使用该表达式(但-print0无论如何您应该考虑使用)。

(-print是一个find表达式。)

该POSIX文件证实了这一点:

如果不存在表达式,则应使用-print作为表达式。

所以find .完全等同于find . -print; 第一个没有表达式,所以-print在内部添加。

-print在手册页中进一步解释了什么:

-print

真的; 在标准输出上打印完整的文件名,后跟换行符。如果您将 find 的输出传送到另一个程序中,并且您正在搜索的文件可能包含换行符的可能性很小,那么您应该认真考虑使用该-print0选项而不是-print. 有关如何处理文件名中的异常字符的信息,请参阅 UNUSUAL FILENAMES 部分。

  • @Rishi 参见 [手册页](http://man7.org/linux/man-pages/man1/find.1.html#EXPRESSION)。 (2认同)

Sté*_*las 22

-print是默认操作。某些find谓词被视为操作,而不是过滤器或条件。例如,-type f不是一个动作。-exec是一个动作,即使它也可以用作条件。

操作包括-print、-exec和-ok。某些find实现具有其他非标准动作谓词,例如-print0, -printf, -execdir, -okdir, -ls...

find files <some-predicates>
Run Code Online (Sandbox Code Playgroud)

如果没有<some-predicates>包含操作等同于:

find files \( <some-predicates> \) -print
Run Code Online (Sandbox Code Playgroud)

(请注意上面的括号,如果有一些-o运算符,它们很重要)。

如有疑问,最好是-print明确使用(或-exec printf '%s\0' {} +(或-print0在可用的情况下),以便对输出进行后处理)。

默认-print操作由 POSIX 指定。一些旧的find实现需要一个显式的-print,但现在通常找不到。

另请注意,某些find实现允许省略files,在这种情况下,它们默认搜索当前目录。也就是说,对他们来说,

find
find -print
Run Code Online (Sandbox Code Playgroud)

相当于

find .
find . -print
Run Code Online (Sandbox Code Playgroud)

然而,这不是标准的,所以最好避免。

在更详细(和有用)的范围内,一些find实现还允许将文件路径作为参数传递给-f选项,如下所示:

find -f "$file1" -f "$file2" -print
Run Code Online (Sandbox Code Playgroud)

它们是唯一find允许将任意文件路径传递给find. 其他实现不能接受像!or -print... 这样的文件路径,所以find "$file" -print(甚至find -- "$file" -print)假定$file不是find谓词的名称(或第一种情况下的选项)。

不幸的是,这既不标准也不便携。


cuo*_*glm 9

它们是相同的,它们都从当前目录写出整个目录层次结构。

从POSIX 找到文档:

以下命令是等效的:

找 。

找 。-打印