1 Star 0 Fork 0

Velcon-Zheng/rm-rt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rt 2.58 KB
一键复制 编辑 原始数据 按行查看 历史
Velcon-Zheng 提交于 2020-04-06 12:36 . Update rt
#!/bin/bash
## rt/remove_trash.sh
## 1. It is designed to replace old command rm without having to delete it.
##  2. It can avoid deleting files or directories with old command rm by mistake.
##  3. You can continue to use new 'rm' to delete files or directories, and it will move the target file or directory to
##   a trash directory instead of directly deleting them.
##  4. Besides, you can set the time to automatically empty the trash directory.
## Author: WeigangZheng, zwg2016@yeah.net
## Date: 2020-04-05
##******First step******
## git clone https://github.com/Velcon-Zheng/rm-rt.git
## cp rm-rt/rt ~/ ##cp rm-rt/rt to your directory you want;
## echo 'alias "rm= ~/rt"' >> ~/.bashrc ; source ~/.bashrc;
##******Second step******
## crontab -e
## 0 0 * * 0 rm -rf ~/zwg.trash/*
#########################
#alias rm="~/rt" # Replace rm command with rt command, the "~/rt" should be set to your global path. It should be written in ~/.bashrc
Trashdir="~/zwg.trash" # Set to your trash dirctory, and suggest not set to your home ~/
## Check the order is correct
if [ ! -d $Trashdir ]; then
echo -e "Warning: $Trashdir not exist;\nTry to creat $Trashdir"
mkdir $Trashdir || exit ;
fi
if [ $# -lt 1 ]; then
echo "Error: no target for rm"
echo "Usage: rm myfile mydirectory"
exit
fi
AA="del"
BB=`basename $1`
CC=1
DD=1
onoff="No"
Time=`date | sed 's/ \+/-/g'`
## move the target files or directories to a trash directory or permanently delete them
for i in $*; do
if [ $CC -lt 2 ]
then
if [ "$AA" == "$BB" ]
then
DD=$[DD+1]
#read -t 10 -p "***请确认是否永久删除文件或文件夹(Are you sure to permanently delete files or directories? Y/N)***:" onoff
read -t 10 -p "Are you sure to permanently delete files or directories? Y/N:" onoff
echo -e "\n"
if [ "$onoff" == "Y" ] || ( echo $onoff | grep -qi "Yes" )
then
unalias rm
fi
else
TrashFileName=`basename $i`
mv $i $Trashdir/$TrashFileName-$Time
fi
else
if [ $DD -ge 2 ] && ( [ "$onoff" == "Y" ] || ( echo $onoff | grep -qi "Yes" ) )
then
rm -rif $i
else
TrashFileName=`basename $i`
mv $i $Trashdir/$TrashFileName-$Time
fi
fi
CC=$[CC+1]
done
if [ $DD -ge 2 ] && ( [ "$onoff" == "Y" ] || ( echo $onoff | grep -qi "Yes" ) )
then
alias rm="~/rt"
echo -e "Warning: have permanently deleted the target file or directory \n"
else
echo -e "Finished: have moved the target file or directory to $Trashdir \n"
fi
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/Velcon-Zheng/rm-rt.git
git@gitee.com:Velcon-Zheng/rm-rt.git
Velcon-Zheng
rm-rt
rm-rt
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385