Unix / Linux: Find and Delete Zero Byte Files

Objective: Find and delete zero byte size files on Unix / Linux.

To delete all zero byte files in the current directory and sub-directories, use the following find command syntax.

$ cd /path/to/directory/to/delete/files
$ find . -type f -size 0 -delete

The -type f option makes sure that we are working on a regular file and not on directories or other special files. The -delete action is not available on all find command implementations. It is mostly found on GNU find.

The -empty option can be used to test for an empty file instead of specifying -size 0. Again, -empty option is not a standard find option.

$ find . -type f -empty -delete

If the -delete option is not supported, the following command syntax can be used instead.

$ find . -type f -size 0 -exec rm -f {} \;

With the above syntax, the find command will execute rm on every zero byte sized file.

If you only want to delete files in the current directory and not in sub-directories, the -maxdepth option could be used.

$ find . -maxdepth 1 -type f -size 0 -exec rm -f {} \;

The -maxdepth option in not supported on Solaris and AIX. However, we can use use the -prune option to delete files only in the current directory and not in sub-directories.

$ find * -prune -type f -size 0 -exec rm -f {} \;

Note the use of * instead of . in the find command.

If you just want to get a list of zero byte sized files, remove the -exec and -delete options. This will cause find to print out the list of empty files.

$ find . -type f -size 0 

Ibrahim is a technology enthusiast with a keen interest in *NIX (Unix, Linux) systems, Android, open source and other tech related stuff. When his cpu load is low, you can probably find him online playing on his Xbox or PlayStation.