怎么刪除Linux中的垃圾文件-創(chuàng)新互聯(lián)

小編給大家分享一下怎么刪除Linux中的垃圾文件,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)專注骨干網(wǎng)絡(luò)服務(wù)器租用十載,服務(wù)更有保障!服務(wù)器租用,成都移動(dòng)服務(wù)器托管 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實(shí)現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。

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

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

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

重要警告!!

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

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

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

/ 目錄包含  Linux 系統(tǒng)運(yùn)行所必需的重要文件,而tmpwatch 并沒有內(nèi)置保護(hù)機(jī)制防止在/ 目錄上運(yùn)行,一旦那些重要的文件被刪除了,后果不堪設(shè)想!所以,小伙伴們?cè)谑褂眠@個(gè) 命令的時(shí)候一定要慎重!

安裝 tmpwatch

大多數(shù) Linux 發(fā)行版的默認(rèn)存儲(chǔ)庫(kù)中都提供 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. 刪除符號(hào)鏈接

可以使用 -s 選項(xiàng)刪除符號(hào)鏈接:

tmpwatch -s 10 /var/log/

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

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

tmpwatch -s 10 /var/log/

5. 刪除時(shí)排除目錄

如果不想刪除某個(gè)目錄,可以使用 --nodirs 選項(xiàng),在刪除時(shí)排除對(duì)該目錄的刪除:

tmpwatch -am 10 --nodirs /var/log/

6. 測(cè)試刪除(不實(shí)際刪除任何內(nèi)容)

這里要再次強(qiáng)調(diào),在對(duì)重要目錄進(jìn)行文件刪除時(shí),不要急著使用 tmpwatch 命令!不妨先看看命令運(yùn)行之后刪除的文件有哪些,不然刪錯(cuò)了腦殼又疼了。。(養(yǎng)成一種好習(xí)慣!)

可以使用 -t 進(jìn)入測(cè)試模式:

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 手冊(cè)頁:

$ man tmpwatch 
$ man tmpreaper

以上是“怎么刪除Linux中的垃圾文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

本文題目:怎么刪除Linux中的垃圾文件-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://bm7419.com/article18/gehgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、搜索引擎優(yōu)化、自適應(yīng)網(wǎng)站、網(wǎng)站維護(hù)域名注冊(cè)、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)