How to identify empty files or directories in Linux or UNIX
The “find” command supports an option “-empty” to find files with zero size and directories which have no files and sub-directories.
The following command finds empty directories:
[neo@techpulp ~]# find . -empty -type d ./Documents/External ./Music/jazz ./Templates ./TestDir [neo@techpulp ~]#
The following command finds files with zero size:
[neo@techpulp ~]# find . -empty -type f ./Documents/draft1.doc ./Documents/testone.txt ./Music/download/u-and-me.mp3 [neo@techpulp ~]#
Similarly you can get the list of empty files and directories together with the following example:
[neo@techpulp ~]# find . -empty ./Documents/External ./Documents/draft1.doc ./Documents/testone.txt ./Music/download/u-and-me.mp3 ./Music/jazz ./Templates ./TestDir [neo@techpulp ~]#
If you want to clean up all empty files and directories recursively, you can use “xargs” command as shown below.
[neo@techpulp ~]# find . -empty | xargs rm -rf


about 1 year ago
For finding empty files/directories and removing them it is advisable to use “-exec” instead of xargs like http://www.theunixtips.com/bash-run-command-on-find-results/
find . -empty -exec rm -fr \{} \;