bash 使用命令行从指定路径递归删除 node_modules 文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42950501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete node_modules folder recursively from a specified path using command line
提问by Sumit
I have multiple npm projects saved in a local directory. Now I want to take backup of my projects without the node_modules
folder, as it is taking a lot of space and can also be retrieved any time using npm install
.
我在本地目录中保存了多个 npm 项目。现在我想在没有node_modules
文件夹的情况下备份我的项目,因为它占用了大量空间并且也可以随时使用npm install
.
So, I need a solution to delete all node_modules folders recursively from a specified path using the command line interface. Any suggestions/ help is highly appreciable.
因此,我需要一个解决方案来使用命令行界面从指定路径递归删除所有 node_modules 文件夹。任何建议/帮助都非常值得赞赏。
回答by Darius M.
Print out a list of directories to be deleted:
打印出要删除的目录列表:
find . -name 'node_modules' -type d -prune
Delete directories from the current working directory:
从当前工作目录中删除目录:
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
Alternatively you can use trash(brew install trash
) for staged deletion:
或者,您可以使用垃圾( brew install trash
) 进行分阶段删除:
find . -name node_modules -type d -prune -exec trash {} +
回答by Sidharth
Improving on the accepted answer,
改进已接受的答案,
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
I found that the command would run a very long time to fetch all folders and then run a delete command, to make the command resumable I'd suggest using \;
and to see progress of the command being run use -print
to see the directory being deleted.
我发现该命令将运行很长时间来获取所有文件夹,然后运行删除命令,以使命令可恢复我建议使用\;
并查看正在运行的命令的进度,-print
以查看正在删除的目录。
Note:You must first cd
into the root directory and then run the command or instead of find .
use find {project_directory}
注意:必须先cd
进入根目录再运行命令或代替find .
使用find {project_directory}
To delete folders one by one
一一删除文件夹
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' \;
To delete folders one by one and printing the folder being deleted
一一删除文件夹并打印被删除的文件夹
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
Edit:
编辑:
For the people who like interactive way of doing this refer to @jeckep answer, run this in the directory that you wish to prune.
对于喜欢以交互方式执行此操作的人,请参阅 @jeckep 答案,在您希望修剪的目录中运行它。
npx npkill
回答by jeckep
Try https://github.com/voidcosmos/npkill
试试 https://github.com/voidcosmos/npkill
npx npkill
it will find all node_modules and let you remove them.
它将找到所有 node_modules 并让您删除它们。
回答by Sumit
I have come across with this solution,
我遇到了这个解决方案,
- first find the folder using
find
and specify name of the folder. - execute delete command recursively
-exec rm -rf '{}' +
- 首先找到使用的文件夹
find
并指定文件夹的名称。 - 递归执行删除命令
-exec rm -rf '{}' +
run the following command to delete folders recursively
运行以下命令递归删除文件夹
find /path -type d -name "node_modules" -exec rm -rf '{}' +
find /path -type d -name "node_modules" -exec rm -rf '{}' +
回答by Ankit Sinha
This works really well
这真的很好用
find . -name "node_modules" -exec rm -rf '{}' +
回答by Ivan V.
bash
function to remove node_modules
. It will remove all node_modules
directories recursively from the current working directory,
while printing found paths.
bash
功能删除node_modules
。它将node_modules
从当前工作目录中递归删除所有目录,同时打印找到的路径。
You just need to put in somewhere in your $PATH
你只需要在你的某个地方 $PATH
rmnodemodules(){
find . -name 'node_modules' -type d -prune -exec echo '{}' \; -exec rm -rf {} \;
}