偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何優(yōu)雅地刪除Linux中的垃圾文件

系統(tǒng) Linux
下面要介紹的是今天的主角—— tmpwatch ,它能幫助我們遞歸刪除在給定時間內(nèi)沒有訪問的文件和空目錄。

[[320981]]

不知道大家是否也跟我一樣,是一只要把的自己電腦文件安排的條理有序,把沒用的文件會及時刪掉的程序猿呢?如果是的話,那么我們可以愉快地探討下文章的內(nèi)容。如果不是的話,你也可以留下來湊湊熱鬧嘛(>-<)。

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

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

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

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

重要警告!!

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

不要在 /(根目錄)中運行 tmpwatch?。?nbsp; 

不要在 /(根目錄)中運行 tmpwatch?。。。ㄈ榫妫?^ - ^ )

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

安裝 tmpwatch

大多數(shù) Linux 發(fā)行版的默認存儲庫中都提供 tmpwatch 的安裝:

在 Fedora 上: 

  1. $ sudo dnf install tmpwatch 

在 CentOS 上: 

  1. $ sudo yum install tmpwatch 

在 openSUSE 上: 

  1. $ sudo zypper install tmpwatch 

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

  1. $ sudo apt install tmpreaper 

使用 tmpwatch/tmpreaper 刪除指定時間內(nèi)未訪問的文件

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

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

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

  1. tmpwatch 10d /var/log/ 

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

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

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

  1. tmpwatch -m 10d /var/log/ 

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

  •  d - 天數(shù)
  •  h - 小時
  •  m - 分鐘
  •  s - 秒數(shù)

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

  1. tmpwatch -m 10 /var/log/ 

3. 刪除符號鏈接

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

  1. tmpwatch -s 10 /var/log/ 

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

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

  1. tmpwatch -a 10 /var/log/ 

5. 刪除時排除目錄

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

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

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

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

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

  1. tmpwatch -t 30 /var/log/ 

CentOS 7 下輸出: 

  1. removing file /var/log/wtmp  
  2. removing directory /var/log/ppp if empty  
  3. removing directory /var/log/tuned if empty  
  4. removing directory /var/log/anaconda if empty  
  5. removing file /var/log/dmesg.old  
  6. removing file /var/log/boot.log  
  7. removing file /var/log/dnf.librepo.log 

基于 Debian 的系統(tǒng)下輸出: 

  1. $ tmpreaper -t 30 /var/log/  
  2. (PID 1803) Pretending to clean up directory `/var/log/'.  
  3. (PID 1804) Pretending to clean up directory `apache2'. 
  4.  Pretending to remove file `apache2/error.log'.  
  5. Pretending to remove file `apache2/access.log'.  
  6. Pretending to remove file `apache2/other_vhosts_access.log'.  
  7. (PID 1804) Back from recursing down `apache2'.  
  8. (PID 1804) Pretending to clean up directory `dbconfig-common'.  
  9. Pretending to remove file `dbconfig-common/dbc.log'.  
  10. (PID 1804) Back from recursing down `dbconfig-common'.  
  11. (PID 1804) Pretending to clean up directory `dist-upgrade'.  
  12. (PID 1804) Back from recursing down `dist-upgrade'.  
  13. (PID 1804) Pretending to clean up directory `lxd'.  
  14. (PID 1804) Back from recursing down `lxd'.  
  15. Pretending to remove file `/var/log//cloud-init.log'.  
  16. (PID 1804) Pretending to clean up directory `landscape'.  
  17. Pretending to remove file `landscape/sysinfo.log'.  
  18. (PID 1804) Back from recursing down `landscape'.  
  19. [...] 

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

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

7. 強制刪除

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

  1. tmpwatch -f 10h /var/log/ 

8. 刪除時跳過某些文件

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

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

輸出結(jié)果: 

  1. (PID 2623) Pretending to clean up directory `/var/log/'.  
  2. (PID 2624) Pretending to clean up directory `apache2'.  
  3. Pretending to remove file `apache2/error.log'.  
  4. Pretending to remove file `apache2/access.log'.  
  5. Pretending to remove file `apache2/other_vhosts_access.log'.  
  6. (PID 2624) Back from recursing down `apache2'.  
  7. (PID 2624) Pretending to clean up directory `dbconfig-common'.  
  8. Pretending to remove file `dbconfig-common/dbc.log'.  
  9. (PID 2624) Back from recursing down `dbconfig-common'.  
  10. (PID 2624) Pretending to clean up directory `dist-upgrade'.  
  11. (PID 2624) Back from recursing down `dist-upgrade'.  
  12. Pretending to remove empty directory `dist-upgrade'.  
  13. Entry matching `--protect' pattern skipped. `ostechnix.txt'  
  14. (PID 2624) Pretending to clean up directory `lxd'. 

設(shè)置 cron job 定期自動刪除文件

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

進入 cron job 任務(wù)編輯窗口: 

  1. # crontab -e 

添加一個周期任務(wù): 

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

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

不了解 corn job 的小伙伴可以上網(wǎng)搜下它的初學(xué)者指南哈。

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

寫在最后

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

tmpwatch 和 tmpreaper 手冊頁: 

  1. $ man tmpwatch  
  2. $ man tmpreaper  

 

責(zé)任編輯:龐桂玉 來源: Linux學(xué)習(xí)
相關(guān)推薦

2020-02-24 11:12:01

Linux電腦數(shù)據(jù)

2009-08-21 19:10:53

Windows 7系統(tǒng)垃圾清理

2020-03-26 11:04:00

Linux命令光標(biāo)

2021-09-14 14:02:40

手機內(nèi)存技術(shù)

2012-05-02 16:09:13

系統(tǒng)垃圾

2021-03-24 10:20:50

Fonts前端代碼

2024-11-21 09:00:00

Python字典代碼

2022-05-24 06:07:48

JShack用戶代碼

2019-03-18 09:40:44

Linux命令編程語言

2020-09-25 11:30:20

Java判空代碼

2020-04-10 10:22:12

Java判空編程語言

2024-11-13 16:37:00

Java線程池

2019-04-11 08:00:00

Windows刪除文件

2022-04-20 20:27:51

Hydra配置文件開發(fā)工具

2023-05-12 14:14:00

Java線程中斷

2023-12-20 13:50:00

SpringBootJSON序列化

2022-03-20 16:36:41

微軟VSCodeBug

2023-12-11 18:20:21

Vue.js事件機制傳遞

2020-03-27 15:10:23

SpringJava框架

2022-03-28 19:53:24

Linux恢復(fù)文件意外刪除文件
點贊
收藏

51CTO技術(shù)棧公眾號