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

不知道你們是否也跟我同樣,是一隻要把的本身電腦文件安排的條理有序,把沒用的文件會及時刪掉的程序猿呢?若是是的話,那麼咱們能夠愉快地探討下文章的內容。若是不是的話,你也能夠留下來湊湊熱鬧嘛(>-<)。node

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

固然,咱們也可使用 find 命令查找並刪除超過 x 天未訪問的文件,不過 tmpwatch 能夠一步到位,何樂而不爲?apache

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

一般,tmpwatch 用於刪除 /tmp 目錄下的文件,以及其它地方其餘無用的文件,如舊的日誌文件。.net

重要警告!!

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

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

安裝 tmpwatch

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

在 Fedora 上:blog

$ sudo dnf install tmpwatch

在 CentOS 上:遞歸

$ sudo yum install tmpwatch

在 openSUSE 上:

$ sudo zypper install tmpwatch

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

$ sudo apt install tmpreaper

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

tmpwatchtmpreaper 的用法幾乎相同,能夠認爲兩者是同樣的命令。爲了便於描述,本文以 tmpwatch 爲例進行講解,使用基於 Debian 系統的朋友能夠將下面的 tmpwatch 改成 tmpreaper

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

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

tmpwatch 10d /var/log/
2. 刪除超過 X 天未修改的文件

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

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

tmpwatch -m 10d /var/log/

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

  • d - 天數
  • h - 小時
  • m - 分鐘
  • s - 秒數

默認時間參數是小時 。假如想刪除過去 10 個小時未修改的文件,能夠寫成下面這種形式:

tmpwatch -m 10 /var/log/
3. 刪除符號連接

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

tmpwatch -s 10 /var/log/
4. 刪除全部文件(包括常規文件,符號連接和目錄)

tmpwatch 不只僅能夠刪普通文件,還能夠刪除一些特殊文件,好比符號連接、目錄、管道文件等等。這個狀況下,須要使用 -a 選項:

tmpwatch -a 10 /var/log/
5. 刪除時排除目錄

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

tmpwatch -am 10 --nodirs /var/log/
6. 測試刪除(不實際刪除任何內容)

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

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

tmpwatch -t 30 /var/log/

CentOS 7 下輸出:

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

基於 Debian 的系統下輸出:

$ 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'.
[...]

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

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

7. 強制刪除

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

tmpwatch -f 10h /var/log/
8. 刪除時跳過某些文件

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

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

輸出結果:

(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'.

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

(偷偷地告訴你,tmpwatch/tmpreapercron job 一塊兒食用更佳哦。)

進入 cron job 任務編輯窗口:

# crontab -e

添加一個週期任務:

0 1 * * * /usr/sbin/tmpwatch 30d /var/log/

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

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

安裝 tmpreaper 時,它會自動建立一個平常 cron job(/etc/cron.daily/Tmpreaper)。它從 /etc/timereaper.conf 文件中讀取配置並執行。默認設置的是刪除 7 天之前的文件,你能夠經過修改 TMPREAPER.conf 文件中 「TMPREAPER_TIME=7d」 來更改這項設置。

寫在最後

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

tmpwatchtmpreaper 手冊頁:

$ man tmpwatch
$ man tmpreaper

-----------------

良許世界500強外企 Linux 開發工程師,Linux 佈道者,歡迎關注個人公衆號「良許Linux」,滿滿都是乾貨!

→「技術乾貨推送」

→「獨家資料共享」

→「高手如雲社羣」

若是您對個人專題內容感興趣,也能夠關注個人博客:lxlinux.net

相關文章
相關標籤/搜索