#!/bin/sh # Simple trash bin - moves files to ~/.local/share/Trash instead of deleting # Usage: trash [file2] ... # trash -l list trashed files # trash -e empty trash permanently # trash -r restore file to original location TRASH_DIR="$HOME/.local/share/Trash" TRASH_FILES="$TRASH_DIR/files" TRASH_INFO="$TRASH_DIR/info" mkdir -p "$TRASH_FILES" "$TRASH_INFO" case "$1" in -l|--list) if [ -z "$(ls -A "$TRASH_FILES" 2>/dev/null)" ]; then echo "trash is empty" exit 0 fi echo "TRASHED FILES:" for info in "$TRASH_INFO"/*.trashinfo; do [ -f "$info" ] || continue name=$(basename "$info" .trashinfo) origin=$(grep "^Path=" "$info" | cut -d= -f2-) date=$(grep "^DeletionDate=" "$info" | cut -d= -f2-) printf " %-40s %s %s\n" "$name" "$date" "$origin" done echo du -sh "$TRASH_FILES" | awk '{print "Total: " $1}' ;; -e|--empty) if [ -z "$(ls -A "$TRASH_FILES" 2>/dev/null)" ]; then echo "trash is already empty" exit 0 fi size=$(du -sh "$TRASH_FILES" | awk '{print $1}') printf "permanently delete all trashed files (%s)? [y/N] " "$size" read -r ans case "$ans" in y|Y) rm -rf "$TRASH_FILES"/* "$TRASH_INFO"/*; echo "trash emptied" ;; *) echo "aborted" ;; esac ;; -r|--restore) shift [ -z "$1" ] && echo "usage: trash -r " && exit 1 info="$TRASH_INFO/$1.trashinfo" file="$TRASH_FILES/$1" if [ ! -f "$info" ] || [ ! -e "$file" ]; then echo "not found in trash: $1" exit 1 fi origin=$(grep "^Path=" "$info" | cut -d= -f2-) if [ -e "$origin" ]; then echo "destination already exists: $origin" exit 1 fi mkdir -p "$(dirname "$origin")" mv "$file" "$origin" && rm "$info" echo "restored: $origin" ;; -h|--help) echo "usage: trash move files to trash" echo " trash -l list trashed files" echo " trash -e empty trash" echo " trash -r restore file" ;; *) [ -z "$1" ] && echo "usage: trash " && exit 1 for f in "$@"; do if [ ! -e "$f" ]; then echo "not found: $f" continue fi fullpath=$(readlink -f "$f") name=$(basename "$f") # handle name collisions dest="$name" n=1 while [ -e "$TRASH_FILES/$dest" ]; do dest="${name}.$n" n=$((n + 1)) done mv "$f" "$TRASH_FILES/$dest" cat > "$TRASH_INFO/$dest.trashinfo" <