<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Cleanup on Linux Café</title>
    <link>https://mrtomlinux.org/tags/cleanup/</link>
    <description>Recent content in Cleanup on Linux Café</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Mon, 05 Oct 2026 08:08:05 +0200</lastBuildDate>
    <atom:link href="https://mrtomlinux.org/tags/cleanup/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>How I Use Find and Xargs to Delete Temp Files Older Than 7 Days Without Risking rm ‑rf</title>
      <link>https://mrtomlinux.org/post/2026-10-05-how-i-use-find-and-xargs-to-delete-temp-files/</link>
      <pubDate>Mon, 05 Oct 2026 08:08:05 +0200</pubDate>
      <guid>https://mrtomlinux.org/post/2026-10-05-how-i-use-find-and-xargs-to-delete-temp-files/</guid>
      <description>&lt;p&gt;Cleaning up old temp files is a daily chore that can quickly become dangerous if you use the wrong tool.&lt;br&gt;&#xA;A single misplaced &lt;code&gt;rm -rf&lt;/code&gt; can wipe out a whole directory tree, and that risk grows when you’re running scripts as root.&lt;br&gt;&#xA;The combination of &lt;code&gt;find&lt;/code&gt; and &lt;code&gt;xargs&lt;/code&gt; (or &lt;code&gt;-delete&lt;/code&gt;) gives you a precise, race‑condition‑aware way to target only the files you really want gone.&lt;/p&gt;&#xA;&lt;h2 id=&#34;why-find--xargs-beats-a-blind-rm--rf&#34;&gt;Why &lt;code&gt;find&lt;/code&gt; + &lt;code&gt;xargs&lt;/code&gt; beats a blind &lt;code&gt;rm -rf&lt;/code&gt;&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Scope control&lt;/strong&gt; – &lt;code&gt;find&lt;/code&gt; lets you restrict the search to a specific path, depth, file type, age, size, or owner.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Safety&lt;/strong&gt; – &lt;code&gt;-delete&lt;/code&gt; removes only the matched files; it never walks into a directory you didn’t ask for.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Speed&lt;/strong&gt; – &lt;code&gt;xargs&lt;/code&gt; batches deletions, reducing the number of &lt;code&gt;rm&lt;/code&gt; invocations.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Robustness&lt;/strong&gt; – &lt;code&gt;-print0&lt;/code&gt; + &lt;code&gt;xargs -0&lt;/code&gt; handles filenames with spaces, newlines, or other odd characters.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;a-minimal-productionready-command&#34;&gt;A minimal, production‑ready command&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Delete regular files in /tmp older than 7 days&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;find /tmp -type f -mtime +7 -print0 &lt;span class=&#34;p&#34;&gt;|&lt;/span&gt; xargs -0 rm -v&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;-type f&lt;/code&gt; skips directories and symlinks.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;-mtime +7&lt;/code&gt; matches files whose modification time is more than seven days ago.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;-print0&lt;/code&gt; outputs a null‑terminated list; &lt;code&gt;xargs -0&lt;/code&gt; consumes it safely.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;-v&lt;/code&gt; gives you a quick audit trail.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;If you prefer a single‑step solution and your &lt;code&gt;find&lt;/code&gt; supports it, replace the pipe with &lt;code&gt;-delete&lt;/code&gt;:&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
