以递归方式快速查找并打印文件夹中的所有文件,不包括`node_modules`和`.git`中的文件 [英] Quickly find and print all files in a folder, recursively, excluding ones from `node_modules` and `.git`
问题描述
/ root /
/root/.git $ b $ 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 $ c
因此,遍历 node_modules
并过滤出来是不可接受的,因为这需要太多的时间。我希望命令永远不要进入 node_modules
或 .git
文件夹。
仅用于直接从搜索文件夹中排除文件:
<$ p $找到> 。 -not \(-path'./.git'-prune \)-not \(-path'./node_modules'-prune \)-type f
如果您想排除子文件夹中的某些路径,您可以使用 *
通配符。
$ b $ node_modules 在
stuff1
和 stuff2
,还有 dist
和 lib
文件夹:
find。 -not \(-path'./.git'-prune \)-not \(-path'./node_modules'-prune \)-not \(-path'./*/node_modules' - 修剪\)不是\(-path'./*/dist'-prune \)-not \(-path'./*/lib'-prune \)-type f
在Windows上使用git bash测试1.9.5
但是,如果传递像 -name'* .js'
这样的过滤器,则它在Windows上无法正常工作。解决方法是不要使用 -name
和pipe到 grep
。
Consider the following folder structure starting in some root folder
/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/
...
Now I am in /root
and I'd like to find all my own files inside it.
I have a small number of my own files and the huge number of files inside node_modules
and .git
.
Because of that, traversing node_modules
and filtering it out is unacceptable, as it takes too much time. I want the command to never enter the node_modules
or .git
folder.
For excluding files only directly from the folder of the search:
find . -not \( -path './.git' -prune \) -not \( -path './node_modules' -prune \) -type f
If you want to exclude certain paths that are in subfolders, you can do that too using *
wildcard.
Say you have node_modules
too inside stuff1
and stuff2
, and additionally dist
and lib
folders:
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
Tested on Windows using git bash 1.9.5
It seems however it does not work properly on Windows when a filter like -name '*.js'
is passed. The workaround could be to not use -name
and pipe to grep
instead.
Kudos to @Daniel C. Sobral
这篇关于以递归方式快速查找并打印文件夹中的所有文件,不包括`node_modules`和`.git`中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!