Remove node_modules recursively
Posted at:
Updated:
Save your space, remove your node_modules now!
Here is syntax to remove node_module recursively at root of folder:
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
Or you can save this code below at your root folder as execute/shell script.remove_all_node_moodules.sh
#!/bin/bash
# Set directory base
DIRECTORY_BASE="$(pwd)"
# Find and delete all node_modules at current directories
find "$DIRECTORY_BASE" -name "node_modules" -type d -prune -exec rm -rf "{}" +
Explaination:
find
: This is the command used to search for files and directories within a specified directory hierarchy.
.
: This represents the starting directory for the search. In this case, it's the current directory (denoted by .).
-name 'node_modules'
: This specifies the name of the directory to search for. In this case, it's looking for directories named "node_modules".
-type d
: This flag tells find to only consider directories.
-prune
: This option tells find to not descend into directories that match the specified criteria. So, when it finds a directory named "node_modules", it won't search within that directory.
-exec rm -rf '{}' +
: This is the action to take for each found directory. It executes the rm -rf
command on the found directories. rm
is the command to remove files or directories, -rf
are options to recursively force removal without prompting for confirmation, and {}
represents the placeholder for the found directories. The +
at the end is used to indicate the end of the command to execute.
#!/bin/bash
: Specifies line that the script should be run with the Bash shell.
DIRECTORY_BASE="$(pwd)"
: This sets the variable DIRECTORY_BASE to the current working directory (as output by the pwd command).
Original article: stackoveflow