0

我尝试使用findand对目录中的所有子目录运行命令-exec,但是在其中一个目录上,运行脚本的用户没有足够的权限,并且出现错误(权限被拒绝)。我试图忽略使用其中一个! -path或使用的目录-prune。这些方法都不起作用。我已经尝试了下面的两个命令。

我尝试了每一种组合subDirToExclude——./开头有和没有/*,最后有和没有。我已经尝试了相对路径、完整路径以及您可以想到的所有它们的每一个组合来尝试匹配此路径,但它根本不起作用。手册页没有帮助,并且该论坛上任何相关问题的建议都不会产生任何有用的结果。为什么手册页中建议的方法都不起作用?这实际上是如何做到的?

find /path/to/dir -maxdepth 1 -type d ! -path "subDirToExclude" -exec somecommand {} +
find /path/to/dir -maxdepth 1 -type d -path "subDirToExclude" -prune -o -exec somecommand {} +
find: ‘/path/to/dir/subDirToExclude’: Permission denied
4

1 回答 1

1

The argument to the -path option should be a full pathname, not just the name of the directory. Use -name if you just want to match the name of the directory.

find /path/to/dir -maxdepth 1 -type d ! -name "subDirToExclude" -exec somecommand {} +

You could also do this without using find at all, since you're not recursing into subdirectories because of -maxdepth 1.

shopt -s extglob
somecommand /path/to/dir /path/to/dir/!(subDirToExclude)/

Putting / at the end of the filename makes the wildcard only match directories. Actually, this will also match symbolic links to directories; if that's a problem, you can't use this solution.

于 2019-08-15T21:06:19.920 回答