搜索和目录和子目录[BASH]计数文件 [英] Searching and counting files in directories and subdirectories [BASH]

查看:170
本文介绍了搜索和目录和子目录[BASH]计数文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有搜索和目录和子目录中的文件计数一个问题。我试图做这样的事情。

I have one question about searching and counting files in directories and subdirectories. I tried to do something like this

for i in $(find $TEST_DIR -type d | wc -l ); do
        for j in $(find $i -type f | wc -l); do
            FILES=$[FILES+j]
        done    
        DIRS=$[DIRS+i]
    done

,但它不工作。我只是在那之后我要比较的文件和目录的数量来计算每个目录和子目录下的文件(子目录)
感谢您的帮助:)

but it doesn't work. I just have to count files in every directory and subdirectory after that I have to compare quantity of files and directories(subdirectories) Thanks for your help :)

推荐答案

找到命令会发现所有的文件/显示目录递归,因此的不需要...循环

The find command will find all files/dirs recursively, so the for...loop is not needed:

FILES=$(find $TEST_DIR -type f | wc -l)
DIRS=$(find $TEST_DIR -type d | wc -l)

如果您的文件名可能包含换行,试试这个:

If your filename may contains newline, try this:

FILES=$(find $TEST_DIR -type f -printf x | wc -c)

这篇关于搜索和目录和子目录[BASH]计数文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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