温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么删除Linux中的垃圾文件

发布时间:2021-10-26 14:38:51 来源:亿速云 阅读:199 作者:小新 栏目:建站服务器

小编给大家分享一下怎么删除Linux中的垃圾文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

当然,我们也可以使用 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 删除指定时间内未访问的文件

tmpwatch 和 tmpreaper 的用法几乎相同,可以认为二者是一样的命令。为了便于描述,本文以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 -s 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/tmpreaper 与 cron 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” 来更改这项设置。

写在最后

最后在提醒一下,在删除文件的时候一定要仔细检查好路径,以免数据丢失。

tmpwatch 和 tmpreaper 手册页:

$ man tmpwatch 
$ man tmpreaper

以上是“怎么删除Linux中的垃圾文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI