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.

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.

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

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.

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.

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.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }