How do I delete 100,000 tiny cache files quickly?

Quick Linux Tip #15:

Try: time find /tmp/cache -type f -name '*.tmp' -print0 | xargs -0 -n 100 -P 8 rm

Info: -print0 outputs null-separated filenames. -0 tells xargs to expect them. -n 100 batches 100 files per rm call. -P 8 runs 8 parallel processes.

Examples:

  • $ find /var/log -name '*.gz' -mtime +30 -print0 | xargs -0 -P 4 rm
  • $ find /data -name '*.raw' -print0 | xargs -0 -P 8 -I {} convert {} {}.jpg
  • $ find /tmp -type d -empty -print0 | xargs -0 -P 4 rmdir

Note: Use nproc to see CPU count. -n batching avoids "argument list too long" errors. Massive speedup vs serial deletion.




LinuxTeck.com
linuxteck@ubuntu:~$ time find /tmp/cache -type f -name '*.tmp' -print0 | xargs -0 -n 100 -P 8 rm

real    0m12.456s
user    0m3.234s
sys     0m8.567s

linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #14: How do I make /var/www/site accessible at /home/linuxteck/website too? NEXT ARTICLE Quick Linux Tip #16: Why is my repeated SSH connection so slow, and how do I fix it?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.