作者:Maxwell Li
日期:2017/02/03
未經(jīng)作者允許,禁止轉(zhuǎn)載本文任何內(nèi)容。如需轉(zhuǎn)載請留言。
近幾天 GitLab 由于程序猿加班誤刪了幾百 G 的數(shù)據(jù),細(xì)細(xì)想來,自己也因?yàn)?rm -rf 闖過不少禍。所以想著在 Linux 下建一個回收站,每次執(zhí)行 rm 命令時,將文件移動到回收站內(nèi),然后定時清除。Google 了一下,發(fā)現(xiàn)已經(jīng)有 trash-cli 工具了。
安裝 trash-cli:
$ apt-get install -y trash-cli
trash-cli 提供以下命令:
trash-put 或 trash 將文件或者目錄放入回收站
trash-empty 清空回收站
trash-list 列出回收站文件
restore-trash 還原回收站文件(會顯示列表,方便使用標(biāo)號還原)
trash-rm 刪除回收站文件(單個永久刪除)
在 ~/.bashrc 文件后加入配置,映射 rm 命令:
alias rm=trash-put
alias rl=trash-list
alias urm=restore-trash
添加完畢后保存,執(zhí)行 source 命令使其生效:
$ source ~/.bashrc
測試:
root@lyn:/home/test# mkdir test
root@lyn:/home/test# touch test1 test2
root@lyn:/home/test# ll
total 12
drwxr-xr-x 3 root root 4096 Feb 3 17:05 ./
drwxr-xr-x 6 root root 4096 Feb 3 17:05 ../
drwxr-xr-x 2 root root 4096 Feb 3 17:05 test/
-rw-r--r-- 1 root root 0 Feb 3 17:05 test1
-rw-r--r-- 1 root root 0 Feb 3 17:05 test2
root@lyn:/home/test# rm test
root@lyn:/home/test# rm test1
root@lyn:/home/test# rm test2
root@lyn:/home/test# rl
2017-02-03 17:05:45 /home/test/test
2017-02-03 17:05:55 /home/test/test2
2017-02-03 17:05:53 /home/test/test1
root@lyn:/home/test# urm
0 2017-02-03 17:05:45 /home/test/test
1 2017-02-03 17:05:55 /home/test/test2
2 2017-02-03 17:05:53 /home/test/test1
What file to restore [0..2]: 1
root@lyn:/home/test# ll
total 8
drwxr-xr-x 2 root root 4096 Feb 3 17:06 ./
drwxr-xr-x 6 root root 4096 Feb 3 17:05 ../
-rw-r--r-- 1 root root 0 Feb 3 17:05 test2
root@lyn:/home/test# rm test2
root@lyn:/home/test# trash-empty
root@lyn:/home/test# rl
所有功能正常,但是作為懶癌晚期,必須設(shè)置定時清理。利用 crontab -e 添加定時任務(wù)。輸入:
0 2 * * * trash-empty
保存退出。
crontab 的詳細(xì)用法:http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html