在Linux中使用Bash脚本删除早于 “X” 天的文件/文件夹
磁盘使用率 监控工具能够在达到给定阈值时提醒我们。但它们无法自行解决 磁盘使用率 问题。需要手动干预才能解决该问题。
如果你想完全自动化此类操作,你会做什么。是的,可以使用 bash 脚本来完成。
该脚本可防止来自 监控工具 的警报,因为我们会在填满磁盘空间之前删除旧的日志文件。
我们过去做了很多 shell 脚本。如果要查看,请进入下面的链接。
我在本文中添加了两个 bash 脚本,它们有助于清除旧日志。
1)在 Linux 中删除早于 “X” 天的文件夹的 Bash 脚本
我们有一个名为 /var/log/app/
的文件夹,其中包含 15 天的日志,我们将删除早于 10 天的文件夹。
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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
的值。另外,请替换你的电子邮箱,而不是用我们的。
<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>
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
<span class="pln">prev_count</span><span class="pun">=</span><span class="lit">0</span>
<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>
<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>
<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>
<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>
<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>
<span class="pln">MESSAGE</span><span class="pun">=</span><span class="str">"/tmp/file1.out"</span>
<span class="pln">TO</span><span class="pun">=</span><span class="str">"[email protected]"</span>
<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>
<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>
<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>
<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>
<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>
<span class="pln">SUBJECT</span><span class="pun">=</span><span class="str">"WARNING: Apache log files are deleted older than 15 days $(date)"</span>
<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>
<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>
<span class="kwd">fi</span>
给 delete-old-folders.sh
设置可执行权限。
<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 点运行。
<span class="com">#</span><span class="pln"> crontab </span><span class="pun">-</span><span class="pln">e</span>
<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>
你将看到类似下面的输出。
<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>
<span class="pun">+--------------------------------------------------------+</span>
<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>
<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>
<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>
<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>
<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 天前的文件。
以下文章与该主题相关,因此你可能有兴趣阅读。
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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
的值。另外,请替换你的电子邮箱,而不是用我们的。
<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>
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
<span class="pln">prev_count</span><span class="pun">=</span><span class="lit">0</span>
<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>
<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>
<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>
<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>
<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>
<span class="pln">MESSAGE</span><span class="pun">=</span><span class="str">"/tmp/file1.out"</span>
<span class="pln">TO</span><span class="pun">=</span><span class="str">"[email protected]"</span>
<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>
<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>
<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>
<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>
<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>
<span class="pln">SUBJECT</span><span class="pun">=</span><span class="str">"WARNING: Apache log folders are deleted older than 15 days $(date)"</span>
<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>
<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>
<span class="kwd">fi</span>
给 delete-old-files.sh
设置可执行权限。
<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 点运行。
<span class="com">#</span><span class="pln"> crontab </span><span class="pun">-</span><span class=&q
相关推荐
applecarelte 2020-10-16
touchfuture 2020-09-16
fsfsdfsdw 2020-04-22
Testingba工作室 2020-09-15
TuxedoLinux 2020-09-11
guying 2020-08-16
haitianisgood 2020-07-21
luobotoutou 2020-07-20
luvzhan 2020-06-28
Jieen 2020-06-28
renshuguo 2020-06-24
cutdeer 2020-06-17
lishaokang 2020-06-08
DBATips 2020-06-07
xiaobater 2020-05-26
木子李CSDN 2020-05-18
死鱼喜欢猫 2020-05-15