快速查找并打印文件夹中的所有文件,递归地排除来自“node_modules”和“.git”的文件

Quickly find and print all files in a folder, recursively, excluding ones from `node_modules` and `.git`

考虑从某个根文件夹开始的以下文件夹结构

/root/
/root/.git
/root/node_modules
/root/A/
/root/A/stuff1/
/root/A/stuff2/
/root/A/node_modules/
/root/B/
/root/A/stuff1/
/root/A/stuff2/
/root/B/node_modules/
...

现在我在 /root 中,我想在其中找到我自己的所有文件。 我有少量自己的文件,大量文件在 node_modules.git.

因此,遍历node_modules并过滤掉它是不可接受的,因为它需要太多时间。我希望命令 从不进入 node_modules.git 文件夹

仅直接从搜索文件夹中排除文件:

find . -not \( -path './.git' -prune \) -not \( -path './node_modules' -prune \) -type f

如果您想排除 在子文件夹 中的某些路径,您也可以使用 * 通配符来做到这一点。

假设您在 stuff1stuff2 中也有 node_modules,另外还有 distlib 文件夹:

find . -not \( -path './.git' -prune \) -not \( -path './node_modules' -prune \) -not \( -path './*/node_modules' -prune \) -not \( -path './*/dist' -prune \)  -not \( -path './*/lib' -prune \) -type f

使用 git bash 1.9.5

在 Windows 上测试

不过,当通过 -name '*.js' 之类的过滤器时,它似乎无法在 Windows 上正常工作。解决方法可能是不使用 -name 而是通过管道传输到 grep

感谢@Daniel C. Sobral