find 在一个命令中组合多个名称和路径

DEK*_*KER 1 find regular-expression

我有一组相当复杂的文件需要找到并做出反应(例如复制到文本文件的路径):

例如:

 find / \( -iname "*ssh*" -or -iname "*scp*" \) \( -not -path -"/run" -not -path "/rw/*" -and -not -path "/home/*" -and -not -path "*/qml/*" \) >> ~/files.txt
Run Code Online (Sandbox Code Playgroud)

在这里我想找到与“ssh”和“scp”相关但不在 /run 或 /rw 目录中的所有文件或文件夹。

我将为此添加更多条件,但命令太长了。我怎样才能用正则表达式做到这一点?

Sté*_*las 5

最好是根本不要下降到您想要排除的那些目录中,而不是在事后使用以下命令过滤掉其中的文件! -path <pattern>

LC_ALL=C find / \
       '(' \
          -path /run -o  -path /rw -o -path /home -o -path '*/qml' \
       ')' -prune -o '(' \
          -name '*[sS][hH][hH]*' -o -name '*[sS][cC][pP]*' \
       ')' -print
Run Code Online (Sandbox Code Playgroud)

这里使用POSIXfind语法。对于 GNU find,这可能是:

LC_ALL=C find / -regextype posix-extended \
  -regex '/home|/rw|.*/qml' -prune -o \
  -iregex '.*s(cp|sh)[^/]*' -print
Run Code Online (Sandbox Code Playgroud)

使用 BSD find,您只需使用-E类似 ingrepsed即可获取 POSIX ERE:

LC_ALL=C find -E / \
  -regex '/home|/rw|.*/qml' -prune -o \
  -iregex '.*s(cp|sh)[^/]*' -print
Run Code Online (Sandbox Code Playgroud)