#!/bin/bash
#                       /usr/local/bin/unrm
#  http://crystalfaeries.net/posix/bin/unrm
# celeste:crystalfaery 2016-04-01 20:37:48+00:00
# 2009-05 by Kyle Rankin of Linux Journal who started from:
# http://forums.gentoo.org/viewtopic-t-365703.html

UNRM="~/documents/unrm"	# directory to receive unremoved files
mkdir -p ~/$UNRM || exit -1	# make the recipient directory

DISK=$1		# disk partition to scan for removed files
if [ "Z$DISK" == "Z" ]
then
	echo "Usage: $0 <partition>"
	echo "e.g.: $0 /dev/sdz9"
	echo "Ensure adequate free space in $UNRM directory."
else

sudo fls -f ext -d -r -p "$DISK" | while read line
do
	filetype=$(echo "$line" | awk {'print $1'})
	filenode=$(echo "$line" | awk {'print $3'})
	filenode=${filenode%:}
	filenode=${filenode%(*}
	filename=$(echo "$line" | cut -f 2)
	echo "unrm: $filename"
	if [ $filetype == "d/d" ]; then
		mkdir -p "$UNRM/$filename"
	else
		mkdir -p "$UNRM/$(dirname $filename)"
		icat -f ext -r -s "$DISK" "$filenode" > "$UNRM/$filename"
	fi
done
fi


syntax highlighted by Code2HTML, v. 0.9.1