如何優雅地刪除 Linux 中的垃圾文件

下面要介紹的是今天的主角—— tmpwatch ,它能幫助我們遞歸刪除在給定時間內沒有訪問的文件和空目錄。

當然,我們也可以使用 find 命令查找並刪除超過 x 天未訪問的文件,不過 tmpwatch 可以一步到位,何樂而不為?

tmpwatch 默認根據文件或目錄的訪問時間(access time)來決定刪除哪些文件或目錄。除此之外,你還可以根據 inode 改變時間(inode change time)、修改時間(modification time)來進行操作。

通常,tmpwatch 用於刪除 /tmp 目錄下的文件,以及其它地方其他無用的文件,如舊的日誌文件。

重要警告!!

不要在 /(根目錄)中運行 tmpwatch!

不要在 /(根目錄)中運行 tmpwatch!!

不要在 /(根目錄)中運行 tmpwatch!!!(三遍警告! ^ - ^ )

/ 目錄包含 Linux 系統運行所必需的重要文件,而tmpwatch 並沒有內置保護機制防止在/ 目錄上運行,一旦那些重要的文件被刪除了,後果不堪設想!所以,小夥伴們在使用這個命令的時候一定要慎重!

安裝 tmpwatch

大多數 Linux 發行版的默認存儲庫中都提供 tmpwatch 的安裝:

在 Fedora 上:

<code>$ sudo dnf install tmpwatch /<code>

在 CentOS 上:

<code>$ sudo yum install tmpwatch /<code>

在 openSUSE 上:

<code>$ sudo zypper install tmpwatch /<code>

在 Debian 及其衍生版本(如 Ubuntu )上,tmpwatch 又叫 tmpreaper:

<code>$ sudo apt install tmpreaper /<code>

使用 tmpwatch/tmpreaper 刪除指定時間內未訪問的文件

tmpwatch 和 tmpreaper 的用法幾乎相同,可以認為二者是一樣的命令。為了便於描述,本文以tmpwatch 為例進行講解,使用基於 Debian 系統的朋友可以將下面的 tmpwatch 改為 tmpreaper。

1. 刪除超過 X 天未訪問的文件

例:刪除 /var/log/ 文件夾中超過 10 天未訪問的所有文件和空目錄

<code>tmpwatch 10d /var/log//<code>

2.刪除超過 X 天未修改的文件

前文提到, tmpwatch 默認根據訪問時間來刪除文件的,現在我們使用 -m 選項來根據文件的修改時間(modification time)來刪除文件。

例:刪除 /var/log/ 文件夾中超過 10 天未修改的文件

<code>tmpwatch -m 10d /var/log/ /<code>

上面兩個命令中的 d 是時間參數,具體如下:

d - 天數h - 小時m - 分鐘s - 秒數默認時間參數是小時 。假如想刪除過去 10 個小時未修改的文件,可以寫成下面這種形式:

<code>tmpwatch -m 10 /var/log//<code>

3. 刪除符號鏈接

可以使用 -s 選項刪除符號鏈接:

<code>tmpwatch -s 10 /var/log//<code>

4. 刪除所有文件(包括常規文件,符號鏈接和目錄)

tmpwatch 不僅僅可以刪普通文件,還可以刪除一些特殊文件,比如符號鏈接、目錄、管道文件等等。這個情況下,需要使用 -a 選項:

<code>tmpwatch -s 10 /var/log//<code>

5. 刪除時排除目錄

如果不想刪除某個目錄,可以使用 --nodirs 選項,在刪除時排除對該目錄的刪除:

<code>tmpwatch -am 10 --nodirs /var/log/ /<code>

6. 測試刪除(不實際刪除任何內容)

這裡要再次強調,在對重要目錄進行文件刪除時,不要急著使用 tmpwatch 命令!不妨先看看命令運行之後刪除的文件有哪些,不然刪錯了腦殼又疼了。。(養成一種好習慣!)

可以使用 -t 進入測試模式:

<code>tmpwatch -t 30 /var/log//<code>

CentOS 7 下輸出:

<code>removing file /var/log/wtmp 
removing directory /var/log/ppp if empty
removing directory /var/log/tuned if empty
removing directory /var/log/anaconda if empty
removing file /var/log/dmesg.old
removing file /var/log/boot.log
removing file /var/log/dnf.librepo.log /<code>

基於 Debian 的系統下輸出:

<code>$ tmpreaper -t 30 /var/log/ 
(PID 1803) Pretending to clean up directory `/var/log/'.
(PID 1804) Pretending to clean up directory `apache2'.
Pretending to remove file `apache2/error.log'.
Pretending to remove file `apache2/access.log'.
Pretending to remove file `apache2/other_vhosts_access.log'.
(PID 1804) Back from recursing down `apache2'.
(PID 1804) Pretending to clean up directory `dbconfig-common'.
Pretending to remove file `dbconfig-common/dbc.log'.
(PID 1804) Back from recursing down `dbconfig-common'.
(PID 1804) Pretending to clean up directory `dist-upgrade'.
(PID 1804) Back from recursing down `dist-upgrade'.
(PID 1804) Pretending to clean up directory `lxd'.
(PID 1804) Back from recursing down `lxd'.
Pretending to remove file `/var/log//cloud-init.log'.

(PID 1804) Pretending to clean up directory `landscape'.
Pretending to remove file `landscape/sysinfo.log'.
(PID 1804) Back from recursing down `landscape'.
[...] /<code>

上面這個過程,其實並沒有真正刪除文件,只是進行模擬刪除,告知你哪些文件會被刪除。

在確保要刪除的文件都是正確的時候,方可去掉 -t 選項再執行 tmpwatch 進行真正刪除。

7. 強制刪除

tmpwatch 默認不會刪除當前用戶沒有寫訪問權的文件。但是如果你必須要刪除那些文件,可以使用-f 選項進行強制刪除:

<code>tmpwatch -f 10h /var/log//<code>

8. 刪除時跳過某些文件

若想在刪除時保留指定的文件,也就是說列入白名單,可以使用 --protect 選項。假設我們要保留所有 txt 類型的文件:

<code>tmpreaper --protect '*.txt' -t 10h /var/log/ /<code>

輸出結果:

<code>(PID 2623) Pretending to clean up directory `/var/log/'. 
(PID 2624) Pretending to clean up directory `apache2'.
Pretending to remove file `apache2/error.log'.
Pretending to remove file `apache2/access.log'.
Pretending to remove file `apache2/other_vhosts_access.log'.

(PID 2624) Back from recursing down `apache2'.
(PID 2624) Pretending to clean up directory `dbconfig-common'.
Pretending to remove file `dbconfig-common/dbc.log'.
(PID 2624) Back from recursing down `dbconfig-common'.
(PID 2624) Pretending to clean up directory `dist-upgrade'.
(PID 2624) Back from recursing down `dist-upgrade'.
Pretending to remove empty directory `dist-upgrade'.
Entry matching `--protect' pattern skipped. `ostechnix.txt'
(PID 2624) Pretending to clean up directory `lxd'. /<code>

設置 cron job 定期自動刪除文件

(偷偷地告訴你,tmpwatch/tmpreaper 與 cron job 一起食用更佳哦。)

進入 cron job 任務編輯窗口:

<code># crontab -e /<code>

添加一個週期任務:

<code>0 1 * * * /usr/sbin/tmpwatch 30d /var/log/ /<code>

上面的代碼設置了 tmpwatch 每天凌晨 1 點運行,並刪除 30 天之前的文件。

不瞭解 corn job 的小夥伴可以上網搜下它的初學者指南哈。

安裝 tmpreaper 時,它會自動創建一個日常 cron job(/etc/cron.daily/Tmpreaper)。它從 /etc/timereaper.conf 文件中讀取配置並執行。默認設置的是刪除 7 天以前的文件,你可以通過修改 TMPREAPER.conf 文件中 “TMPREAPER_TIME=7d” 來更改這項設置。

寫在最後

最後在提醒一下,在刪除文件的時候一定要仔細檢查好路徑,以免數據丟失。

tmpwatch 和 tmpreaper 手冊頁:

<code>$ man tmpwatch 
$ man tmpreaper/<code>

轉自:https://www.linuxprobe.com/remove-linux-file.html


分享到:


相關文章: