<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>katsube</author_name>
  <author_url>https://blog.hatena.ne.jp/katsube/</author_url>
  <blog_title>ねこの足跡R</blog_title>
  <blog_url>https://blog.katsubemakito.net/</blog_url>
  <categories>
    <anon>bash</anon>
    <anon>Linux</anon>
    <anon>rm</anon>
    <anon>tail</anon>
    <anon>ls</anon>
    <anon>シェル</anon>
  </categories>
  <description>日次バッチなどを作っていると、例えばバックアップファイルは最新30件だけ保存し、それ以上古いファイルは削除したいといった気持ちになりますよね。そんな時に利用するのが次のワンライナー。 $ cd backup/ $ rm -f `ls -t . | tail -n+31` これだと30日前の日付を計算して毎回削除するといった処理を書かなくて済みます。厳密にn日分確保する必要がある場合は日付の計算を行った方が良いでしょうが、念の為一定期間取っておきたいくらいのゆるい要件であれば十分でしょう。 保存しておきたいファイル数に「+1」することをお忘れなく。 ディレクトリを削除する場合はrmに-rをつけて…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Fblog.katsubemakito.net%2Fshell%2Fbash-rm-over-files&quot; title=&quot; [Linux] Bashで一定件数以上のファイルを削除する - ねこの足跡R&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url></image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2020-04-10 18:29:48</published>
  <title> [Linux] Bashで一定件数以上のファイルを削除する</title>
  <type>rich</type>
  <url>https://blog.katsubemakito.net/shell/bash-rm-over-files</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
