一. 功能簡介
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