為什麼明確添加 -print 時 find(1) 的行為會有所不同?

為什麼明確添加 -print 時 find(1) 的行為會有所不同?

的手冊頁find(1)說:

如果表達式不包含 以外的任何操作-prune-print則對表達式為 true 的所有檔案執行。

然而,這兩個表達之間似乎存在差異:

$ find . -path '*fo*' -prune -o -type f -print
./bar/xzyzzy
$ find . -path '*fo*' -prune -o -type f
./foo
./bar/xzyzzy

為什麼./foo包含在後者的輸出中?

我使用以下命令建立了上述範例目錄樹結構:

$ cd $(mktemp -d)
$ mkdir foo
$ mkdir bar
$ touch foo/quux
$ touch bar/xzyzzy

對我來說的輸出find --version是:

find (GNU findutils) 4.4.2`

答案1

哦,我想我已經自己解決了...

在前一種情況下,-print執行僅有的-path '*fo*'當由於 的短路行為而導致第一個條件 ( ) 不成立時-o

然而,在第二種情況下,-print所有的-path '*fo*'表達式為真 - 當為真時就是這種情況或者 -type f是真的。換句話說,我的問題中的兩個命令都相當於:

$ find . \( -path '*fo*' -prune \) -o \( -type f -print \)
./bar/xzyzzy

... 和:

$ find . \( -path '*fo*' -prune -o -type f \) -print
./foo
./bar/xzyzzy

我想,無論如何:)

相關內容