Created
August 26, 2024 05:48
-
-
Save DerekBuntin/c4f1f615ff20e009e947619667c9bd55 to your computer and use it in GitHub Desktop.
Find and remove folders recursively - Terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# One Liner | |
find . -type d \( -name "thumbs" -o -name "list" \) -exec rm -rf {} + | |
# Array | |
#!/bin/bash | |
# Define an array of directory names to be removed | |
dirs_to_remove=("thumbs" "list") | |
# Build the find expression from the array | |
find_expr="" | |
for dir in "${dirs_to_remove[@]}"; do | |
if [ -n "$find_expr" ]; then | |
find_expr="${find_expr} -o -name ${dir}" | |
else | |
find_expr="-name ${dir}" | |
fi | |
done | |
# Execute the find command | |
find . -type d \( ${find_expr} \) -exec rm -rf {} + |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One Liner Version:
Steps to Run the Command:
cd ~/Desktop/Images
find . -type d \( -name "thumbs" -o -name "list" \) -exec rm -rf {} +
Example Session:
Here's how you can conduct the operation:
Navigate to the Images directory
cd ~/Desktop/Images
Run the command to delete all 'thumbs' and 'list' directories from the current location downwards
find . -type d \( -name "thumbs" -o -name "list" \) -exec rm -rf {} +
Important Considerations:
Be Cautious: The rm -rf command will delete directories and their contents without prompting for confirmation. Make sure you're in the correct directory and you're okay with deleting the specified folders.
Backup Important Data: If the directories contain important data, ensure you have backups before running the command.
This command will ensure that all directories named thumbs and list are removed from the current directory and all its subdirectories.
Array Version:
Explanation:
Steps to Implement: Create the Shell Script (.sh) File:
cd ~/Desktop
chmod +x remove_directories.sh
./remove_directories.sh
Example Session in Terminal:
Navigate to the Desktop
cd ~/Desktop
Create the script file
nano remove_directories.sh
Make the script executable
chmod +x remove_directories.sh
Run the script
./remove_directories.sh
Happy Coding :-)