关于rm的艺术

/ Linux_manageLinux_Safe / 没有评论 / 1773浏览

后悔药系列

放脚本

cat /tmp/rm.sh

#!/bin/bash

mkdir -p /tmp/.trash
TRASH_DIR="/tmp/.trash"
for i in $*; do
    #定义秒时间戳
    STAMP=`date +%s`
    #得到文件名称(非文件夹),参考man basename
    fileName=`basename $i`
    #将输入的参数,对应文件mv至.trash目录,文件后缀,为当前的时间戳
    mv $i $TRASH_DIR/$fileName.$STAMP
    echo -e "\033[31m $1 >>>>> /tmp/.trash \033[0m"
done

改环境变量

cat >> /root/.bashrc <<EOF
alias rm="sh /tmp/rm.sh"          #这是我脚本放的位置
EOF
source /root/.bashrc   ## 生效

测试

[root@pa2 tmp]# touch 666
[root@pa2 tmp]# rm 666
 666 >>>>> /tmp/.trash 

 [root@pa2 tmp]# ll .trash/
total 12
-rw-r--r-- 1 root root    0 May 16 18:17 666.1526465830   ##在这
-rw-r--r-- 1 root root   73 May 15 17:20 lua_JZ2h4i.1526465674
drwxr-xr-x 3 root root 4096 May 10 17:22 pear.1526465758
-rw-r--r-- 1 root root  269 May 14 14:34 wr.sh.1526465716

清理脚本+定时任务

[root@pa2 tmp]# cat clear.sh
#!/bin/bash
find /tmp/.trash -mtime +3 -exec \rm {} \;

加入定时任务,我知道你会的

删根测试?


[root@local ~]# rm /*
 /bin >>>>> /tmp/.trash 
/tmp/rm.sh: line 7: date: command not found
/tmp/rm.sh: line 9: basename: command not found
/tmp/rm.sh: line 11: mv: command not found
 /bin >>>>> /tmp/.trash 
/tmp/rm.sh: line 7: date: command not found
/tmp/rm.sh: line 9: basename: command not found
/tmp/rm.sh: line 11: mv: command not found
 /bin >>>>> /tmp/.trash 
/tmp/rm.sh: line 7: date: command not found
/tmp/rm.sh: line 9: basename: command not found
/tmp/rm.sh: line 11: mv: command not found
 /bin >>>>> /tmp/.trash 
················省略······················
 
[root@local ~]# cd /tmp/
[root@local tmp]# ll -a 
-bash: /bin/ls: No such file or directory    #一下回到解放前

改别名系列

echo "alias rm='No use rm command'" >> /etc/profile

vim /root/.bashrc   #注释掉想要改别名的那一行

source /etc/profile #生效

结果

实验证明,你要删根,谁都阻止不了

但是,又不能否认我们这个脚本的作用,抛开环境谈作用都是扯淡