新聞中心
我們知道系統(tǒng)里面常常會(huì)有一些忘記刪除的長時(shí)間不用而且確實(shí)沒有用處的文件,如果不去處理,這些無用的文件會(huì)越來越多,浪費(fèi)許多系統(tǒng)資源。在不知道文件名的情況下,很難去檢查某一目錄下到底是哪些文件長時(shí)間沒有被訪問,同時(shí)刪除這些文件。我們今天就介紹一款非常實(shí)用的工具來完成這一工作————tmpwatch

tmpwatch 是什么
tmpwatch 能夠循環(huán)地刪除指定目錄下指定時(shí)間內(nèi)沒有被訪問的文件,這一命令常常用于清理臨時(shí)文件目錄,比如 /tmp 或者 /var/tmp 這類目錄。
它只清除指定目錄下的空目錄、普通文件和符號鏈接文件,也不會(huì)影響其他目錄,而且會(huì)避開那些屬于root用戶的系統(tǒng)相關(guān)關(guān)鍵文件的。
默認(rèn)設(shè)置下,tmpwatch 命令依據(jù)文件的 atime (access time)而非 mtime (modify time)來刪除文件。如果你想改變它的刪除依據(jù),可以在使用這個(gè)命令時(shí)加上你想修改的參數(shù)。
注意:千萬不要在根目錄底下運(yùn)行 tmpwatch 或者 tmpreaper 命令,因?yàn)橄到y(tǒng)可沒有任何機(jī)制阻止你在根目錄下運(yùn)行此命令。
重要警告!!
不要在 /(根目錄)中運(yùn)行 tmpwatch!
不要在 /(根目錄)中運(yùn)行 tmpwatch!!
不要在 /(根目錄)中運(yùn)行 tmpwatch!!!(三遍警告! ^ – ^ )
/ 目錄包含 Linux 系統(tǒng)運(yùn)行所必需的重要文件,而tmpwatch 并沒有內(nèi)置保護(hù)機(jī)制防止在/ 目錄上運(yùn)行,一旦那些重要的文件被刪除了,后果不堪設(shè)想!所以,小伙伴們在使用這個(gè)命令的時(shí)候一定要慎重!
安裝 tmpwatch
大多數(shù) Linux 發(fā)行版的默認(rèn)存儲(chǔ)庫中都提供 tmpwatch 的安裝:
在 Fedora 上:
$ sudo dnf install tmpwatch
在 CentOS 上:
$ sudo yum install tmpwatch
在 openSUSE 上:
$ sudo zypper install tmpwatch
在 Debian 及其衍生版本(如 Ubuntu )上,tmpwatch 又叫 tmpreaper:
$ sudo apt install tmpreaper
使用 tmpwatch/tmpreaper 刪除指定時(shí)間內(nèi)未訪問的文件
tmpwatch 和 tmpreaper 的用法幾乎相同,可以認(rèn)為二者是一樣的命令。為了便于描述,本文以tmpwatch 為例進(jìn)行講解,使用基于 Debian 系統(tǒng)的朋友可以將下面的 tmpwatch 改為 tmpreaper。
1. 刪除超過 X 天未訪問的文件
例:刪除 /var/log/ 文件夾中超過 10 天未訪問的所有文件和空目錄
tmpwatch 10d /var/log/
2.刪除超過 X 天未修改的文件
前文提到, tmpwatch 默認(rèn)根據(jù)訪問時(shí)間來刪除文件的,現(xiàn)在我們使用 -m 選項(xiàng)來根據(jù)文件的修改時(shí)間(modification time)來刪除文件。
例:刪除 /var/log/ 文件夾中超過 10 天未修改的文件
tmpwatch -m 10d /var/log/
上面兩個(gè)命令中的 d 是時(shí)間參數(shù),具體如下:
d – 天數(shù) h – 小時(shí) m – 分鐘 s – 秒數(shù) 默認(rèn)時(shí)間參數(shù)是小時(shí) 。假如想刪除過去 10 個(gè)小時(shí)未修改的文件,可以寫成下面這種形式:
tmpwatch -m 10 /var/log/
3. 刪除符號鏈接
可以使用 -s 選項(xiàng)刪除符號鏈接:
tmpwatch -s 10 /var/log/
4. 刪除所有文件(包括常規(guī)文件,符號鏈接和目錄)
tmpwatch 不僅僅可以刪普通文件,還可以刪除一些特殊文件,比如符號鏈接、目錄、管道文件等等。這個(gè)情況下,需要使用 -a 選項(xiàng):
tmpwatch -s 10 /var/log/
5. 刪除時(shí)排除目錄
如果不想刪除某個(gè)目錄,可以使用 –nodirs 選項(xiàng),在刪除時(shí)排除對該目錄的刪除:
tmpwatch -am 10 --nodirs /var/log/
6. 測試刪除(不實(shí)際刪除任何內(nèi)容)
這里要再次強(qiáng)調(diào),在對重要目錄進(jìn)行文件刪除時(shí),不要急著使用 tmpwatch 命令!不妨先看看命令運(yùn)行之后刪除的文件有哪些,不然刪錯(cuò)了腦殼又疼了。。(養(yǎng)成一種好習(xí)慣!)
可以使用 -t 進(jìn)入測試模式:
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 的系統(tǒng)下輸出:
$ 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'. [...]
上面這個(gè)過程,其實(shí)并沒有真正刪除文件,只是進(jìn)行模擬刪除,告知你哪些文件會(huì)被刪除。
在確保要?jiǎng)h除的文件都是正確的時(shí)候,方可去掉 -t 選項(xiàng)再執(zhí)行 tmpwatch 進(jìn)行真正刪除。
7. 強(qiáng)制刪除
tmpwatch 默認(rèn)不會(huì)刪除當(dāng)前用戶沒有寫訪問權(quán)的文件。但是如果你必須要?jiǎng)h除那些文件,可以使用-f 選項(xiàng)進(jìn)行強(qiáng)制刪除:
tmpwatch -f 10h /var/log/
8. 刪除時(shí)跳過某些文件
若想在刪除時(shí)保留指定的文件,也就是說列入白名單,可以使用 –protect 選項(xiàng)。假設(shè)我們要保留所有 txt 類型的文件:
tmpreaper --protect '*.txt' -t 10h /var/log/
輸出結(jié)果:
(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'.
設(shè)置 cron job 定期自動(dòng)刪除文件
(偷偷地告訴你,tmpwatch/tmpreaper 與 cron job 一起食用更佳哦。)
進(jìn)入 cron job 任務(wù)編輯窗口:
# crontab -e
添加一個(gè)周期任務(wù):
0 1 * * * /usr/sbin/tmpwatch 30d /var/log/
上面的代碼設(shè)置了 tmpwatch 每天凌晨 1 點(diǎn)運(yùn)行,并刪除 30 天之前的文件。
不了解 corn job 的小伙伴可以上網(wǎng)搜下它的初學(xué)者指南哈。
安裝 tmpreaper 時(shí),它會(huì)自動(dòng)創(chuàng)建一個(gè)日常 cron job(/etc/cron.daily/Tmpreaper)。它從 /etc/timereaper.conf 文件中讀取配置并執(zhí)行。默認(rèn)設(shè)置的是刪除 7 天以前的文件,你可以通過修改 TMPREAPER.conf 文件中 “TMPREAPER_TIME=7d” 來更改這項(xiàng)設(shè)置。
寫在最后
最后在提醒一下,在刪除文件的時(shí)候一定要仔細(xì)檢查好路徑,以免數(shù)據(jù)丟失。
tmpwatch 和 tmpreaper 手冊頁:
$ man tmpwatch
$ man tmpreaper
本文名稱:Linux下使用tmpwatch刪除文件
URL地址:http://m.fisionsoft.com.cn/article/dpeoohi.html


咨詢
建站咨詢
