Bash snippet : Recursively delete node_modules, vendor, and .git folder from multiple laravel projects using .sh file

Published

Usefull for recursively delete node_modules, vendor folders from multiple laravel projects, at once.

#!/bin/bash
# This is a recursive delete script for multiple laravel projects 
# @arguments:
#
# Deletes the folders inside the folder where this script.sh file is placed
# Example : If you have many laravel projects inside /var/www/, create script.sh with this contents and run from terminal, sudo bash script.sh
#
# @install: 
# $ chmod +x script.sh
# $ sudo bash script.sh
#
for D in ./*; do
    if [ -d "$D" ]; then
        cd "$D"
            for X in ./*; do
                if [ -d "$X" ]; then
                    #uncomment below line if you want to delete all .zip files in the projects root directory
                    #find . -name "*.zip" -type f | xargs rm -rf   
                    #uncomment below line if you want to delete .git folder in the projects root directory                                     
                    if [ "$X" == "./node_modules" ] || [ "$X" == "./vendor" ]; then 
                        echo "match found";
                        rm -rf "$X"
                        echo "match deleted";
                    else 
                        echo "not a match";
                    fi
                fi
            done
        cd ..
    fi
done
# script.sh;

Usage : create script.sh file in directory, where all projects are in. In terminal, run, sudo bash script.sh

Comments

Post a Comment