如何在 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 |