Removing zero byte files in Linux

Posted on December 1, 2008

I’m working on a project at work that includes several programs writing A LOT of files to disk.  If the programs can’t write to disk, they basically end up creating a 0 byte file.  When you have several programs, writing several files, you can quickly end up with a lot of empty files.  Right now, I’m trying to balance things out and have actually ended up with the partition going to 100% full.  When that happens, I get a lot of 0 byte files. At first, I was going into each directory and deleting the 0 byte files by hand – not good.  I did a little searching, to see if there was an easier way.   Thankfully, there is!  This page includes exactly what I was looking for.  Essentially, you use find to find the files and pipe them through xargs to actually remove them via rm.   Basically, to try it out, open a linux terminal window and navigate to the folder that you want to check and type: find . -type f -size 0 | xargs ls -ld

If it looks like it found what you wanted it to, change the ls -ld to rm so that rather than just listing them, it will remove them.  So type: find . -type f -size 0 | xargs rm

One last thing.  For it to work correctly, you’ll probably need to be root.  I tried it with sudo and it didn’t work.

UPDATE: Here’s an additional resource that talks about using find and xargs –

Jeff Eske

Filed Under Linux | Leave a Comment

Leave a Comment

If you would like to make a comment, please fill out the form below.

You must be logged in to post a comment.

© Copyright Jeff's Blog • Powered by Wordpress • Using Detour theme created by Brian Gardner.