Skip to content

Instantly share code, notes, and snippets.

@turbotobias
Created February 7, 2023 05:48
Show Gist options
  • Save turbotobias/61c8a74d3a8c42369fe4394ee8590df5 to your computer and use it in GitHub Desktop.
Save turbotobias/61c8a74d3a8c42369fe4394ee8590df5 to your computer and use it in GitHub Desktop.
Delete all node_modules from sub-folders in a directory
#!/bin/bash
# Here is a shell script that deletes the node_modules folder from each folder found in the current working directory:
# Deletes one level down, no recursion
# Run this to allow deletion:
# $ chmod +x./delete_node_modules.sh
# Run this to delete:
# $ ./delete_node_modules.sh
for d in */; do
# Check if the directory contains a node_modules folder
if [ -d "$d/node_modules" ]; then
# Delete the node_modules folder
rm -rf "$d/node_modules"
# Print a message indicating that the folder was deleted
echo "Deleted node_modules in $d"
fi
done
@turbotobias
Copy link
Author

If you have some folders you do not want to delete node_modules from, you can use this:

#!/bin/bash

excluded=("folder-with-secrets" "folder-with-artefacts")

# Find all directories in the current working directory
for d in */; do
  # Get the current directory name without the trailing "/"
  dir_name=${d%/}
  # Check if the directory name is not in the excluded array
  if [[ ! " ${excluded[@]} " =~ " ${dir_name} " ]]; then
    # Check if the directory contains a node_modules folder
    if [ -d "$d/node_modules" ]; then
      # Delete the node_modules folder
      rm -rf "$d/node_modules"
      # Print a message indicating that the folder was deleted
      echo "Deleted node_modules in $d"
    fi
  fi
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment