Skip to content

Instantly share code, notes, and snippets.

@datavudeja
Forked from jvarelaaloisio/Readme.md
Created May 31, 2026 22:14
Show Gist options
  • Select an option

  • Save datavudeja/574a7b931db21abefeb81553dd11c9c4 to your computer and use it in GitHub Desktop.

Select an option

Save datavudeja/574a7b931db21abefeb81553dd11c9c4 to your computer and use it in GitHub Desktop.
Remove directories based on name. Really useful for temporary folders, like "Library" in the unity engine.

Summary of the arguments for the script:

Script Arguments Summary

  • -f, --foldername: Specifies the name of the folder to search and remove (FolderName).

  • -d or --directory: Specifies a custom directory path from the command line.

  • -p, --permanent: Optional flag that indicates whether to permanently remove the folders (Permanent).

  • -y, --yes: Optional flag that indicates yes to all prompts (YesToAll).

  • -s, --showflags: Optional flag that displays the current settings of Permanent and YesToAll (ShowFlags).

Usage Examples

  • To remove all folders named "Library" in the current directory permanently, confirming each removal:

    ./RemoveAllSubfoldersWithName.sh -f "Library" -p
    
  • To remove all folders named "Library" in the current directory, confirming each removal:

    ./RemoveAllSubfoldersWithName.sh -f "Library"
    
  • To remove all folders named "Library" in the specified directory, showing current settings:

    ./RemoveAllSubfoldersWithName.sh -f "Library" -s
    
  • To remove all folders named "Library" in the specified directory permanently without prompts:

    ./RemoveAllSubfoldersWithName.sh -f "Library" -py
    

This summary outlines the main arguments and their usage patterns for the script. Adjust the paths and folder names as needed when executing the script in your environment.

# Script to remove directories with a specific name
# Author: Juan Pablo Varela Aloisio
# Email: juampyvarela@gmail.com
param (
[string]$FolderName = "",
[Alias("directory")][string]$Path = "$(Get-Location)",
[Alias("p")][switch]$Permanent,
[Alias("y")][switch]$YesToAll,
[switch]$ShowFlags
)
# Search for directories
function Find-Folders {
param (
[string]$Path,
[string]$FolderName
)
Get-ChildItem -Path $Path -Directory -Recurse -Force | Where-Object { $_.Name -eq $FolderName } | ForEach-Object {
$_.FullName
}
}
function Remove-Folder {
param (
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$false)]
[switch]$Permanent
)
if ($Permanent) {
Remove-Item -Path $Path -Recurse -Force
} else {
Remove-Item -Path $Path -Recurse
}
}
# Prompt for FolderName if not provided
if ([string]::IsNullOrWhiteSpace($FolderName)) {
$FolderName = Read-Host "Please enter the folder name"
}
# Output log file path
$logFileName = "${FolderName}_DirectoriesRemoval.log"
$logFilePath = Join-Path -Path $Path -ChildPath $logFileName
# Ensure the directory for the log file exists, create it if necessary
$logDirectory = Split-Path -Path $logFilePath -Parent
if (-not (Test-Path -Path $Path -PathType Container)) {
Write-Host -ForegroundColor Red "Error: Directory '$Path' does not exist."
return
}
if($ShowFlags) {
Write-Host -ForegroundColor Yellow "Yes to all: $YesToAll"
Write-Host -ForegroundColor Yellow "Permanent removal: $Permanent"
}
# Find folders
$folders = Find-Folders -Path $Path -FolderName $FolderName
# Initialize log content
$logContent = @()
if ($folders.Count -eq 0) {
$logContent += "No folders with name $FolderName were found at path $Path"
$logContent
return
}
# Remove all folders
if($YesToAll) {
foreach ($folder in $folders) {
Remove-Folder -Path $folder -Permanent:$Permanent
$logContent += "removed folder $folder"
}
}
# Ask for aproval on every folder
else {
foreach ($folder in $folders) {
$response = Read-Host "Do you want to remove the folder $folder ? (y/n)"
# Use the method
if ($response -match '^(?i)yes$|^(?i)y$') {
Remove-Folder -Path $folder -Permanent:$Permanent
$logContent += "removed folder $folder"
} else {
$logContent += "kept folder $folder"
}
}
}
Write-Host -ForegroundColor Green "Done."
Write-Host -ForegroundColor DarkGray "Log file: $logFilePath"
# Write log to file
$logContent | Out-File -FilePath $logFilePath
#!/bin/bash
# Script to remove directories with a specific name
# Author: Juan Pablo Varela Aloisio
# Email: juampyvarela@gmail.com
# Default values
FolderName=""
Path=$(pwd)
Permanent=false
YesToAll=false
ShowFlags=false
# Search for directories
find_folders() {
local path=$1
local folder_name=$2
find "$path" -type d -name "$folder_name"
}
# Remove a folder
remove_folder() {
local path=$1
local permanent=$2
if [ "$permanent" = true ]; then
rm -rf "$path"
else
rm -r "$path"
fi
}
# Parse arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-f|--foldername)
FolderName="$2"
shift # past argument
shift # past value
;;
-p|--permanent)
Permanent=true
shift # past argument
;;
-y|--yes)
YesToAll=true
shift # past argument
;;
-s|--showflags)
ShowFlags=true
shift # past argument
;;
-d|--directory)
Path="$2"
shift # past argument
shift # past value
;;
-[a-zA-Z]*)
# Process each character in the combined short option
chars="${key:1}"
for (( i=0; i<${#chars}; i++ )); do
case "${chars:$i:1}" in
p) Permanent=true ;;
y) YesToAll=true ;;
s) ShowFlags=true ;;
d) Path="$2"; shift ;;
*)
echo "Invalid option: -${chars:$i:1}" >&2
exit 1
;;
esac
done
shift # past combined short option
;;
*) # unknown option
echo "Invalid option: $1" >&2
exit 1
;;
esac
done
# Prompt for FolderName if not provided
if [ -z "$FolderName" ]; then
read -p "Please enter the folder name: " FolderName
fi
# Output log file path
logFileName="${FolderName}_DirectoriesRemoval.log"
logFilePath="$Path/$logFileName"
# Ensure the directory for the log file exists
logDirectory=$(dirname "$logFilePath")
if [ ! -d "$Path" ]; then
echo "Error: Directory '$Path' does not exist."
exit 1
fi
# Show flags if requested
if [ $ShowFlags = true ]; then
echo "Yes to all: $YesToAll"
echo "Permanent removal: $Permanent"
fi
# Find folders
folders=$(find_folders "$Path" "$FolderName")
# Initialize log content
logContent=()
if [ -z "$folders" ]; then
logContent+=("No folders with name $FolderName were found at path $Path")
echo "${logContent[@]}" | tee "$logFilePath"
exit 0
fi
# Remove all folders
if [ "$YesToAll" = true ]; then
for folder in $folders; do
remove_folder "$folder" "$Permanent"
logContent+=("removed folder $folder")
done
else
for folder in $folders; do
read -p "Do you want to remove the folder $folder? (y/n): " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
remove_folder "$folder" "$Permanent"
logContent+=("removed folder $folder")
else
logContent+=("kept folder $folder")
fi
done
fi
# Write log to file
echo "Done."
echo "Log file: $logFilePath"
printf "%s\n" "${logContent[@]}" > "$logFilePath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment