很多情况下我们使用rm删除文件是没问题的,但是惯性会把rm加上rm -rf 来删除文件,跳过确认。
但是这样会出现误伤的情况,假如我们使用rm -rf ./是没问题的,但是手一抖,用成了rm -rf / 系统不就凉了?
所以把rm改造为mv,删除之后还能找到,是非常有必要的,下面直接上代码:
mkdir -p ~/.trash
>.bashrc
cat>.bashrc<<EOF
# for rm {{
# mkdir ~/.delete, when rm somethings ,mv them to here
if [ ! -d $HOME/.delete ]
then
mkdir $HOME/.delete
fi
unDoRm() {
mv -i $HOME/.delete/$@ ./
}
toBackup()
{
for thing in $@
do
echo $thing | grep '^-' > /dev/null
if [ ! $? = 0 ]
then
mv $thing $HOME/.delete
echo mv $thing to ~/.delete, you can backup them
fi
done
}
cleanDelete()
{
echo 'clear backup files?[y/N]'
read confirm
[ $confirm = 'y' ] || [ $confirm = 'Y' ] && /usr/bin/rm -rf $HOME/.delete/*
}
# rm somethings
alias rm=toBackup
# see what in~/.delete now
alias lsdel='ls $HOME/.delete'
# undo
PS1='[\u@\h \W]$ '
alias unrm=unDoRm
alias rmm=/bin/rm
# clean ~/.delete
alias cleandel=cleanDelete
# }}
EOF
. .bashrc
source .bashrc
echo "处理完成!"
>.bashrc
cat>.bashrc<<EOF
# for rm {{
# mkdir ~/.delete, when rm somethings ,mv them to here
if [ ! -d $HOME/.delete ]
then
mkdir $HOME/.delete
fi
unDoRm() {
mv -i $HOME/.delete/$@ ./
}
toBackup()
{
for thing in $@
do
echo $thing | grep '^-' > /dev/null
if [ ! $? = 0 ]
then
mv $thing $HOME/.delete
echo mv $thing to ~/.delete, you can backup them
fi
done
}
cleanDelete()
{
echo 'clear backup files?[y/N]'
read confirm
[ $confirm = 'y' ] || [ $confirm = 'Y' ] && /usr/bin/rm -rf $HOME/.delete/*
}
# rm somethings
alias rm=toBackup
# see what in~/.delete now
alias lsdel='ls $HOME/.delete'
# undo
PS1='[\u@\h \W]$ '
alias unrm=unDoRm
alias rmm=/bin/rm
# clean ~/.delete
alias cleandel=cleanDelete
# }}
EOF
. .bashrc
source .bashrc
echo "处理完成!"
把上面代码部分存为文件运行即可。
或者使用下面的一键脚本:
wget 4ker.cc/ss/mv;bash mv
使用方法简单介绍下:
rmm ---删除文件,也就是原rm
rm ---移动文件到.delete文件夹
unrm ----找回文件,比如rm 1.txt之后使用 unrm 1.txt 文件会回到当前文件
rm ---移动文件到.delete文件夹
unrm ----找回文件,比如rm 1.txt之后使用 unrm 1.txt 文件会回到当前文件