How to list empty files? (Bash)
find . -name '*.txt' -size 0
Print files which match *.txt
and are of size zero.
To find all empty files in the current directory:
find . -maxdepth 1 -type f -name '*.txt' -empty
How it works:
find .
This starts
find
looking for files in the current directory.-maxdepth 1
By default,
find
searches recursively through subdirectories. This tells it not to. If you do want a recursive search, just remove this option.-type f
This limits the search to regular files.
-name '*.txt'
This limits the search to
.txt
files.-empty
This limits the search to empty files.
Non-recursive with stat
, output control with awk
:
$ stat -c "%n %b" *|awk '!$2 && $0=$1'
stat
prints filename (%n
) and allocated block count (%b
). In awk
refrain from printing if blocks allocated (!$2
) and replace record with filename when printing.