CentOS設定系統資源回收筒功能
linux rm刪除檔案之後,恢複就比較麻煩了,即使恢複了,檔案名稱格式都變成一串數字了。
修改root使用者的環境變數
vi ~/.bashrc
注釋第5行的別名
#alias rm='rm -i'
最後一行添加如下內容
mkdir -p ~/.trashalias rm=trashalias r=trashalias rl='ls ~/.trash'alias ur=undelfileundelfile(){ mv -i ~/.trash/$@ ./}trash(){ mv $@ ~/.trash/}cleartrash(){ read -p "clear sure?[n]" confirm [ $confirm == 'y' ] || [ $confirm == 'Y' ] && /bin/rm -rf ~/.trash/*}
重新載入環境變數
source ~/.bashrc
使用命令ll -a查看目錄,發現多了目錄.trash,這個目錄是用來存在刪除的檔案
drwxr-xr-x. 2 root root 4096 Jun 4 11:31 .trash
刪除一個檔案
[root@localhost ~]# rm percona-xtrabackup_2.2.3.orig.tar.gz
查看目錄,發現刪除的檔案在資源回收筒目錄
[root@localhost ~]# ll .trash/total 33780-rw-r--r--. 1 root root 34584359 Jun 2 09:39 percona-xtrabackup_2.2.3.orig.tar.gz
如果需要清空資源回收筒檔案 使用以下命令
[root@localhost ~]# cleartrashclear sure?[n]y
再次查看,發現空了。
[root@localhost ~]# ll .trash/total 0
雖然rm用別名定義了,但是可以是用絕對路徑刪除檔案 比如 /bin/rm 1.txt 它是不會儲存到.trash目錄的。
如果需要定義自動清理7天刪除的檔案 可以寫一個指令碼
#!/bin/bashfind /root/.trash -ctime 7 -type f -name "*" -exec /bin/rm {} \;
然後在任務計劃裡面調用一下就可以了。
如果Linux除了root使用者,還有其他使用者需要登陸伺服器,也想他們使用資源回收筒機制 可以修改系統內容變數
vi /etc/profile
最後一行添加
mkdir -p ~/.trashalias rm=trashalias r=trashalias rl='ls ~/.trash'alias ur=undelfileundelfile(){ mv -i ~/.trash/$@ ./}trash(){ mv $@ ~/.trash/}cleartrash(){ read -p "clear sure?[n]" confirm [ $confirm == 'y' ] || [ $confirm == 'Y' ] && /bin/rm -rf ~/.trash/*}
重新載入環境變數
source /etc/profile
建立普通使用者測試
useradd a
設定密碼
passwd a
登陸Linux 查看目錄,發現會建立.trash目錄
[a@localhost ~]$ ll -atotal 24drwx------. 3 a a 4096 Jun 4 11:45 .drwxr-xr-x. 5 root root 4096 Jun 4 11:44 ..-rw-r--r--. 1 a a 18 Oct 16 2014 .bash_logout-rw-r--r--. 1 a a 176 Oct 16 2014 .bash_profile-rw-r--r--. 1 a a 124 Oct 16 2014 .bashrcdrwxrwxr-x. 2 a a 4096 Jun 4 11:45 .trash
建立一個空檔案
[a@localhost ~]$ touch 1.txt
刪除檔案
[a@localhost ~]$ rm 1.txt
查看資源回收筒目錄,發現多了一個檔案
[a@localhost ~]$ ll .trash/total 0-rw-rw-r--. 1 a a 0 Jun 4 11:45 1.txt
如果對.trash目錄位置覺得不爽,可以修改環境變數,改成其他位置,注意保證目錄可寫。