将 find 命令与 -perm 和 -maxdepth 一起使用

Moh*_*ghi 0 linux permissions find

当我输入此命令时:

$ find . -perm 777 -maxdepth 1
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

find: warning: you have specified the -maxdepth option after a non-option argument -perm, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

这意味着什么?

tha*_*guy 5

参数的顺序find非常重要,因为它们被评估为从左到右短路的布尔表达式:

# Deletes *.tmp files
find . -name '*.tmp' -delete

# Deletes ALL file, because -delete is performed before -name
find . -delete -name '*.tmp'
Run Code Online (Sandbox Code Playgroud)

然而,-maxdepth行为却并非如此。-maxdepth是一个改变find工作方式的选项,因此无论它放在哪里它都适用相同的:

# Deletes all '*.tmp' files, but only in the current dir, not subdirs
find . -maxdepth 1 -name '*.tmp' -delete  

# Deletes all '*.tmp' files, still only in the current dir
find . -name '*.tmp' -delete -maxdepth 1 
Run Code Online (Sandbox Code Playgroud)

由于您将 放在-maxdepth 1a 之后-perm 777,看起来您正试图-maxdepth仅适用于某些文件。由于这是不可能的,因此find打印此警告。

它建议您将其重写find . -maxdepth 1 -perm 777以明确您打算-maxdepth应用于所有内容。