免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Linux如何模擬垃圾回收站功能

我們知道Windows系統(tǒng)有回收站的功能,正確設置后,當用戶刪除文件或文件夾時,操作系統(tǒng)會將這些刪除的文件或文件夾放到回收站中,而并沒有真正意義上的刪除文件。其實Linux系統(tǒng)中也可以模擬這樣的功能。下面介紹一下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目錄下面,下面測試環(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。關于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)文件直接被刪除了,并沒有將其放到回收站。這個是因為我們沒有設置postgres用戶家目錄下的.bashrc/home/postgres/.bashrc),所以,如果要對每個用戶都生效,有兩種解決方案:

 

1:修改每個用戶家目錄下的.bashrc文件,修改其私有環(huán)境變量。

 

2:修改/etc/bashrc文件,這個是是系統(tǒng)全局環(huán)境變量設定

 

例如,我們修改/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)境變量設定,~/.bashrc用戶家目錄下的私有環(huán)境變量設定。這里不做展開介紹。

 

你會發(fā)現(xiàn)回收站目錄在各個用戶的家目錄下,文件夾名Trash,其實這個是可以修改配置的,因為saferm.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è),定期清空垃圾回收站。例如類似這樣的設置

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 

 

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
上班族必看的電腦技能,解決你的尷尬
將linux下的rm命令改造成mv到指定的目錄下
PostgreSQL查看數(shù)據(jù)目錄總結
linux回收站的自作與自動備份/系統(tǒng)維護(清理)
rm-rf *可怕不?來給 rm 加個垃圾桶
Centos 登陸用戶提示bash
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服