User Tools

Site Tools


linux:find:find_empty_files

Linux - Find - Find Empty Files

find ./ -type f -size 0

or

find ./ -type f -empty

This commands will find all zero size files in the current directory with sub-directories and then print the full pathname for each file to the screen.

  • ./ means start searching from the current directory. If you want to find files from another directory then replace the ./ with the path to needed directory. For example, to search everything under the system log directory you need to replace ./ with /var/log.
  • -type f flag specifies to find only files.
  • -size 0 and -empty flags specifies to find zero length files.

Find and then delete all zero size files

To find and then delete all zero size files, there are variants you can use:

find ./ -type f -size 0 -exec rm -f {} \;
 
find ./ -type f -size 0 | xargs rm -f
 
find ./ -type f -size 0 -delete
  • The xargs will cause all the filenames to be sent as arguments to the rm -f commands. This will save processes that are forked every time -exec rm -f is run. But it fails with spaces etc in file names.

NOTE: The -delete option is the best choice when it is supported by the find you are using.

It avoids the overhead of executing the rm command by doing the unlink() call inside find().

linux/find/find_empty_files.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki