我們知道Windows系統(tǒng)有回收站的功能,正確設(shè)置后,當用戶刪除文件或文件夾時,操作系統(tǒng)會將這些“刪除”的文件或文件夾放到回收站中,而并沒有真正意義上的刪除文件。其實Linux系統(tǒng)中也可以模擬這樣的功能。下面介紹一下GitHub上的一個非常有意思的項目,里面有個腳本Saferm.sh可以模擬這種功能。關(guān)于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目錄下面,下面測試環(huán)境為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。關(guān)于bashrc,它用于保存用戶的環(huán)境信息,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 functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi 修改/root/.bashrc文件,修改或增加一行配置
#alias rm='rm -i' alias rm=saferm.sh
測試
執(zhí)行source .bashrc ,讓環(huán)境變量生效,然后我們簡單測試,測試驗證其已經(jīng)生效
[root@KerryDB tmp]# source /root/.bashrc [root@KerryDB tmp]# ls linux-stuff [root@KerryDB tmp]# rm -rf linux-stuff/ Moving linux-stuff/ to /root/Trash [root@KerryDB tmp]#
如上所示,我修改/root/.bashrc 這個文件,在root賬號下刪除文件或文件夾時,系統(tǒng)將其移動到/root/Trash下面。那么此時在postgres用戶下測試,就會發(fā)現(xiàn)文件直接被刪除了,并沒有將其放到“回收站”。這個是因為我們沒有設(shè)置postgres用戶家目錄下的.bashrc(/home/postgres/.bashrc),所以,如果要對每個用戶都生效,有兩種解決方案:
1:修改每個用戶家目錄下的.bashrc文件,修改其私有環(huán)境變量。
2:修改/etc/bashrc文件,這個是是系統(tǒng)全局環(huán)境變量設(shè)定
例如,我們修改/etc/bashrc后,執(zhí)行source /etc/bashrc使其生效后,測試發(fā)現(xiàn)在postgres用戶下也會將刪除的文件移動到/home/postgres/Trash下了。
[root@KerryDB ~]# su - postgres Last 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 是系統(tǒng)全局環(huán)境變量設(shè)定,~/.bashrc用戶家目錄下的私有環(huán)境變量設(shè)定。這里不做展開介紹。
你會發(fā)現(xiàn)“回收站”目錄在各個用戶的家目錄下,文件夾名Trash,其實這個是可以修改配置的,因為saferm.sh里面就是這樣設(shè)定的。當然也可以修改。
BUG和問題
另外,就是發(fā)現(xiàn)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
這種方式刪除的文件直接被刪除了(這個也是偶然一次操作測試發(fā)現(xiàn)的),并沒有移動到回收站下面。
定期清理“回收站”
如果一直不清理回收站,那么就有可能出現(xiàn)磁盤空間告警的情況,正確的做法是配置crontab作業(yè),定期清空“垃圾回收站”。例如類似這樣的設(shè)置 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./rm-saferm-trash.html
|
|