6

How to find all zero bytes files in directory and even in subdirectories?

I did this:

#!/bin/bash
lns=`vdir -R *.* $dir| awk '{print $8"\t"$5}'`
temp=""
for file in $lns ; do
    if test $file = "0" ;then
        printf $temp"\t"$file"\n"
    fi
    temp=$file
done

...but I got only files in that directory, not all files, and if any file name had a space I got only the first word followed by a tab.

Can any one help me?

  • Question also posted on stackoverflow - please don't post the same question in multiple places. – glenn jackman Mar 29 '13 at 14:34
  • ok i never repeate it again – Civa Mar 29 '13 at 15:58
16

find is an easy way to do this-:

find . -size 0

or if you require a long listing append the -ls option

find . -size 0 -ls
  • can i filter files in a directory other than *.xml files – Civa Mar 29 '13 at 13:22
  • Yes you can - find . ! -name \*.xml -size 0 – suspectus Mar 29 '13 at 13:25
  • 2
    @Civa You can even do find . -empty – terdon Mar 29 '13 at 13:31
  • Yes indeed -empty will return zero sized files and empty directories. – suspectus Mar 29 '13 at 13:33
  • -empty is non-standard and not supported on a minimal, POSIX compliant find implementation – Gert van den Berg May 15 '18 at 14:33
0

find will include all files and directories under the paths given as parameters, filtering them based on rules given as additional parameteres. You can use

find "$dir" -type f -name 'glob*' -size 0 -print

Some find implementations does not require a directory as the first parameter (some do, like the Solaris one) and will default to the current working directory (.). On most implementations, the -print parameter can be omitted, if it is not specified, find defaults to printing matching files.

  • "$dir" gets substituted by the shell with the value of the dir variable (as from question)
  • -type f limits it to files (no directories or symlinks)
  • -name 'glob*' limits it to file that have name matching glob* (filenames starting with glob). To include all files, omit this
  • -size 0 only includes files with a size of 0 (the same in all units, for non-zero values, c needs to be included to check the file size in bytes)
  • -print is the action to perform with matching files. -print will print the filenames. It can be omitted on standard compliant find implementations. If it is not present -print is implied.
-3

you can try this:

ls -l | awk -F " " '{if ($5 == 0) print $9}'

Zero byte file of working dir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.