#!/bin/bash # unrm--Searches the deleted files archive for the specified file or # directory. If there is more than one matching result, it shows a list # of results ordered by timestamp and lets the user specify # which one to restore. setglobal archivedir = ""$HOME/.deleted-files"" setglobal realrm = $[which rm] setglobal move = $[which mv] setglobal dest = $[pwd] if test ! -d $archivedir { echo "$0: No deleted files directory: nothing to unrm" > !2 ; exit 1 } cd $archivedir # If given no arguments, just show a listing of the deleted files if test $Argc -eq 0 { echo "Contents of your deleted files archive (sorted by date):" ls -FC | sed -e 's/\([[:digit:]][[:digit:]]\.\)\{5\}//g' \ -e 's/^/ /' exit 0 } # Otherwise, we must have a user-specified pattern to work with. # Let's see if the pattern matches more than one file or directory # in the archive. setglobal matches = $[ls -d *"$1" !2 > /dev/null | wc -l] if test $matches -eq 0 { echo "No match for \"$1\" in the deleted file archive." > !2 exit 1 } if test $matches -gt 1 { echo "More than one file or directory match in the archive:" setglobal index = '1' for name in [$[ls -td *"$1]] { setglobal datetime = $[echo $name | cut -c1-14| \ awk -F. '{ print $5"/"$4" at "$3":"$2":"$1 }] setglobal filename = $[echo $name | cut -c16-] if test -d $name { setglobal filecount = $[ls $name | wc -l | sed 's/[^[:digit:]]//g] echo " $index) $filename (contents = $(filecount) items," ' ' " deleted = $datetime)" } else { setglobal size = $[ls -sdk1 $name | awk '{print $1}] echo " $index) $filename (size = $(size)Kb, deleted = $datetime)" } setglobal index = $shExpr(' $index + 1') } echo "" echo -n "Which version of $1 do you want to restore ('0' to quit)? [1] : " read desired if test ! -z $[echo $desired | sed 's/[[:digit:]]//g] { echo "$0: Restore canceled by user: invalid input." > !2 exit 1 } if test $(desired:=1) -ge $index { echo "$0: Restore canceled by user: index value too big." > !2 exit 1 } if test $desired -lt 1 { echo "$0: restore canceled by user." > !2 ; exit 1 } setglobal restore = $[ls -td1 *"$1" | sed -n "$(desired)p] if test -e "$dest/$1" { echo "\"$1\" already exists in this directory. Cannot overwrite." > !2 exit 1 } echo -n "Restoring file \"$1\" ..." $move $restore "$dest/$1" echo "done." echo -n "Delete the additional copies of this file? [y] " read answer if test $(answer:=y) = "y" { $realrm -rf *"$1" echo "deleted." } else { echo "additional copies retained." } } else { if test -e "$dest/$1" { echo "\"$1\" already exists in this directory. Cannot overwrite." > !2 exit 1 } setglobal restore = $[ls -d *"$1] echo -n "Restoring file \"$1\" ... " $move $restore "$dest/$1" echo "done." } exit 0