i need a shell script that recursively find all the nodejs project in the current directory and remove node_modules folder. it should avoid traversing into the node_modules folder
Assistant
You can create a simple bash script to accomplish this task. Here's an example script which does exactly that. It usesfindcommand to recursively look forpackage.jsonfiles (which should be present in every project). Then for each directory containing apackage.jsonfile, it will try to remove thenode_modulesdirectory if it exists.
User
find command finds package.json inside of node_modules as well. nothing from node_modules should be listed by find
Assistant
I apologize for misunderstanding your requirement earlier. Here is an updated version of the script which excludes thenode_modulesdirectories. This script uses the-pruneoption of thefindcommand, which preventsfindfrom descending into directories that match the specified pattern.
find . -name 'package.json' -print0 | while IFS= read -r -d '' file; do
dir=(dirname"file")
node_modules_dir="${dir}/node_modules"
code
if [ -d "$node_modules_dir" ]; then
echo "Removing ${node_modules_dir}"
rm -rf "$node_modules_dir"
fi
done
Here's how this script works:
find . -name 'package.json' -print0- This will find all files namedpackage.jsonfrom the current directory recursively. The-print0option is used to handle file paths that contain spaces or other special characters.
while IFS= read -r -d '' file; do- This reads each file path, preserving whitespace and other special characters.
dir=(dirname"file")- This gets the directory of the currentpackage.jsonfile.
node_modules_dir="${dir}/node_modules"- This creates the path to thenode_modulesdirectory in the current project.
if [ -d "$node_modules_dir" ]; then- This checks if thenode_modulesdirectory exists.
rm -rf "$node_modules_dir"- If thenode_modulesdirectory exists, this line deletes it.
done- This ends thewhileloop.
find . -name 'package.json' -print0- This will find all files namedpackage.jsonfrom the current directory recursively. The-print0option is used to handle file paths that contain spaces or other special characters.
while IFS= read -r -d '' file; do- This reads each file path, preserving whitespace and other special characters.
dir=(dirname"file")- This gets the directory of the currentpackage.jsonfile.
node_modules_dir="${dir}/node_modules"- This creates the path to thenode_modulesdirectory in the current project.
if [ -d "$node_modules_dir" ]; then- This checks if thenode_modulesdirectory exists.
rm -rf "$node_modules_dir"- If thenode_modulesdirectory exists, this line deletes it.
done- This ends thewhileloop.
Please, make sure you have the right permissions to execute the script, you can give it execution permissions with the commandchmod +x scriptname.sh.
Note: Be careful while running scripts like this, make sure to double-check your directories as this script will delete directories without confirmation.
(
d
i
r
nam
e
"
code
if [ -d "$node_modules_dir" ]; then
echo "Removing ${node_modules_dir}"
rm -rf "$node_modules_dir"
fi
done
Here's what's new in this script:
-name 'node_modules' -prune -o- This tellsfindto avoid directories namednode_modules.
-name 'package.json' -print0- This tellsfindto search for files namedpackage.json. The-print0option is used to handle file paths that contain spaces or other special characters.
-name 'node_modules' -prune -o- This tellsfindto avoid directories namednode_modules.
-name 'package.json' -print0- This tellsfindto search for files namedpackage.json. The-print0option is used to handle file paths that contain spaces or other special characters.
Please be aware, this script could still potentially be destructive if used improperly. Always double check the directories you are working with and always maintain backups when running scripts that delete files or directories.