• 如何优雅地删除 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 上:

    $ 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

  • 相关阅读:
    QQ机器人
    Javascript实现base64的加密解密
    C# 对List<T>取交集、连集及差集
    简单正则验证
    中止线程
    线程同步synchronized
    volatile
    并发
    垃圾回收机制
    给定一个正整数num ,反复将各个位上的数字相加,直到结果为一位数
  • 原文地址:https://www.cnblogs.com/yychuyu/p/12418334.html
Copyright © 2020-2023  润新知