Joh*_*ohn 3 command-line bash find
我正在使用find . -type f -mtime 0终端中的功能搜索在最后一天编辑过的文件。我想在查询中排除一些文件和文件夹,例如.DS_Store文件和.sh文件。
目前我正在这样做:
find . -type f -not -path "*.DS_Store" -and -not -path "*.sh" -mtime 0
我还有很多要排除的文件,我想知道是否可以缩短表达式。我不想写:
-not -path "PathHere" -and -not -path "AnotherPathHere" 等等。
有什么建议?
-and无论如何都是多余的,因此您可以简单地删除它,并且!可以代替非标准-not运算符使用。您可以使用 bash 数组列出每行一个排除项。不会更短,但更容易阅读和编辑。
filters=(
! -name '*.DS_Store'
! -name '*.sh'
! -name '*.bash'
)
find . -type f \( "${filters[@]}" \) -print
Run Code Online (Sandbox Code Playgroud)
扩展上述内容也可以避免使用-prune以下命令进入某些目录:
filters=(
! -name '*.DS_Store'
! -name '*.sh'
! -name '*.bash'
)
prune_dirs=(
-name '*.tmp'
-o -name 'tmp'
-o -name '.Trash*'
)
find . -type d \( "${prune_dirs[@]}" \) -prune -o -type f \( "${filters[@]}" \) -print
Run Code Online (Sandbox Code Playgroud)