如何在 Linux 中自动删除 /tmp 文件夹? 自动磁盘日志清理 Bash 脚本
已发表: 2014-03-13
这是一个简单的脚本,它将为任何 Linux 环境执行自动磁盘日志清理。 您只需提供正确的 CRUNCHIFY_TMP_DIRS 。 当磁盘没有剩余空间时,就会出现各种问题。
但在此之前,让我们对一些重要的命令有一些基本的了解。
第1步)
检查 df -H 命令。
使用df
命令显示有关文件系统上的总空间和可用空间的信息。
FileSystem 参数指定文件系统所在设备的名称、文件系统安装的目录或文件系统的相对路径名。
如果不指定 FileSystem 参数,则df command
显示所有当前安装的文件系统的信息。 如果指定了文件或目录,则 df 命令显示其所在文件系统的信息。
输出:
1 2 3 |
Filesystem Size Used Avail Capacity iused ifree % iused Mounted on / dev / disk0s2 499G 114G 385G 23 % 27868719 94059510 23 % / devfs 189k 189k 0B 100 % 640 0 100 % / dev |
第2步)
接下来过滤掉文件系统并找出空间百分比
1 |
df - H | grep - vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' |
输出:
1 2 |
23 % / dev / disk0s2 100 % devfs |
因此,有时您可能希望以编程方式清理特定文件夹中的文件,以防空间不足。 在这种情况下,您只需要执行以下脚本,它将根据脚本中提到的过滤条件清理所有未使用的文件。 它还会向脚本中指定的用户发送一封电子邮件。
另一个必须阅读:
- 如何在 JAVA 中运行 Windows/Mac 命令并返回文本结果
完整的 Linux DiskCleanup 脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#!/bin/bash # Diskclean-Linux.sh - Remove unused files from /tmp directories # @author: Crunchify.com # ------------- Here are Default Configuration -------------------- # CRUNCHIFY_TMP_DIRS - List of directories to search # DEFAULT_FILE_AGE - # days ago (rounded up) that file was last accessed # DEFAULT_LINK_AGE - # days ago (rounded up) that symlink was last accessed # DEFAULT_SOCK_AGE - # days ago (rounded up) that socket was last accessed CRUNCHIFY_TMP_DIRS = "/tmp /var/tmp /usr/src/tmp /mnt/tmp" DEFAULT_FILE_AGE =+ 2 DEFAULT_LINK_AGE =+ 2 DEFAULT_SOCK_AGE =+ 2 # Make EMPTYFILES true to delete zero-length files EMPTYFILES = false #EMPTYFILES=true cd / tmp / log "cleantmp.sh[$] - Begin cleaning tmp directories" echo "" echo "delete any tmp files that are more than 2 days old" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth \ - type f - a - ctime $ DEFAULT_FILE_AGE \ - print - delete echo "" echo "delete any old tmp symlinks" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth \ - type l - a - ctime $ DEFAULT_LINK_AGE \ - print - delete echo "" if / usr / bin / $ EMPTYFILES ; then echo "delete any empty files" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth \ - type f - a - empty \ - print - delete fi echo "Delete any old Unix sockets" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth \ - type s - a - ctime $ DEFAULT_SOCK_AGE - a - size 0 \ - print - delete echo "" echo "delete any empty directories (other than lost+found)" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth - mindepth 1 \ - type d - a - empty - a ! - name 'lost+found' \ - print - delete echo "" / usr / bin / logger "cleantmp.sh[$] - Done cleaning tmp directories" # send out an email about diskcleanup action mail - s "Disk cleanup has been performed successfully." you @ email .com echo "" echo "Diskcleanup Script Successfully Executed" exit 0 |
希望这可以帮助。 如果您对上述脚本有任何疑问,请告诉我。 任何建议都非常受欢迎。 该脚本适用于 Linux 和 Mac OS X。

想要every 3 days
运行一次以上脚本吗? 只需使用以下 cron 计划
有关Setting up CronJobs
的详细教程即将发布。 敬请期待。
1 |
0 0 */ 3 * * / opt / crunchify / crunchify_script . sh |