- 我們在Linux環(huán)境中工作時, 經(jīng)常會用到rm命令刪除文件, 但是rm命令是即刻刪除的, 有時可能會出現(xiàn)誤刪的問題.
- 針對這種情況, 本文參考網(wǎng)上已有解決方案, 學(xué)習(xí)并編寫了一個回收站腳本.
一 工具
- 基本功能
- 暫存被刪除文件
rm <file/directory>
- 恢復(fù)被刪除文件
re <file/directory>
- 清空回收站
rc
- 獲取回收站大小
rsize
- 暫存被刪除文件
初始化與安裝
- github, https://github.com/trioZwShen/itrashcan.git
- 在
~/.bashrc
中粘貼下面的設(shè)置環(huán)境變量, 并執(zhí)行source ~/.bashrc
, 或者重新打開終端# itrashcan export itrashcan="/home/szw/repos/itrashcan" # set the script path export itrashcan_home="/home/szw/.trashcan" # set the transcan path source "$itrashcan/config.sh" # source the config for alias
- 初始化itrashcan
bash $itrashcan/init.sh
- 查看config.sh中的命令別名
# set alias # rm by itrashcan alias rm="$itrashcan/delete.sh" # clear the trashcan alias rc="$itrashcan/clear.sh" # restore the target alias re="$itrashcan/restore.sh" # go to the trashcan alias gor="cd $itrashcan_home/trash" # get the size of trashcan alias rsize="du -sh $itrashcan_home/trash"
- 刪除回收站
bash remove.sh
測試
- rm
$ ls
a b c
$ rm *
/home/szw/download/a is move to trashcan
/home/szw/download/b is move to trashcan
/home/szw/download/c is move to trashcan
- re
$ re a b
/home/szw/download/a restored
/home/szw/download/b restored
- rc
$ rc
Are you sure you want to permanently erase the items in the Trash?[Y/n]
Y
Done!
- rsize
$ rsize
4.0K /home/szw/.trashcan/trash
二 功能實現(xiàn)
1 環(huán)境初始化
環(huán)境變量
# itrashcan
export itrashcan="/home/szw/repos/itrashcan" # set the script path
export itrashcan_home="/home/szw/.trashcan" # set the transcan path
source "$itrashcan/config.sh" # source the config for alias
- itrashcan設(shè)置為腳本所在路徑
- itrashcan_home設(shè)置為回收站的目標(biāo)位置, 回收站名稱可以自擬
- config.sh中包含了itrashcan的命令別名
腳本
- init.sh 執(zhí)行前需設(shè)置環(huán)境變量
- 檢查環(huán)境變量是否被設(shè)置
- 檢查回收站目標(biāo)位置是否有沖突
- 創(chuàng)建回收站
- 初始化腳本以及回收站的權(quán)限
#!/bin/bash
# run this script for init your environment
# set -x
set -e
# check the $itrashcan_home.
if [ ! -n "$itrashcan_home" ]; then
echo "Please set the environment var first!!!"
exit
fi
# exit if the $itrashcan_home already exists.
if [ -d "$itrashcan_home" ]; then
echo "The directory already exists in the target path, please check!!!"
exit
fi
# create trashcan and trash log
mkdir $itrashcan_home
mkdir $itrashcan_home"/trash"
touch $itrashcan_home"/trash.log"
# init permissions
chmod 777 -R $itrashcan_home
chmod 755 -R $itrashcan
echo "Init Done."
-
remove.sh
- 刪除回收站
#!/bash/bin
# run this script for remove itrashcan config and environmental
# set -x
set -e
# clear file and directory
/bin/rm -rf $itrashcan_home
echo "Remove done."
2 刪除文件
delete.sh
- 該腳本首先會檢查環(huán)境是否已經(jīng)初始化, 沒有則init
# init if the $itrashcan_home not exists if [ ! -d "$itrashcan_home" ]; then bash init.sh fi
- 檢查輸入是否正確, 如果
-f
選項, 那么使用real rm - 利用
ll
和du
判斷目標(biāo)文件是否大于2G, 如果大于2G, 則會直接刪除 - 重命名回收站中的文件為
filename_deletetime
, 防止文件重名 - 獲取文件的絕對路徑, 用于恢復(fù)回收站文件
- 移動文件, 并打log
#!/bin/bash
# set -x
set -e
realrm="/bin/rm"
# init if the $itrashcan_home not exists
if [ ! -d "$itrashcan_home" ]; then
bash init.sh
fi
# usage tips
if [ $# -eq 0 ]; then
echo "Usage:delete file [file2 file3 ...]"
echo "If the options contain -f then the script will exec 'rm' directly."
fi
# identify input options, if f is point then exec realrm
while getopts "dfiPRrvw" opt
do
case $opt in
f)
echo "real rm done"
exec $realrm "$@"
;;
d)
# do nothing
;;
i)
# do nothing
;;
P)
# do nothing
;;
R)
# do nothing
;;
r)
# do nothing
;;
v)
# do nothing
;;
w)
# do nothing
;;
?) # unknow argument
exit
;;
esac
done
# travel the input file
for file in $@
do
if [ -f "$file" -o -d "$file" ]; then
# real rm if size is larger than 2G
if [ -f "$file" ] && [ `ls -l $file|awk '{print $5}'` -gt 2147483648 ]
then
echo "$file size is lager than 2G, will be deleted directly"
rm -rf $file
fi
if [ -d "$file" ] && [ `du -sb $file|awk '{print $1}'` -gt 2147483648 ]
then
echo "The directory: $file size is lager than 2G, will be deleted directly"
rm -rf $file
fi
# rename
now=`date +%Y-%m-%d_%H_%M_%S`
filename=`basename $file`
newfilename="${filename}_${now}"
# get fullpath
basepath=$(cd `dirname $file`; pwd)
fullpath="${basepath}/${filename}"
# mv to trashcan
if mv -f "$fullpath" "$itrashcan_home/trash/$newfilename"
then
bash $itrashcan/log.sh $newfilename $filename $now $fullpath
echo "$fullpath is move to trashcan"
else
echo "fail"
fi
else
echo "$file is not exist"
fi
done
# trashcan size > 10G remider user
if [ `du -sb "${itrashcan_home}"/trash|awk '{print $1}'` -gt 5368709120 ]; then
echo "Warning: trashcan's size[`du -sh "${itrashcan_home}/trash"|awk '{print $1}'`] is biger than 5G."
fi
3 恢復(fù)數(shù)據(jù)
restore.sh
- 檢查輸入
- 設(shè)置log路徑, 回收站路徑, 以及待查找的目標(biāo)模式
findpattern
- 在log中查找目標(biāo)文件, 獲取同名文件數(shù)rowcount;
- 如果有多個重名文件, 需要用戶自己確定恢復(fù)哪個
- 檢查回收站中該文件是否存在
- 檢查原路徑是否存在, 檢查原路徑中是否有重名文件
- 恢復(fù)數(shù)據(jù), 并刪除該記錄
#!/bin/bash
set -e
if [ $# -eq 0 ]; then
echo "Please enther the target what your want to restore!";
exit;
fi
# travel the input file
for target in $@
do
log="${itrashcan_home}/trash.log"
trashcan="${itrashcan_home}/trash"
findpattern="filename:${target};"
# get the number of files with the same name
rowcount=`cat -n "$log" | grep "$findpattern" | wc -l`
if [ $rowcount -eq 0 ]; then
echo "${target} does not exists in LOG"
exit
elif [ $rowcount -eq 1 ]; then
output=`cat -n "$log" | grep "$findpattern"`
index=`echo $output | awk '{print $1}'`
else
cat -n "${itrashcan_home}/trash.log" | grep "${findpattern}"
echo -n "Please enter the line number of the target: "
read index
output=`cat -n "$log" | grep "\ ${index}\ *.*${findpattern}.*"`
fi
# get the fullpath and trashname by index
fullpath=`echo $output | awk '{print $5}'`
fullpath=${fullpath#*:}
trashname=`echo $output | awk '{print $2}'`
trashname=${trashname#*:}
# check the trashname
trashnamepath="${trashcan}/${trashname}"
if [ ! -d "$trashnamepath" -a ! -f "$trashnamepath" ]; then
echo "${target} does not exists in TRASHCAN"
exit
fi
# Whether the original path exists.
basepath=`dirname $fullpath`
if [ ! -d $basepath ]; then
echo "The original path [${basepath}] does not exist!!!"
exit
fi
# Is there a duplicate file under the original path?
if [ -d $fullpath -o -f $fullpath ]; then
echo "The original path has a duplicate file [$fullpath]!!!"
exit
fi
# restore from trashcan
if ! mv -f "$trashnamepath" "$fullpath"; then
echo "failed"
exit
else
# delete the log
sed -i "${index}d" $log
echo "${fullpath} restored"
fi
done
4 log
log.sh
- 需記錄原文件名, 重命名后的文件名, 源文件所在路徑
#!/bin/bash
log="${itrashcan_home}/trash.log"
# create log file if log does not exist
if [ ! -d $log ]; then
touch $log
fi
echo "trashname:$1 filename:$2; deletetime:$3 fullpath:$4" >> $log
5 清空回收站
clear.sh
#!/bin/bash
echo "Are you sure you want to permanently erase the items in the Trash?[Y/n]"
read ask
if [ $ask = 'y' -o $ask = 'Y' ]
then
`rm -rf $itrashcan_home/trash/*`
`rm -rf $itrashcan_home/trash.log`
echo "Done!"
fi
參考blog
http://www.reibang.com/writer#/notebooks/27796113/notes/42433921/preview