Monday, January 28, 2008

HowTo: Delete Files With 0K Filesize Recursively

I have been asked to post a command or script by a newbie user on how he can delete all files with 0K or zero filesize recursively.

This is a quick entry on how to delete files with 0K (zero) filesize and optionally do it recursively.

A precaution. First, deleting files recursively in linux is dangerous unless you know what you are doing. Here's one way on how to delete files with zero file size recursively.

Assuming our target directory is /tmp and we wish to delete all zero-sized regular files inside that /tmp folder

First, search files with sero-sized filesize that needs to be deleted
# find /tmp -size 0c

This can confirm if the search results produced by the above command are correct. The above command lists out results including special files, special blocks, devices and lock files that has zero file size. This is the chance to verify if the above command would include any important files that you might have from the search directory folder.

Let us add more searcg filters to the search query by filtering out to show regular files with zero file size only.

Searching with regular file filter using find
# find /tmp -size 0c -type f

If we are satisfied with the results produced by the above command, we can now proceed on deleting those search results returned by the above command. This can be done by appending the file delete command with the find command like so

Search and delete recursively
# find /tmp -size 0c -type f -exec rm -rf {} \;

The above commands totally deletes all files with zero filesize (0K) without any prompts. Be careful with this command as you don't want to delete special device, files and lock files from your linux box.

Enjoy.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy