小编给大家分享一下在Linux中如何使用Bash脚本删除早于 “X” 天的文件/文件夹,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
1)在 Linux 中删除早于 “X” 天的文件夹的 Bash 脚本
我们有一个名为 /var/log/app/
的文件夹,其中包含 15 天的日志,我们将删除早于 10 天的文件夹。
$ ls -lh /var/log/app/ drwxrw-rw- 3 root root 24K Oct 1 23:52 app_log.01drwxrw-rw- 3 root root 24K Oct 2 23:52 app_log.02drwxrw-rw- 3 root root 24K Oct 3 23:52 app_log.03drwxrw-rw- 3 root root 24K Oct 4 23:52 app_log.04drwxrw-rw- 3 root root 24K Oct 5 23:52 app_log.05drwxrw-rw- 3 root root 24K Oct 6 23:54 app_log.06drwxrw-rw- 3 root root 24K Oct 7 23:53 app_log.07drwxrw-rw- 3 root root 24K Oct 8 23:51 app_log.08drwxrw-rw- 3 root root 24K Oct 9 23:52 app_log.09drwxrw-rw- 3 root root 24K Oct 10 23:52 app_log.10drwxrw-rw- 3 root root 24K Oct 11 23:52 app_log.11drwxrw-rw- 3 root root 24K Oct 12 23:52 app_log.12drwxrw-rw- 3 root root 24K Oct 13 23:52 app_log.13drwxrw-rw- 3 root root 24K Oct 14 23:52 app_log.14drwxrw-rw- 3 root root 24K Oct 15 23:52 app_log.15
该脚本将删除早于 10 天的文件夹,并通过邮件发送文件夹列表。
你可以根据需要修改 -mtime X
的值。另外,请替换你的电子邮箱,而不是用我们的。
# /opt/script/delete-old-folders.sh #!/bin/bashprev_count=0fpath=/var/log/app/app_log.*find $fpath -type d -mtime +10 -exec ls -ltrh {} \; > /tmp/folder.outfind $fpath -type d -mtime +10 -exec rm -rf {} \;count=$(cat /tmp/folder.out | wc -l)if [ "$prev_count" -lt "$count" ] ; thenMESSAGE="/tmp/file1.out"TO="[email protected]"echo "Application log folders are deleted older than 15 days" >> $MESSAGEecho "+----------------------------------------------------+" >> $MESSAGEecho "" >> $MESSAGEcat /tmp/folder.out | awk '{print $6,$7,$9}' >> $MESSAGEecho "" >> $MESSAGESUBJECT="WARNING: Apache log files are deleted older than 15 days $(date)"mail -s "$SUBJECT" "$TO" < $MESSAGErm $MESSAGE /tmp/folder.outfi
给 delete-old-folders.sh
设置可执行权限。
# chmod +x /opt/script/delete-old-folders.sh
最后添加一个 cronjob 自动化此任务。它于每天早上 7 点运行。
# crontab -e 0 7 * * * /bin/bash /opt/script/delete-old-folders.sh
你将看到类似下面的输出。
Application log folders are deleted older than 20 days+--------------------------------------------------------+Oct 11 /var/log/app/app_log.11Oct 12 /var/log/app/app_log.12Oct 13 /var/log/app/app_log.13Oct 14 /var/log/app/app_log.14Oct 15 /var/log/app/app_log.15
2)在 Linux 中删除早于 “X” 天的文件的 Bash 脚本
我们有一个名为 /var/log/apache/
的文件夹,其中包含15天的日志,我们将删除 10 天前的文件。
# ls -lh /var/log/apache/ -rw-rw-rw- 3 root root 24K Oct 1 23:52 2daygeek_access.01-rw-rw-rw- 3 root root 24K Oct 2 23:52 2daygeek_access.02-rw-rw-rw- 3 root root 24K Oct 3 23:52 2daygeek_access.03-rw-rw-rw- 3 root root 24K Oct 4 23:52 2daygeek_access.04-rw-rw-rw- 3 root root 24K Oct 5 23:52 2daygeek_access.05-rw-rw-rw- 3 root root 24K Oct 6 23:54 2daygeek_access.06-rw-rw-rw- 3 root root 24K Oct 7 23:53 2daygeek_access.07-rw-rw-rw- 3 root root 24K Oct 8 23:51 2daygeek_access.08-rw-rw-rw- 3 root root 24K Oct 9 23:52 2daygeek_access.09-rw-rw-rw- 3 root root 24K Oct 10 23:52 2daygeek_access.10-rw-rw-rw- 3 root root 24K Oct 11 23:52 2daygeek_access.11-rw-rw-rw- 3 root root 24K Oct 12 23:52 2daygeek_access.12-rw-rw-rw- 3 root root 24K Oct 13 23:52 2daygeek_access.13-rw-rw-rw- 3 root root 24K Oct 14 23:52 2daygeek_access.14-rw-rw-rw- 3 root root 24K Oct 15 23:52 2daygeek_access.15
你可以根据需要修改 -mtime X
的值。另外,请替换你的电子邮箱,而不是用我们的。
# /opt/script/delete-old-files.sh #!/bin/bashprev_count=0fpath=/var/log/apache/2daygeek_access.*find $fpath -type f -mtime +15 -exec ls -ltrd {} \; > /tmp/file.outfind $fpath -type f -mtime +15 -exec rm -rf {} \;count=$(cat /tmp/file.out | wc -l)if [ "$prev_count" -lt "$count" ] ; thenMESSAGE="/tmp/file1.out"TO="[email protected]"echo "Apache Access log files are deleted older than 20 days" >> $MESSAGEecho "+--------------------------------------------- +" >> $MESSAGEecho "" >> $MESSAGEcat /tmp/file.out | awk '{print $6,$7,$9}' >> $MESSAGEecho "" >> $MESSAGESUBJECT="WARNING: Apache log folders are deleted older than 15 days $(date)"mail -s "$SUBJECT" "$TO" < $MESSAGErm $MESSAGE /tmp/file.outfi
给 delete-old-files.sh
设置可执行权限。
# chmod +x /opt/script/delete-old-files.sh
最后添加一个 cronjob 自动化此任务。它于每天早上 7 点运行。
# crontab -e 0 7 * * * /bin/bash /opt/script/delete-old-folders.sh
你将看到类似下面的输出。
Apache Access log files are deleted older than 20 days+--------------------------------------------------------+Oct 11 /var/log/apache/2daygeek_access.11Oct 12 /var/log/apache/2daygeek_access.12Oct 13 /var/log/apache/2daygeek_access.13Oct 14 /var/log/apache/2daygeek_access.14Oct 15 /var/log/apache/2daygeek_access.15
以上是“在Linux中如何使用Bash脚本删除早于 “X” 天的文件/文件夹”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程之家行业资讯频道!