can pass path in arg, added -y for direct deletion

This commit is contained in:
2024-11-01 18:39:43 +01:00
parent 8ea758be21
commit fc6c427784

View File

@@ -1,7 +1,21 @@
#!/bin/bash #!/bin/bash
# Ask the user for the path to the folder # Initialize variables
read -p "Enter the path to the folder: " folder_path auto_confirm=false
# Parse arguments for the folder path and the -y option
while [[ "$#" -gt 0 ]]; do
case "$1" in
-y) auto_confirm=true ;;
*) folder_path="$1" ;;
esac
shift
done
# If no folder path is provided, prompt the user for it
if [ -z "$folder_path" ]; then
read -p "Enter the path to the folder: " folder_path
fi
# Check if the provided path is valid # Check if the provided path is valid
if [ ! -d "$folder_path" ]; then if [ ! -d "$folder_path" ]; then
@@ -21,19 +35,21 @@ else
echo "$trash_folders" echo "$trash_folders"
echo echo
# Ask for confirmation before deletion # Ask for confirmation before deletion if -y is not provided
read -p "Do you really want to delete all these folders? (y/n): " confirmation if [ "$auto_confirm" = false ]; then
read -p "Do you really want to delete all these folders? (y/n): " confirmation
if [[ "$confirmation" == "y" || "$confirmation" == "Y" ]]; then if [[ "$confirmation" != "y" && "$confirmation" != "Y" ]]; then
# Loop through each found folder and delete it while displaying progress echo "Deletion canceled."
while IFS= read -r folder; do exit 0
echo "Deleting folder: $folder" fi
rm -rf "$folder"
echo "Deleted $folder"
done <<< "$trash_folders"
echo "All .Trash-1000 folders have been deleted."
else
echo "Deletion canceled."
fi fi
# Loop through each found folder and delete it
while IFS= read -r folder; do
echo "Deleting folder: $folder"
rm -rf "$folder"
echo "Deleted $folder"
done <<< "$trash_folders"
echo "All .Trash-1000 folders have been deleted."
fi fi