How to find all zero byte files in directory

6

1

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?

Civa

Posted 2013-03-29T12:41:26.977

Reputation: 198

Question also posted on stackoverflow - please don't post the same question in multiple places.

– glenn jackman – 2013-03-29T14:34:10.030

ok i never repeate it again – Civa – 2013-03-29T15:58:41.333

Answers

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

suspectus

Posted 2013-03-29T12:41:26.977

Reputation: 3 957

-empty is non-standard and not supported on a minimal, POSIX compliant find implementation – Gert van den Berg – 2018-05-15T14:33:06.607

can i filter files in a directory other than *.xml files – Civa – 2013-03-29T13:22:36.233

Yes you can - find . ! -name \*.xml -size 0 – suspectus – 2013-03-29T13:25:57.623

2@Civa You can even do find . -empty – terdon – 2013-03-29T13:31:19.343

Yes indeed -empty will return zero sized files and empty directories. – suspectus – 2013-03-29T13:33:45.027

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.

Gert van den Berg

Posted 2013-03-29T12:41:26.977

Reputation: 538

-3

you can try this:

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

Zero byte file of working dir.

Tango

Posted 2013-03-29T12:41:26.977

Reputation: 1

The most general reason why this is wrong: Why you shouldn't parse the output of ls.

– Kamil Maciorowski – 2018-10-05T19:43:18.577