Skip to content

Instantly share code, notes, and snippets.

@naranyala
Last active August 8, 2023 03:10
Show Gist options
  • Save naranyala/31ff982ac254e2223151e4495787f73e to your computer and use it in GitHub Desktop.
Save naranyala/31ff982ac254e2223151e4495787f73e to your computer and use it in GitHub Desktop.
this script can delete directory or file that utilize `trash-cli` capabalities to move into "trash", this script also has confirmation prompt to delete and clear message state
#!/bin/bash
# Function to print usage information
print_usage() {
echo "Usage: $0 <file_path>"
}
# Check if an argument was provided
if [ $# -eq 0 ]; then
print_usage
exit 1
fi
# Get the file or directory path from the first argument
filepath="$1"
# Check if the file or directory exists
if [ ! -e "$filepath" ]; then
echo "Error: File or directory does not exist: $filepath"
exit 1
fi
# Check if trash-cli is available
trash_command=""
if command -v trash-put &>/dev/null; then
trash_command="trash-put"
elif command -v trash-cli &>/dev/null; then
trash_command="trash-cli"
fi
# Function to remove file or directory using trash-cli
remove_with_trash_cli() {
"$trash_command" "$1"
echo "File or directory '$1' moved to trash."
}
# Function to remove file or directory permanently
remove_permanently() {
if [ -d "$1" ]; then
# Remove directory recursively
rm -r "$1"
echo "Directory '$1' has been permanently removed."
else
# Remove the file
rm "$1"
echo "File '$1' has been permanently removed."
fi
}
# Prompt for confirmation before removing the file or directory
if [ -n "$trash_command" ]; then
# trash-cli is available, use it to move to trash
read -p "Are you sure you want to move '$filepath' to trash? (y/n): " confirmation
if [ "$confirmation" = "y" ] || [ "$confirmation" = "Y" ]; then
remove_with_trash_cli "$filepath"
else
echo "Removal of '$filepath' canceled."
fi
else
# trash-cli not available, prompt for permanent removal
echo -e "trash-cli is not available."
read -p "Are you sure you want to remove '$filepath' permanently? (y/n): " confirmation
if [ "$confirmation" = "y" ] || [ "$confirmation" = "Y" ]; then
remove_permanently "$filepath"
else
echo "Removal of '$filepath' canceled."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment