以递归方式快速查找并打印文件夹中的所有文件,不包括`node_modules`和`.git`中的文件 [英] Quickly find and print all files in a folder, recursively, excluding ones from `node_modules` and `.git`

查看:318
本文介绍了以递归方式快速查找并打印文件夹中的所有文件,不包括`node_modules`和`.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
因此,遍历 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



赞誉 @Daniel C. Sobral


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆