Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all zero-byte files in directory and subdirectories

Tags:

linux

shell

How can I find all zero-byte files in a directory and its subdirectories?

I have done 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 only get results in the current directory, not subdirs, and if any file name contains a space then I get only first word followed by tab

like image 378
Civa Avatar asked Mar 29 '13 12:03

Civa


People also ask

How do I find a zero-byte file?

If you just want to get a list of zero byte sized files, remove the -exec and -delete options. This will cause find to print out the list of empty files.

How do you check for zero bytes in Unix?

The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data.

Where can I find 0kb file in Linux?

Delete Empty Files in a Directory -type f -empty -print, will find all the empty files in the given directory recursively.


1 Answers

To print the names of all files in and below $dir of size 0:

find "$dir" -size 0 

Note that not all implementations of find will produce output by default, so you may need to do:

find "$dir" -size 0 -print 

Two comments on the final loop in the question:

Rather than iterating over every other word in a string and seeing if the alternate values are zero, you can partially eliminate the issue you're having with whitespace by iterating over lines. eg:

printf '1 f1\n0 f 2\n10 f3\n' | while read size path; do     test "$size" -eq 0 && echo "$path"; done 

Note that this will fail in your case if any of the paths output by ls contain newlines, and this reinforces 2 points: don't parse ls, and have a sane naming policy that doesn't allow whitespace in paths.

Secondly, to output the data from the loop, there is no need to store the output in a variable just to echo it. If you simply let the loop write its output to stdout, you accomplish the same thing but avoid storing it.

like image 122
William Pursell Avatar answered Sep 29 '22 23:09

William Pursell