config find 命令排除某些目录

jia*_*ian 2 bash

如何配置以永久排除find命令的某些目录。 /sf/ask/294702971/ 我尝试将以下别名添加到 bashrc

alias find='find -not \( -path ./.git -prune \)'
Run Code Online (Sandbox Code Playgroud)

似乎不起作用。

在 ripgrep 中你可以配置它。https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md#configuration-file 那么如何一次完成所有配置 make find 排除某些目录(如 git)。

Sté*_*las 5

我会将其编写为myfind包装脚本,例如:

\n
#! /bin/sh -\npredicate_found=false skip_one=false\nfor arg do\n  if "$skip_one"; then\n    skip_one=false\n  elif ! "$predicate_found"; then\n    case $arg in\n      (-depth | -ignore_readdir_race | -noignore_readdir_race | \\\n       -mount | -xdev | -noleaf | -show-control-chars) ;;\n      (-files0-from | -maxdepth | -mindepth) skip_one=true;;\n      (['()!'] | -[[:lower:]]?*)\n        predicate_found=true\n        set -- "$@" ! '(' -name .git -prune ')' '('\n    esac\n  fi\n  set -- "$@" "$arg"\n  shift\ndone\nif "$predicate_found"; then\n  set -- "$@" ')'\nelse\n  set -- "$@" ! '(' -name .git -prune ')'\nfi\nexec find "$@"\n
Run Code Online (Sandbox Code Playgroud)\n

! ( -name .git -prune )在第一个非选项谓词\xc2\xb9 之前插入(如果未找到谓词,则在末尾插入),并将其余部分包装在 和 之间,()避免使用-o.

\n

例如,myfind -L . /tmp /etc -maxdepth 1 -type d -o -print会变成find -L . /tmp /etc -maxdepth 1 ! '(' -name .git -prune ')' '(' -type d -o -print ')'.

\n

prune就是所有 .git目录。要仅修剪作为参数传递的每个目录的深度为 1 的目录,使用 FreeBSD 的,您可以在 before 中find添加一个.-depth 1-name .git

\n

.git如果添加一些-mindepth 2(或任何大于 2 的数字),目录最终可能会被遍历。

\n

请注意不能与(或暗示)-prune结合使用。-depth-delete-depth

\n
\n

\xc2\xb9 这里负责处理 GNUfind 选项谓词,以避免在您在它们之前插入内容时出现警告。它为此使用启发式方法。如果你使用 BSD 的话,它可能会被愚弄myfind -f -my-dir-starting-with-dash-...

\n