Skip to content

Instantly share code, notes, and snippets.

@DerekBuntin
Created August 26, 2024 05:48
Show Gist options
  • Save DerekBuntin/c4f1f615ff20e009e947619667c9bd55 to your computer and use it in GitHub Desktop.
Save DerekBuntin/c4f1f615ff20e009e947619667c9bd55 to your computer and use it in GitHub Desktop.
Find and remove folders recursively - Terminal
# 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 {} +
@DerekBuntin
Copy link
Author

One Liner Version:

Steps to Run the Command:

  1. Open Terminal: Open the Terminal application on your macOS.
  2. Navigate to the Desired Parent Directory: Use the cd command to navigate to the directory where you want to start the search. For example, if you want to begin from the Images directory on your Desktop: cd ~/Desktop/Images
  3. Run the Removal Command: 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:

  1. Define the Array: dirs_to_remove contains the list of directory names you want to remove.
  2. Build the find Expression Dynamically:
    • Loop Through the Array: The script loops through each element in the array and constructs the find_expr string.
    • Concatenate with -o -name: Each directory is concatenated with the -o -name option to build the complete expression.
  3. Execute the find Command: The dynamically constructed expression (${find_expr}) is used within the find command.

Steps to Implement: Create the Shell Script (.sh) File:

  1. Open Terminal.
  2. Navigate to a Directory: cd ~/Desktop
  3. Create the Script File: nano remove_directories.sh
  4. Paste the Script: Copy the modified script and paste it into nano. In nano, you can paste by right-clicking or using Command + V.
  5. Save and Exit: In nano, save the file with Control + O, press Enter to confirm, then exit nano with Control + X.
  6. Make the Script Executable: chmod +x remove_directories.sh
  7. Run the Script: ./remove_directories.sh

Example Session in Terminal:

Navigate to the Desktop
cd ~/Desktop

Create the script file
nano remove_directories.sh

  • Paste the script content into nano - (Use Command + V to paste, or right-click)
  • Save and exit nano (Control + O, Enter, then Control + X)

Make the script executable
chmod +x remove_directories.sh

Run the script
./remove_directories.sh

Happy Coding :-)

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