Delete node_modules folder recursively from a specified path using command line

后端 未结 7 1886
暖寄归人
暖寄归人 2021-01-29 18:26

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 a

相关标签:
7条回答
  • 2021-01-29 18:41

    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:

    find . -name node_modules -type d -prune -exec trash {} +
    
    0 讨论(0)
  • 2021-01-29 18:42

    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 '{}' \;
    

    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.

    npx npkill
    
    0 讨论(0)
  • 2021-01-29 18:44

    Try https://github.com/voidcosmos/npkill

    npx npkill
    

    it will find all node_modules and let you remove them.

    0 讨论(0)
  • 2021-01-29 18:55

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

    0 讨论(0)
  • 2021-01-29 18:56

    bash function to remove node_modules. It will remove all node_modules directories recursively from the current working directory, while printing found paths.

    You just need to put in somewhere in your $PATH

    rmnodemodules(){
    
      find . -name 'node_modules' -type d -prune -exec echo '{}' \; -exec rm -rf {} \; 
    
    }
    
    0 讨论(0)
  • 2021-01-29 18:58

    This works really well

    find . -name "node_modules" -exec rm -rf '{}' +
    
    0 讨论(0)
提交回复
热议问题