加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 查找子目录中的最大文件数

发布时间:2020-12-15 21:35:52 所属栏目:安全 来源:网络整理
导读:所以我正在尝试编写一个bash脚本,它将查看指定文件夹中的所有子目录,并返回单个子目录中的最大文件数.这就是我现在所拥有的: #!/bin/bash maxCount=0 fileCount=0 # script that writes out all the directories and how many files are in each directoryf
所以我正在尝试编写一个bash脚本,它将查看指定文件夹中的所有子目录,并返回单个子目录中的最大文件数.这就是我现在所拥有的:

#!/bin/bash   
maxCount=0 
fileCount=0 
# script that writes out all the directories and how many files are in each directory

find ./testdata/ -maxdepth 1 -mindepth 1 -type d | while read dir; do  #loop all subdirectories    
fileCount= find "$dir" -type f | wc -l #count all the files in subdirectory

    if [ $fileCount -gt $maxCount ] #if the count is higher than the max     
    then
        maxCount= "$fileCount" #set the count equal to the max
    fi

    done

#print out how many messages are in the thread    
echo "$maxCount"

首先,变量fileCount设置不正确.找到“$dir”-type f |的输出wc -l仍然被设置为stdout,因此脚本保持返回零.

当前输出示例:

1
1
2
1
1
1
0

最后一个零是echo“$maxCount”的输出

不太确定我做错了什么.谢谢!

使用xfce4终端

解决方法

您可以使用以下命令执行您想要的操作,该命令利用了find的-exec选项

find ./testdata/  -maxdepth 1 -mindepth 1 -type d -exec bash -c 'find {} -type f | wc -l' ; | sort -n | tail -n 1

就像你的方法一样,这条线

fileCount= find "$dir" -type f | wc -l #count all the files in subdirectory

=和find之间应该没有空格,你应该有一个Command Substitution将值赋给变量fileCount,如下所示:

fileCount=$(find "$dir" -type f | wc -l)

如果你想坚持使用for循环:

find . -maxdepth 1 -mindepth 1 -type d | while read dir;do
    cnt=$(find ${dir} -type f | wc -l)
    echo ${cnt}   
done | sort -n | tail -n 1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读