Removing zero byte files in Linux

Linux — Jeff Eske on December 1, 2008 at 5:08 pm

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


No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. | Jeff's Blog