在Linux中使用Bash脚本删除早于 “X” 天的文件/文件夹

在Linux中使用Bash脚本删除早于 “X” 天的文件/文件夹

磁盘使用率 监控工具能够在达到给定阈值时提醒我们。但它们无法自行解决 磁盘使用率 问题。需要手动干预才能解决该问题。

如果你想完全自动化此类操作,你会做什么。是的,可以使用 bash 脚本来完成。

该脚本可防止来自 监控工具 的警报,因为我们会在填满磁盘空间之前删除旧的日志文件。

我们过去做了很多 shell 脚本。如果要查看,请进入下面的链接。

我在本文中添加了两个 bash 脚本,它们有助于清除旧日志。

1)在 Linux 中删除早于 “X” 天的文件夹的 Bash 脚本

我们有一个名为 /var/log/app/ 的文件夹,其中包含 15 天的日志,我们将删除早于 10 天的文件夹。

  1. <span class="pln">$ </span><span class="kwd">ls</span><span class="pln"> </span><span class="pun">-</span><span class="pln">lh </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span>
  2.  
  3. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">01</span>
  4. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">02</span>
  5. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">03</span>
  6. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">04</span>
  7. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">05</span>
  8. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">54</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">06</span>
  9. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">53</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">07</span>
  10. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">51</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">08</span>
  11. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">9</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">09</span>
  12. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">10</span>
  13. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">11</span>
  14. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">12</span>
  15. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">13</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">13</span>
  16. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">14</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">14</span>
  17. <span class="pln">drwxrw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">15</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> app_log</span><span class="pun">.</span><span class="lit">15</span>

该脚本将删除早于 10 天的文件夹,并通过邮件发送文件夹列表。

你可以根据需要修改 -mtime X 的值。另外,请替换你的电子邮箱,而不是用我们的。

  1. <span class="com"># /opt/</span><span class="kwd">script</span><span class="pun">/</span><span class="kwd">delete</span><span class="pun">-</span><span class="pln">old</span><span class="pun">-</span><span class="pln">folders</span><span class="pun">.</span><span class="pln">sh</span>
  2.  
  3. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
  4. <span class="pln">prev_count</span><span class="pun">=</span><span class="lit">0</span>
  5. <span class="pln">fpath</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">app_log</span><span class="pun">.*</span>
  6. <span class="kwd">find</span><span class="pln"> $fpath </span><span class="pun">-</span><span class="pln">type d </span><span class="pun">-</span><span class="pln">mtime </span><span class="pun">+</span><span class="lit">10</span><span class="pln"> </span><span class="pun">-</span><span class="kwd">exec</span><span class="pln"> </span><span class="kwd">ls</span><span class="pln"> </span><span class="pun">-</span><span class="pln">ltrh </span><span class="pun">{}</span><span class="pln"> \; </span><span class="pun">></span><span class="pln"> </span><span class="str">/tmp/</span><span class="pln">folder</span><span class="pun">.</span><span class="pln">out</span>
  7. <span class="kwd">find</span><span class="pln"> $fpath </span><span class="pun">-</span><span class="pln">type d </span><span class="pun">-</span><span class="pln">mtime </span><span class="pun">+</span><span class="lit">10</span><span class="pln"> </span><span class="pun">-</span><span class="kwd">exec</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> </span><span class="pun">-</span><span class="pln">rf </span><span class="pun">{}</span><span class="pln"> \;</span>
  8. <span class="pln">count</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">folder</span><span class="pun">.</span><span class="pln">out </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">wc</span><span class="pln"> </span><span class="pun">-</span><span class="pln">l</span><span class="pun">)</span>
  9. <span class="kwd">if</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="str">"$prev_count"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">lt </span><span class="str">"$count"</span><span class="pln"> </span><span class="pun">]</span><span class="pln"> </span><span class="pun">;</span><span class="pln"> </span><span class="kwd">then</span>
  10. <span class="pln">MESSAGE</span><span class="pun">=</span><span class="str">"/tmp/file1.out"</span>
  11. <span class="pln">TO</span><span class="pun">=</span><span class="str">"[email protected]"</span>
  12. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"Application log folders are deleted older than 15 days"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  13. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"+----------------------------------------------------+"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  14. <span class="kwd">echo</span><span class="pln"> </span><span class="str">""</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  15. <span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">folder</span><span class="pun">.</span><span class="pln">out </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $6,$7,$9}'</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  16. <span class="kwd">echo</span><span class="pln"> </span><span class="str">""</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  17. <span class="pln">SUBJECT</span><span class="pun">=</span><span class="str">"WARNING: Apache log files are deleted older than 15 days $(date)"</span>
  18. <span class="pln">mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"$SUBJECT"</span><span class="pln"> </span><span class="str">"$TO"</span><span class="pln"> </span><span class="pun"><</span><span class="pln"> $MESSAGE</span>
  19. <span class="kwd">rm</span><span class="pln"> $MESSAGE </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">folder</span><span class="pun">.</span><span class="pln">out</span>
  20. <span class="kwd">fi</span>

delete-old-folders.sh 设置可执行权限。

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">chmod</span><span class="pln"> </span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="kwd">script</span><span class="pun">/</span><span class="kwd">delete</span><span class="pun">-</span><span class="pln">old</span><span class="pun">-</span><span class="pln">folders</span><span class="pun">.</span><span class="pln">sh</span>

最后添加一个 cronjob 自动化此任务。它于每天早上 7 点运行。

  1. <span class="com">#</span><span class="pln"> crontab </span><span class="pun">-</span><span class="pln">e</span>
  2.  
  3. <span class="lit">0</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="str">/bin/</span><span class="kwd">bash</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="kwd">script</span><span class="pun">/</span><span class="kwd">delete</span><span class="pun">-</span><span class="pln">old</span><span class="pun">-</span><span class="pln">folders</span><span class="pun">.</span><span class="pln">sh</span>

你将看到类似下面的输出。

  1. <span class="typ">Application</span><span class="pln"> log folders are deleted older than </span><span class="lit">20</span><span class="pln"> days</span>
  2. <span class="pun">+--------------------------------------------------------+</span>
  3. <span class="typ">Oct</span><span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">app_log</span><span class="pun">.</span><span class="lit">11</span>
  4. <span class="typ">Oct</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">app_log</span><span class="pun">.</span><span class="lit">12</span>
  5. <span class="typ">Oct</span><span class="pln"> </span><span class="lit">13</span><span class="pln"> </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">app_log</span><span class="pun">.</span><span class="lit">13</span>
  6. <span class="typ">Oct</span><span class="pln"> </span><span class="lit">14</span><span class="pln"> </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">app_log</span><span class="pun">.</span><span class="lit">14</span>
  7. <span class="typ">Oct</span><span class="pln"> </span><span class="lit">15</span><span class="pln"> </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">app_log</span><span class="pun">.</span><span class="lit">15</span>

2)在 Linux 中删除早于 “X” 天的文件的 Bash 脚本

我们有一个名为 /var/log/apache/ 的文件夹,其中包含15天的日志,我们将删除 10 天前的文件。

以下文章与该主题相关,因此你可能有兴趣阅读。

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">ls</span><span class="pln"> </span><span class="pun">-</span><span class="pln">lh </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">apache</span><span class="pun">/</span>
  2.  
  3. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">01</span>
  4. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">02</span>
  5. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">03</span>
  6. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">04</span>
  7. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">05</span>
  8. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">54</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">06</span>
  9. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">53</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">07</span>
  10. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">51</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">08</span>
  11. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">9</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">09</span>
  12. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">10</span>
  13. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">11</span>
  14. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">12</span>
  15. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">13</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">13</span>
  16. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">14</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">14</span>
  17. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> root root </span><span class="lit">24K</span><span class="pln"> </span><span class="typ">Oct</span><span class="pln"> </span><span class="lit">15</span><span class="pln"> </span><span class="lit">23</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> </span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.</span><span class="lit">15</span>

该脚本将删除 10 天前的文件并通过邮件发送文件夹列表。

你可以根据需要修改 -mtime X 的值。另外,请替换你的电子邮箱,而不是用我们的。

  1. <span class="com"># /opt/</span><span class="kwd">script</span><span class="pun">/</span><span class="kwd">delete</span><span class="pun">-</span><span class="pln">old</span><span class="pun">-</span><span class="pln">files</span><span class="pun">.</span><span class="pln">sh</span>
  2.  
  3. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
  4. <span class="pln">prev_count</span><span class="pun">=</span><span class="lit">0</span>
  5. <span class="pln">fpath</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">apache</span><span class="pun">/</span><span class="lit">2daygeek</span><span class="pln">_access</span><span class="pun">.*</span>
  6. <span class="kwd">find</span><span class="pln"> $fpath </span><span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">mtime </span><span class="pun">+</span><span class="lit">15</span><span class="pln"> </span><span class="pun">-</span><span class="kwd">exec</span><span class="pln"> </span><span class="kwd">ls</span><span class="pln"> </span><span class="pun">-</span><span class="pln">ltrd </span><span class="pun">{}</span><span class="pln"> \; </span><span class="pun">></span><span class="pln"> </span><span class="str">/tmp/</span><span class="kwd">file</span><span class="pun">.</span><span class="pln">out</span>
  7. <span class="kwd">find</span><span class="pln"> $fpath </span><span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">mtime </span><span class="pun">+</span><span class="lit">15</span><span class="pln"> </span><span class="pun">-</span><span class="kwd">exec</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> </span><span class="pun">-</span><span class="pln">rf </span><span class="pun">{}</span><span class="pln"> \;</span>
  8. <span class="pln">count</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="kwd">file</span><span class="pun">.</span><span class="pln">out </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">wc</span><span class="pln"> </span><span class="pun">-</span><span class="pln">l</span><span class="pun">)</span>
  9. <span class="kwd">if</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="str">"$prev_count"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">lt </span><span class="str">"$count"</span><span class="pln"> </span><span class="pun">]</span><span class="pln"> </span><span class="pun">;</span><span class="pln"> </span><span class="kwd">then</span>
  10. <span class="pln">MESSAGE</span><span class="pun">=</span><span class="str">"/tmp/file1.out"</span>
  11. <span class="pln">TO</span><span class="pun">=</span><span class="str">"[email protected]"</span>
  12. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"Apache Access log files are deleted older than 20 days"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  13. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"+--------------------------------------------- +"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  14. <span class="kwd">echo</span><span class="pln"> </span><span class="str">""</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  15. <span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="kwd">file</span><span class="pun">.</span><span class="pln">out </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $6,$7,$9}'</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  16. <span class="kwd">echo</span><span class="pln"> </span><span class="str">""</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  17. <span class="pln">SUBJECT</span><span class="pun">=</span><span class="str">"WARNING: Apache log folders are deleted older than 15 days $(date)"</span>
  18. <span class="pln">mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"$SUBJECT"</span><span class="pln"> </span><span class="str">"$TO"</span><span class="pln"> </span><span class="pun"><</span><span class="pln"> $MESSAGE</span>
  19. <span class="kwd">rm</span><span class="pln"> $MESSAGE </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="kwd">file</span><span class="pun">.</span><span class="pln">out</span>
  20. <span class="kwd">fi</span>

delete-old-files.sh 设置可执行权限。

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">chmod</span><span class="pln"> </span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="kwd">script</span><span class="pun">/</span><span class="kwd">delete</span><span class="pun">-</span><span class="pln">old</span><span class="pun">-</span><span class="pln">files</span><span class="pun">.</span><span class="pln">sh</span>

最后添加一个 cronjob 自动化此任务。它于每天早上 7 点运行。

  1. <span class="com">#</span><span class="pln"> crontab </span><span class="pun">-</span><span class=&q

相关推荐