shell编程之实现windows回收站功能分享
一. 功能简介
1. 将删除的文件放在回收站中
2. 恢复删除的文件
3. 实现linux rm命令的功能, 使用起来几乎和linux 系统自带的rm ,命令完全一样
4. 新增功能: rm -l, rm -e, rm -c
5. 该脚本每次在运行时候会检查$home/.trash 目录下文件大小之和, 若
超过最大容量, 脚本会自动将日志文件中所记录文件中的前一半文件从回
收站中清除,所以建议删除大文件(相对于回收站最大容量而言)直接用
命令/bin/rm 而不要用 rm.
二. 使用方法:
1. 将trash文件放到 $home/bin/
2. 在$home/.bashrc 文件中加入alias rm=”$home/bin/trash”, 重新登陆终端或执行bash命令。
3. 执行命令rm -e 配置回收站的最大容量,单位k
4. 回收站的默认目录为:$home/.trash, 默认配置文件为:$home/.trash/trash.conf
默认log文件为:$home/.trash/trash.log
5. 怎样恢复文件:
在linux 终端中输入rm -l, 然后 在rownumber: 后面键入要删除文件所在的行标识:988 键入y/y 然后按回车键 恢复成功.
如果想只查看删除列表, 则键入rm -l 后直接按回车键或者键入q/q
6. 更详细的参数介绍请键入:rm --help
三. 注意事项
1. 想要手动清空$home/.trash目录需要用/bin/rm命令, 请不要尝试用rm -r $home/.trash 的方法.
2. 该脚本不支持rm -r -f , rm -rfi (选项组合超过2个)格式.
3. 如果你可以你甚至可以用该脚本作为备份脚本, 假若想备份test2.txt你只需要执行rm test2.txt, 当然如果真想备份某个文件的话, 最好编写专门的备份脚本。
#!/bin/bash
#配置回收站最大的存储空间(字节)
#maxmemory=51200 (50m)
#maxmemory=102400 (100m)
#maxmemory=512000 (500m)
#根据情况设置为50m(对于isoa服务开发来说足够了)
maxmemory=3145728
#设置回收站所在的目录
trash=$home/.trash
#设置日志文件所在的目录
mvlog=$trash/trash.log
from1=$1
from2=$2
var_pwd=
var_father=
#回收站若不存在,则新建之
if [ ! -e $trash ];then
mkdir -p $trash
chmod 755 $trash
fi
#产生7位的随机数
function rand()
{
a=(0 1 2 3 4 5 6 7 8 9 a b c d e a b c d e f)
for ((i=0;i<7;i++))
do
echo -n ${a[$random%${#a[*]}]}
done
}
random=$(rand)
#文件不存在时的提示信息
function file_null()
{
local file=$1
echo "rm: cannot remove '$file': no such file or directory"
}
#打印参数出错后的提示信息
function echo_msg()
{
echo -n "rm: missing operand
try 'rm --help' for more information.
"
}
function echo_msg2()
{
echo -n "rm: invalid option '$1'
try 'rm --help' for more information.
"
}
#回收站管理函数
function deal()
{
local tmp=$(mktemp /tmp/tfile.xxxxxx)
local num=$(($(cat $mvlog| wc -l)/2))
#awk -f: -v nu=$num -v trash=$trash '{if (nr<=nu) system("rm -rf "trash"'/'"$2"':'"$3""); \
#else print $0}' $mvlog | sort -o $mvlog
awk -f: -v nu=$num -v trash=$trash '{if (nr<=nu) system("rm -rf "trash"'/'"$2"':'"$3""); \
else print $0}' $mvlog >> $tmp
mv $tmp $mvlog
}
jug=
#目录处理函数
function jug_cur()
{
local tmp=
local dirname=$1
local jug=${dirname/\/*/}
if [ "$jug" == "." ];then
var_pwd=${dirname/./$(pwd)}
jug=0
elif [ "$jug" == ".." ];then
tm=$(pwd)
tmp=${tm%/*}
var_father=${dirname/../$tmp}
jug=1
#elif [ "$jug" == "~" ];then
#return 2
else
jug=2
fi
}
#命令不带参数时的普通文件删除函数
function rm1
{
local filename=$(basename $from1)
local dirname=$(dirname $from1)
jug_cur $dirname
if [ "$jug" -eq 0 ];then
dirname=$var_pwd
elif [ $jug -eq 1 ];then
dirname=$var_father
fi
if [ -d "$from1" ];then
echo "rm: cannot remove '$from1': is a directory"
else
if [ ! -e $from1 ];then
file_null $from1
else
echo "$dirname:$filename:$random:$(date +%y-%m-%d.%t)" >> $mvlog
mv "$from1" "$trash/$filename:$random"
fi
fi
}
#rm -i
function rmi()
{
local filename=$(basename $from2)
local dirname=$(dirname $from2)
jug_cur $dirname
if [ $jug -eq 0 ];then
dirname=$var_pwd
elif [ $jug -eq 1 ];then
dirname=$var_father
fi
if [ -f "$from2" ];then
echo -n "rm: remove regular file '$from2'?"
read answer
if [ "$answer" = 'y' -o "$answer" = 'y' ];then
echo "$dirname:$filename:$random:$(date +%y-%m-%d.%t)" >> $mvlog
mv "$from2" "$trash/$filename:$random"
fi
else
if [ ! -e $from2 ];then
file_null $from2
else
echo "rm: cannot remove '$from2': is a directory"
fi
fi
}
#rm -f
function rmf()
{
local filename=$(basename $from2)
local dirname=$(dirname $from2)
jug_cur $dirname
if [ $jug -eq 0 ];then
dirname=$var_pwd
elif [ $jug -eq 1 ];then
dirname=$var_father
fi
if [ -f "$from2" ];then
echo "$dirname:$filename:$random:$(date +%y-%m-%d.%t)" >> $mvlog
mv "$from2" "$trash/$filename:$random"
else
if [ ! -e $from2 ];then
:
else
echo "rm: cannot remove '$from2': is a directory"
fi
fi
}
#rm -r
function rmr()
{
local filename=$(basename $from2)
local dirname=$(dirname $from2)
jug_cur $dirname
if [ $jug -eq 0 ];then
dirname=$var_pwd
elif [ $jug -eq 1 ];then
dirname=$var_father
fi
if [ "$from2" = "." -o "$from2" = ".." ];then
echo "rm: cannot remove directory: '$from2'"
elif [ -e "$from2" ];then
echo "$dirname:$filename:$random:$(date +%y-%m-%d.%t)" >> $mvlog
mv "$from2" "$trash/$filename:$random"
else
file_null $from2
fi
}
#rm -rf
function rmrf()
{
local filename=$(basename $from2)
local dirname=$(dirname $from2)
jug_cur $dirname
if [ $jug -eq 0 ];then
dirname=$var_pwd
elif [ $jug -eq 1 ];then
dirname=$var_father
fi
if [ "$from2" = "." -o "$from2" = ".." ];then
echo "rm: cannot remove directory: '$from2'"
elif [ -e "$from2" ];then
echo "$dirname:$filename:$random:$(date +%y-%m-%d.%t)" >> $mvlog
mv "$from2" "$trash/$filename:$random"
else
:
fi
}
#rm -ir
function rmir()
{
local filename=$(basename $from2)
local dirname=$(dirname $from2)
jug_cur $dirname
if [ $jug -eq 0 ];then
dirname=$var_pwd
elif [ $jug -eq 1 ];then
dirname=$var_father
fi
if [ -e "$from2" ];then
if [ -d "$from2" ];then
echo -n "rm: remove directory '$from2'?"
else
echo -n "rm: remove regular file '$from2'?"
fi
read answer
if [ "$answer" = 'y' -o "$answer" = 'y' ];then
echo "$dirname:$filename:$random:$(date +%y-%m-%d.%t)" >> $mvlog
mv "$from2" "$trash/$filename:$random"
fi
else
if [ ! -e $from2 ];then
file_null $from2
fi
fi
}
#清空回收站
function rmc()
{
/bin/rm -rf $trash
}
function rml()
{
local tmp=$(mktemp /tmp/tfile.xxxxxx)
clear
if [ ! -d "$trash" ];then
mkdir $trash
fi
if [ ! -f "$mvlog" ];then
touch $mvlog
fi
line=$(cat -n $mvlog | awk -f: '{print $1, "filename:"$2, "time: "$4":"$5":"$6}')
linecount=$(cat $mvlog | wc -l)
echo -e "$line"
echo
echo
echo "[$linecount] please enter the file you want to restore (replaced with the line number)"
printf "rownumber: "
read answer
if [ "$answer" = 'q' -o "$answer" = 'q' -o "$answer" = "" ];then
:
else
printf "please confirm (y/n): "
read answer1
if [ "$answer1" = 'y' -o "$answer1" = 'y' ];then
address=$(sed -n "$answer""p" $mvlog | awk -f: '{print $1}')
filename=$(sed -n "$answer""p" $mvlog | awk -f: '{print $2}')
filerand=$(sed -n "$answer""p" $mvlog | awk -f: '{print $3}')
fullname=$address/$filename
if [ -e "$fullname" ];then
echo "the file exist!"
sleep 0.5
else
old="$trash/$filename:$filerand"
new="$address/$filename"
mv "$old" "$new"
#deline=$(cat $mvlog|sed "$answer""d" | sort -o $mvlog)
deline=$(cat $mvlog|sed "$answer""d" >> $tmp)
mv $tmp $mvlog
echo "restore success!"
sleep 0.5
fi
fi
fi
}
function help()
{
cat << 'eof'
usage: rm [option]... file...
remove (unlink) the file(s).
-f, --force ignore nonexistent files, never prompt
-i, --interactive prompt before any removal
--no-preserve-root do not treat `/' specially (the default)
--preserve-root fail to operate recursively on `/'
-r, -r, --recursive remove directories and their contents recursively
--help display this help and exit
by default, rm does not remove directories. use the --recursive (-r or -r)
option to remove each listed directory, too, along with all of its contents.
to remove a file whose name starts with a `-', for example `-foo',
use one of these commands:
rm -- -foo
rm ./-foo
note that if you use rm to remove a file, it is usually possible to recover
the contents of that file. if you want more assurance that the contents are
truly unrecoverable, consider using shred.
report bugs to <bug-coreutils@gnu.org>.
eof
}
#脚本开始
#检测回收站已用存储空间,如果已经达到最大值,则删除日志文件中位于前面的一半的文件
mem=$(du -s $trash|awk '{print $1}')
if [ "$mem" -gt $maxmemory ];then
deal
fi
if [ "$#" -eq 0 ];then
echo_msg
fi
if [ "$#" -eq 1 ];then
case "$from1" in
-i)
echo_msg
-f)
echo_msg
-r | -r)
echo_msg
-ir|-ri|-ir|-ri|-if|-fi|-rf|-fr|-rf|-fr)
echo_msg
-l)
rml
-c)
rmc
--help)
help
-*)
echo_msg2 $from1
*)
rm1
esac
fi
if [ "$#" -ge 2 ];then
until [ "$2" = "" ]
do
from2=$2
case "$from1" in
-i)
rmi
-f)
rmf
-r|-r)
rmr
-l)
rml
-rf|-rf|-fr|-fr)
rmrf
-ir|-ri|-ir|-ri)
rmir
-if|-fi)
rmf
--help)
help
exit 1
-*)
echo_msg2 $from1
exit 1
*)
{
until [ "$1" = "" ]
do
from1=$1
rm1
shift
done
}
esac
shift
done
fi
exit