How to find all zero-byte files in a directory inc

2019-03-08 06:54发布

How can I find all zero-byte files in a directory and even in 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 that directory file not all files and if any file name has a space then I get only first word followed by tab

Can anyone help me?

标签: linux shell
4条回答
等我变得足够好
2楼-- · 2019-03-08 07:26

This is the correct way to search for size 0:

find /path/to/dir -size 0 -type f -name "*.xml"

Search for multiple file extensions of size 0:

find /path/to/dir -size 0 -type f \( -iname \*.css -o -iname \*.js \)

Note: If you removed the \( ... \) the results would be all of the files that meet this requirement hence ignoring the size 0.

查看更多
一夜七次
3楼-- · 2019-03-08 07:36

As addition to the answers above:

If you would like to delete those files

find $dir -size 0 -type f -delete
查看更多
神经病院院长
4楼-- · 2019-03-08 07:39

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
查看更多
姐就是有狂的资本
5楼-- · 2019-03-08 07:42

No, you don't have to bother grep.

find $dir -size 0 ! -name "*.xml"
查看更多
登录 后发表回答