在目录和子目录中查找所有零字节文件 [英] Find all zero-byte files in directory and subdirectories

查看:58
本文介绍了在目录和子目录中查找所有零字节文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在目录及其子目录中找到所有零字节文件?

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

我已经这样做了:

#!/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

推荐答案

要打印大小为$ 0的$ dir中及其以下的所有文件的名称:

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

find "$dir" -size 0

请注意,并非所有find的实现都会默认生成输出,因此您可能需要这样做:

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

请注意,如果ls输出的任何路径包含换行符,这将在您的情况下失败,并且这将加强两点:不要解析ls,并且具有明智的命名策略,该策略不允许在路径中使用空格

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.

第二,要从循环中输出数据,无需将输出存储在仅用于echo的变量中.如果仅让循环将其输出写入stdout,则可以完成相同的操作,但要避免存储它.

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.

这篇关于在目录和子目录中查找所有零字节文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆