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.
1 2 |
$ 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.
1 |
$ find . -type f -empty -delete |
If the -delete
option is not supported, the following command syntax can be used instead.
1 |
$ 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.
1 |
$ 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.
1 |
$ 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.
1 |
$ find . -type f -size 0 |