Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Bash script in Linux to delete files / folders earlier than "X" days

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how to use Bash script in Linux to delete files/folders earlier than "X" days, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

1) Bash script to delete folders older than "X" days in Linux

We have a folder named/var/log/app/that contains logs for 15 days, and we will delete folders older than 10 days.

$ 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

The script deletes folders older than 10 days and sends a list of folders by mail.

You can change the value of-mtime X as needed. Also, please replace your email address with ours.

# /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.152)在 Linux 中删除早于 "X" 天的文件的 Bash 脚本 我们有一个名为 /var/log/apache/ 的文件夹,其中包含15天的日志,我们将删除 10 天前的文件。 以下文章与该主题相关,因此你可能有兴趣阅读。 如何在 Linux 中查找和删除早于 "X" 天和 "X" 小时的文件? 如何在 Linux 中查找最近修改的文件/文件夹 如何在 Linux 中自动删除或清理 /tmp 文件夹内容? # 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 该脚本将删除 10 天前的文件并通过邮件发送文件夹列表。 你可以根据需要修改 -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

Set executable permissions for delete-old-files.sh

# chmod +x /opt/script/delete-old-files.sh

Finally add a cronjob to automate this task. It runs every morning at 7:00.

# crontab -e 0 7 * * * /bin/bash /opt/script/delete-old-folders.sh

You will see output similar to the one below.

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 The above is "How to delete files/folders older than"X"days in Linux using Bash scripts" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report