How I Use Find and Xargs to Delete Temp Files Older Than 7 Days Without Risking rm ‑rf

Cleaning up old temp files is a daily chore that can quickly become dangerous if you use the wrong tool.
A single misplaced rm -rf can wipe out a whole directory tree, and that risk grows when you’re running scripts as root.
The combination of find and xargs (or -delete) gives you a precise, race‑condition‑aware way to target only the files you really want gone.

Why find + xargs beats a blind rm -rf

  • Scope control – find lets you restrict the search to a specific path, depth, file type, age, size, or owner.
  • Safety – -delete removes only the matched files; it never walks into a directory you didn’t ask for.
  • Speed – xargs batches deletions, reducing the number of rm invocations.
  • Robustness – -print0 + xargs -0 handles filenames with spaces, newlines, or other odd characters.

A minimal, production‑ready command

# Delete regular files in /tmp older than 7 days
find /tmp -type f -mtime +7 -print0 | xargs -0 rm -v
  • -type f skips directories and symlinks.
  • -mtime +7 matches files whose modification time is more than seven days ago.
  • -print0 outputs a null‑terminated list; xargs -0 consumes it safely.
  • -v gives you a quick audit trail.

If you prefer a single‑step solution and your find supports it, replace the pipe with -delete:

[Read More]

Taming Recursive Greps with find and xargs in My Homelab Log Cleanup Scripts

Introduction to Log Cleanup

I’ve been dealing with a growing amount of log data in my homelab, and it’s getting out of hand. Logs are crucial for debugging and monitoring, but they can quickly consume disk space and make it hard to find what you need. I’ve been experimenting with ways to improve my log cleanup scripts, and I’ve refined my approach using find and xargs.

Understanding the Problem

Recursive greps can be slow and inefficient, especially with large log files. A simple grep command can take a long time to execute, and it may not even find what you’re looking for. When you’re trying to clean up logs, you often need to perform actions like deleting or compressing files, which grep alone can’t handle. That’s where find and xargs come in – two powerful commands that can help you manage your logs more effectively.

[Read More]

Reclaiming Disk Space with Find and xargs After a Package Manager Mishap

Introduction to Disk Space Reclamation

I’ve lost count of how many times I’ve run into issues with package managers leaving behind unnecessary files on my Linux systems. This can happen due to incomplete package removals, failed updates, or simply because the package manager didn’t clean up after itself. In my experience, this is more common than you’d think, especially when using less popular packages or experimenting with different distributions.

Identifying the Problem

To start reclaiming disk space, you first need to identify where the unnecessary files are located. A good starting point is to use the find command, which allows you to search for files based on various criteria such as size, modification time, and permissions. For example, to find all files larger than 100MB in the /usr directory, you can use the following command:

[Read More]