我們知道Windows系統有回收站的功能,正確設定后,當用戶洗掉檔案或檔案夾時,作業系統會將這些“洗掉”的檔案或檔案夾放到回收站中,而并沒有真正意義上的洗掉檔案,其實Linux系統中也可以模擬這樣的功能,下面介紹一下GitHub上的一個非常有意思的專案,里面有個腳本Saferm.sh可以模擬這種功能,關于Saferm.sh的介紹如下,更多詳細資訊參考https://github.com/lagerspetz/linux-stuff
This repo contains useful linux scripts. No guarantee that they work or warranty of any kind is given. Some highlights:
Saferm.sh
· scripts/saferm.sh: alias this to "rm". Moves files to your desktop environment's trash folder instead of permanently deleting files when you type "rm".
· scripts/manually-installed.sh: Shows the list of manually installed (deb) packages on the system. Useful for keeping track of what is installed.
· scripts-manually-installed-deps.sh: Shows which packages are manually installed, but do not need to be, because they are being pulled as dependencies by other packages. With the -a flag marks these manually installed dependencies as automatically installed.
安裝
安裝方式非常簡單,就是將saferm.sh這個腳本拷貝到/bin目錄下面,下面測驗環境為CentOS Linux release 7.5.1804 (Core)
# git clone https://github.com/lagerspetz/linux-stuff# mv linux-stuff/scripts/saferm.sh /bin
配置
找到.bashrc檔案,修改或增加一行alias rm=saferm.sh,關于bashrc,它用于保存用戶的環境資訊,bashrc用于互動式non-loginshell,每個可登陸用戶的目錄下都有.bashrc這樣一個隱藏檔案,
[root@KerryDB tmp]# find / -name ".bashrc"/home/postgres/.bashrc
/etc/skel/.bashrc
/root/.bashrc
[root@KerryDB tmp]# more /root/.bashrc# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'# Source global definitionsif [ -f /etc/bashrc ]; then. /etc/bashrc
fi
修改/root/.bashrc檔案,修改或增加一行配置
#alias rm='rm -i'
alias rm=saferm.sh
測驗
執行source .bashrc ,讓環境變數生效,然后我們簡單測驗,測驗驗證其已經生效
[root@KerryDB tmp]# source /root/.bashrc[root@KerryDB tmp]# lslinux-stuff
[root@KerryDB tmp]# rm -rf linux-stuff/Moving linux-stuff/ to /root/Trash
[root@KerryDB tmp]#

如上所示,我修改/root/.bashrc 這個檔案,在root賬號下洗掉檔案或檔案夾時,系統將其移動到/root/Trash下面,那么此時在postgres用戶下測驗,就會發現檔案直接被洗掉了,并沒有將其放到“回收站”,這個是因為我們沒有設定postgres用戶家目錄下的.bashrc(/home/postgres/.bashrc),所以,如果要對每個用戶都生效,有兩種解決方案:
1:修改每個用戶家目錄下的.bashrc檔案,修改其私有環境變數,
2:修改/etc/bashrc檔案,這個是是系統全域環境變數設定
例如,我們修改/etc/bashrc后,執行source /etc/bashrc使其生效后,測驗發現在postgres用戶下也會將洗掉的檔案移動到/home/postgres/Trash下了,
[root@KerryDB ~]# su - postgresLast login: Thu May 21 15:48:50 +08 2020 on pts/1
[postgres@KerryDB ~]$ cat >test.txt
it's only a test
^C
[postgres@KerryDB ~]$ rm test.txt
Moving test.txt to /home/postgres/Trash
[postgres@KerryDB ~]$
其實/etc/bashrc 是系統全域環境變數設定,~/.bashrc用戶家目錄下的私有環境變數設定,這里不做展開介紹,
你會發現“回收站”目錄在各個用戶的家目錄下,檔案夾名Trash,其實這個是可以修改配置的,因為saferm.sh里面就是這樣設定的,當然也可以修改,

BUG和問題
另外,就是發現saferm.sh確實也是有bug的,并不能100%的保證任何被洗掉的檔案都會放入“回收站”,例如下面例子所示
[root@KerryDB log]# ls -lrt mail*-rw------- 1 root root 0 Apr 19 03:31 maillog-20200426
-rw------- 1 root root 0 Apr 26 03:41 maillog-20200503
-rw------- 1 root root 0 May 3 03:22 maillog-20200510
-rw------- 1 root root 0 May 10 03:17 maillog-20200517
-rw------- 1 root root 0 May 17 03:39 maillog
[root@KerryDB log]# find /var/log -mtime +7 -name "maillog*" -exec rm -rf {} \;[root@KerryDB log]# ls /root/Trash/linux-stuff
這種方式洗掉的檔案直接被洗掉了(這個也是偶然一次操作測驗發現的),并沒有移動到回收站下面,
定期清理“回收站”
如果一直不清理回收站,那么就有可能出現磁盤空間告警的情況,正確的做法是配置crontab作業,定期清空“垃圾回收站”,例如類似這樣的設定
0 * * * 6 find /root/Trash/ -mtime +7 -name "*" -exec rm -rf {} \;
參考資料:
https://github.com/lagerspetz/linux-stuff/blob/master/scripts/saferm.sh
https://github.com/kaelzhang/shell-safe-rm
https://www.linuxprobe.com/rm-saferm-trash.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/77483.html
標籤:Linux
