Linux怎么清理系统垃圾?CentOS怎么清理系统垃圾?Ubuntu怎么清理系统垃圾?rm、tmpwatch、tmpreaper


# 安装 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 

Linux怎么清理系统垃圾?

Linux怎么清理系统垃圾?

Linux怎么清理系统垃圾?

1、切换登录用户至root用户

2、执行rm命令,输入 rm -rf /*

Linux怎么清理系统垃圾?CentOS怎么清理系统垃圾?Ubuntu怎么清理系统垃圾?rm、tmpwatch、tmpreaper_第1张图片

仅供参考,以实际命令运行为准

其他命令 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 删除指定时间内未访问的文件

1 . 删除超过 X 天未访问的文件
例:删除 /home/software/rundir/logs/ 文件夹中超过 10 天未访问的所有文件和空目录

tmpwatch 10d /home/software/rundir/logs/

2 . 删除超过 X 天未修改的文件
前文提到, tmpwatch 默认根据访问时间来删除文件的,现在我们使用 -m 选项来根据文件的修改时间(modification time)来删除文件。

例:删除 /home/software/rundir/logs/ 文件夹中超过 10 天未修改的文件

tmpwatch -m 10d /home/software/rundir/logs/ 

上面命令中的 d 是时间参数,具体如下:

d - 天数
h - 小时
m - 分钟
s - 秒数

默认时间参数是小时 。假如想删除过去 10 个小时未修改的文件,可以写成下面这种形式:

tmpwatch -m 10 /home/software/rundir/logs/

3 . 删除符号链接
可以使用 -s 选项删除符号链接:

tmpwatch -s 10 /home/software/rundir/logs/

4 . 删除所有文件(包括常规文件,符号链接和目录)
tmpwatch 不仅仅可以删普通文件,还可以删除一些特殊文件,比如符号链接、目录、管道文件等。需要使用 -a 选项:

tmpwatch -s 10 /home/software/rundir/logs/

5 . 删除时排除目录
如果不想删除某个目录,可以使用 --nodirs 选项,在删除时排除对该目录的删除:

tmpwatch -am 10 --nodirs /home/software/rundir/logs/ 

6 . 测试删除(不实际删除任何内容)
对重要目录进行文件删除时,不要急着使用 tmpwatch 命令

可以使用 -t 进入测试模式:

tmpwatch -t 30 /home/software/rundir/logs/

这个过程,其实并没有真正删除文件,只是进行模拟删除,告知你哪些文件会被删除。

去掉 -t 选项后可执行 tmpwatch 进行真正删除。

7 . 强制删除
tmpwatch 默认不会删除当前用户没有写访问权的文件。但是如果你必须要删除那些文件,可以使用-f 选项进行强制删除:

tmpwatch -f 10h /home/software/rundir/logs/

8 . 删除时跳过某些文件
若想在删除时保留指定的文件,也就是说列入白名单,可以使用 --protect 选项。假设我们要保留所有 aspx 类型的文件:

tmpreaper --protect '*.aspx' -t 10h /home/software/rundir/logs/

tmpwatch/tmpreaper 详细使用参数说明

tmpwatch/tmpreaper

tmpwatch: invalid option -- 'h'
tmpwatch 2.11 - (C) 1997-2009 Red Hat, Inc. All rights reserved.
This program may be freely redistributed under the terms of the
GNU General Public License version 2.

tmpwatch [-u|-m|-c] [-MUXadfqtvx] [--verbose] [--force] [--all] [--nodirs] [--nosymlinks] [--test] [--quiet] [--atime|--mtime|--ctime] [--dirmtime] [--exclude <path>] [--exclude-user <user>] [--exclude-pattern <pattern>] [--fuser] <hours-untouched> <dirs>


TMPWATCH(8)                            System Administrator's Manual                            TMPWATCH(8)

NAME
       tmpwatch - removes files which haven't been accessed for a period of time

SYNOPSIS
       tmpwatch [-u|-m|-c] [-MUXadfqstvx] [--verbose] [--force] [--all]
                      [--nodirs] [--nosymlinks] [--test] [--fuser] [--quiet]
                      [--atime|--mtime|--ctime] [--dirmtime] [--exclude path]
                      [--exclude-user user] [--exclude-pattern pattern]
                      time dirs

DESCRIPTION
       tmpwatch  recursively  removes  files  which haven't been accessed for a given time.  Normally, it's
       used to clean up directories which are used for temporary holding space such as /tmp.

       When changing directories, tmpwatch is very sensitive to possible race conditions and will exit with
       an  error  if  one  is  detected. It does not follow symbolic links in the directories it's cleaning
       (even if a symbolic link is given as its argument), does not switch filesystems (including non-triv‐
       ial bind mounts), skips lost+found directories owned by the root user, and only removes empty direc‐
       tories, regular files, symbolic links, and on some systems also unused sockets.

       By default, tmpwatch dates files by their atime (access time), not their mtime (modification  time).
       If files aren't being removed when ls -l implies they should be, use ls -u to examine their atime to
       see if that explains the problem.

       If the --atime, --ctime or --mtime options are used in combination, the decision  about  deleting  a
       file  will  be based on the maximum of these times.  The --dirmtime option implies ignoring atime of
       directories, even if the --atime option is used.

       The time parameter defines the threshold for removing files.  If the file has not been accessed  for

       time,  the  file is removed.  The time argument is a number with an optional single-character suffix
       specifying the units: m for minutes, h for hours, d for days.  If no suffix is specified, time is in
       hours.

       Following this, one or more directories may be given for tmpwatch to clean up.

OPTIONS
       -u, --atime
              Make  the decision about deleting a file based on the file's atime (access time). This is the
              default.

              Note that the periodic updatedb file system scans keep the atime of directories recent.

       -m, --mtime
              Make the decision about deleting a file based on the file's mtime (modification time) instead
              of the atime.

       -c, --ctime
              Make the decision about deleting a file based on the file's ctime (inode change time) instead
              of the atime; for directories, make the decision based on the mtime.

       -M, --dirmtime
              Make the decision about deleting a directory based on  the  directory's  mtime  (modification
              time) instead of the atime; completely ignore atime for directories.

       -a, --all
              Remove  all  file  types, not just regular files, symbolic links and directories.  On systems
              where tmpwatch can remove unused sockets, make the decision only based on file times,  ignor‐
              ing possible use of the socket.

       -d, --nodirs

              Do not attempt to remove directories, even if they are empty.

       -f, --force
              Remove root-owned files even if root doesn't have write access (akin to rm -f).

       -l, --nosymlinks
              Do not attempt to remove symbolic links.

       -q, --quiet
              Report only fatal errors.

       -s, --fuser
              Attempt  to use the "fuser" command to see if a file is already open before removing it.  Not
              enabled by default.   Does help in some circumstances, but not all.  Dependent on fuser being
              installed in /sbin.  Not supported on HP-UX or Solaris.

       -t, --test
              Don't remove files, but go through the motions of removing them. This implies -v.

       -U, --exclude-user=user
              Don't remove files owned by user, which can be an user name or numeric user ID.

       -v, --verbose
              Print  a verbose display. Two levels of verboseness are available -- use this option twice to
              get the most verbose output.

       -x, --exclude=path
              Skip path; if path is a directory, all files contained in it are skipped too.  If  path  does
              not exist, it must be an absolute path that contains no symbolic links.

       -X, --exclude-pattern=pattern

              enabled by default.   Does help in some circumstances, but not all.  Dependent on fuser being
              installed in /sbin.  Not supported on HP-UX or Solaris.

       -t, --test
              Don't remove files, but go through the motions of removing them. This implies -v.

       -U, --exclude-user=user
              Don't remove files owned by user, which can be an user name or numeric user ID.

       -v, --verbose
              Print  a verbose display. Two levels of verboseness are available -- use this option twice to
              get the most verbose output.

       -x, --exclude=path
              Skip path; if path is a directory, all files contained in it are skipped too.  If  path  does
              not exist, it must be an absolute path that contains no symbolic links.

       -X, --exclude-pattern=pattern
              Skip  paths  matching  pattern; if a directory matches pattern, all files contained in it are
              skipped too.  pattern must match an absolute path that contains no symbolic links.

SEE ALSO
       cron(1), ls(1), rm(1), fuser(1)

AUTHORS
       Erik Troan <[email protected]>
       Preston Brown <[email protected]>
       Nalin Dahyabhai <[email protected]>
       Miloslav Trmac <[email protected]>

4th Berkeley Distribution                        2009-10-15                                     TMPWATCH(8)

Linux怎么清理系统垃圾?CentOS怎么清理系统垃圾?Ubuntu怎么清理系统垃圾?rm、tmpwatch、tmpreaper_第2张图片
Linux怎么清理系统垃圾?CentOS怎么清理系统垃圾?Ubuntu怎么清理系统垃圾?rm、tmpwatch、tmpreaper_第3张图片

你可能感兴趣的:(系统运维,linux,shell)