Delete node_modules folder recursively from a spec

2019-03-08 05:20发布

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.

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.

4条回答
不美不萌又怎样
2楼-- · 2019-03-08 05:52

This works really well

find . -name "node_modules" -exec rm -rf '{}' +
查看更多
Fickle 薄情
3楼-- · 2019-03-08 06:02

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 '{}' +

run the following command to delete folders recursively

find /path -type d -name "node_modules" -exec rm -rf '{}' +

查看更多
Deceive 欺骗
4楼-- · 2019-03-08 06:05

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.

Note: You must first cd into the root directory and then run the command or instead of find . use 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 '{}' \;
查看更多
在下西门庆
5楼-- · 2019-03-08 06:13

Original answer:

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

Alternatively you can use trash (npm install --global trash-cli) for save deletion:

find . -name node_modules -type d -prune -exec trash {} +
查看更多
登录 后发表回答