#!/bin/bash
archivedir="$HOME/.deleted-files"
realrm="$(which rm)"
move="$(which mv)"
dest=$(pwd)
if [ ! -d $archivedir ] ; then
echo "$0: No deleted files directory: nothing to unrm" >&2
exit
fi
cd $archivedir
if [ $# -eq 0 ] ; then
echo "Contents of your deleted files archive (sorted by date):"
ls -FC | sed -e 's/\([[:digit:]][[:digit:]]\.\)\{5\}//g' -e 's/^/ /'
fi
matches="$(ls -d *"$1" 2> /dev/null | wc -l)"
if [ $matches -eq 0 ] ; then
echo "No match for \"$1\" in the deleted file archive." >&2
exit 1
fi
if [ $matches -gt 1 ] ; then
echo "More than one file or directory match in the archive:"
index=1
for name in $(ls -td *"$1")
do
datetime="$(echo $name | cut -c1-14| awk -F . '{print $5"/"$4" at "$3":"$2":"$1}')"
filename="$(echo $name | cut -c16-)"
if [ -d $name ] ; then
filecount="$(ls $name | wc -l | sed 's/[^[:digit:]]//g')"
echo "$index) $filename (contents = ${filecount} items, deleted = $datetime)"
else
size="$(ls -sdk1 $name | awk '{print $1}')"
echo "$index) $filename (size = ${size}Kb, deleted = $datetime)"
fi
index=$(($index+1))
done
echo ""
echo -n "Which version of $1 do you want to restore ('0' to quit)? [1] : "
read desired
if [ ! -z "$(echo $desired | sed 's/[[:digit:]]//g')" ] ; then
echo "$0: Restore canceled by user: invalid input." >&2
exit 1
fi
if [ ${desired:=1} -ge $index ] ; then
echo "$0: Restore canceled by user: index value too big." >&2
exit 1
fi
if [ $desired -lt 1 ] ; then
echo "$0: restore canceled by user." >&2
fi
restore="$(ls -td1 *"$1" | sed -n "${desired}p")"
if [ -e "$dest/$1" ] ; then
echo "\"$1\" already exists in this directory. Cannot overwrite." >&2
exit
fi
echo -n "Restoring file \"$1\" ..."
$move "$restore" "$dest/$1"
echo "done."
echo -n "Delete the additional copies of this file? [y] "
read answer
if [ ${answer:=y} = "y" ] ; then
$realrm -rf *"$1"
echo "deleted."
else
echo "additional copies retained."
fi
else
if [ -e "$dest/$1" ] ; then
echo "\"$1\" already exists in this directory. Cannot overwrite." >&2
exit 1
fi
restore="$(ls -d *"$1")"
echo -n "Restoring file \"$1\" ..."
$move "$restore" "$dest/$1"
echo "done."
fi
exit 0
unrm
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Rebecca Saxe :On Reading Minds | TED Talk Video 1: On Rea...