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
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 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,11 +35,16 @@ else
echo "$trash_folders" echo "$trash_folders"
echo echo
# Ask for confirmation before deletion # Ask for confirmation before deletion if -y is not provided
if [ "$auto_confirm" = false ]; then
read -p "Do you really want to delete all these folders? (y/n): " confirmation read -p "Do you really want to delete all these folders? (y/n): " confirmation
if [[ "$confirmation" != "y" && "$confirmation" != "Y" ]]; then
echo "Deletion canceled."
exit 0
fi
fi
if [[ "$confirmation" == "y" || "$confirmation" == "Y" ]]; then # Loop through each found folder and delete it
# Loop through each found folder and delete it while displaying progress
while IFS= read -r folder; do while IFS= read -r folder; do
echo "Deleting folder: $folder" echo "Deleting folder: $folder"
rm -rf "$folder" rm -rf "$folder"
@@ -33,7 +52,4 @@ else
done <<< "$trash_folders" done <<< "$trash_folders"
echo "All .Trash-1000 folders have been deleted." echo "All .Trash-1000 folders have been deleted."
else
echo "Deletion canceled."
fi
fi fi