trash (2009B)
1 #!/bin/sh 2 TRASHDIR="$HOME/.Trash" 3 [ -d "$TRASHDIR" ] || mkdir -p "$TRASHDIR" 4 5 printfq() { printf "'%s'\n" "$(printf '%s' "$1" | sed -e "s/'/'\\''/g")"; } 6 7 PARAMS="" 8 9 trash_empty() { 10 printf "Empty the trash? [y/n] " 11 stty raw && conf="$(dd bs=1 count=1 2>/dev/null)" && stty -raw 12 printf "\n" 13 14 case "$conf" in 15 Y|y) 16 find "${TRASHDIR:?}" -delete 17 printf "Deleted all items in the trash.\n" 18 ;; 19 *) 20 # code otherwise 21 printf "Cancelled.\n" 22 ;; 23 esac 24 } 25 26 trash_empty_secure() { 27 printf "Securely empty the trash? [y/n] " 28 stty raw && conf="$(dd bs=1 count=1 2>/dev/null)" && stty -raw 29 printf "\n" 30 31 case "$conf" in 32 Y|y) 33 command -v shred >/dev/null 2>&1 || { printf "Please install shred (1) to empty trash securely.\n" >&2 && exit 1; } 34 find "${TRASHDIR:?}" -type f -exec shred -uz {} + 35 find "${TRASHDIR:?}" -delete 36 printf "Shredded all items in the trash.\n" 37 ;; 38 *) 39 printf "Cancelled.\n" 40 ;; 41 esac 42 } 43 44 trash_usage() { 45 printf "%s\n" "$(du -sh "$TRASHDIR")" 46 } 47 48 while [ $(($#)) -ne 0 ]; do 49 case "$1" in 50 -e|--empty) 51 trash_empty && exit 0 52 ;; 53 -s|--secure-empty) 54 trash_empty_secure && exit 0 55 ;; 56 -u|--usage) 57 trash_usage && exit 0 58 ;; 59 -h|--help) 60 printf "Usage:\n" 61 printf "trash -e|--empty\t\tempty the trash\n" 62 printf "trash -u|--usage\t\tprints disk usage info\n" 63 printf "trash arg1 [arg2...]\t\tmove items to the trash\n" 64 exit 0 65 ;; 66 --) # end arg parsing 67 shift 68 break 69 ;; 70 -*) # unsupported flags 71 echo "Unsupported flag $1" >&2 72 exit 1 73 ;; 74 *) # preserve positional arguments 75 PARAMS="$PARAMS $(printfq "$1")" 76 shift 77 ;; 78 esac 79 done 80 eval set -- "$PARAMS" 81 82 for f in "$@"; do 83 destname="${f##*/}" 84 ctr=0; while [ -e "$TRASHDIR/$destname" ]; do destname="${f##*/}.$ctr"; ctr=$((ctr+1)); done 85 [ -e "$f" ] && mv "$f" "${TRASHDIR:?}/$destname" && printf "Trashed %s\n" "$f" 86 done