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

Finding the maximum number of files in a subdirectory

所以我正在尝试编写一个 bash 脚本来查看指定文件夹中的所有子目录,并 return 单个子目录中的最大文件数。这是我现在拥有的:

#!/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 设置不正确。 find "$dir" -type f | 的输出wc -l 仍被设置为标准输出,因此脚本保持 returning 为零。

当前输出示例:

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之间不应该有space,你应该有一个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

你可以在纯 Bash:

中更有效地做到这一点
#!/bin/bash

# build a hash of directories and file counts
declare -A file_hash
while read -r -d '' file; do     # read the null delimited output of find
  dir="${file%%/*}"              # extract **top dirname** from file path
  ((file_hash[$dir]++))          # increment the count for this dir
done < <(find . -type f -print0) # find all files and output them with a null delimiter
                                 # this will gracefully handle files or directories that have new lines in their name

# find the top directory name with the biggest file count
max=0
for i in "${!file_hash[@]}"; do
  count="${file_hash[$i]}"
  ((count > max)) && { max=$count; max_dir=$i; }
done
printf 'max_dir=[%s], max_count=[%s]\n' "$max_dir" "$max"

在这种方法中,我们使用 find 对顶级子目录进行单次扫描。当有大量目录时,这会很好。

格式正确:

#!/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=${find "$dir" -type f | wc -l}

使用 Command Substitution 将 fileCount 变量正确设置为正确的值

{ while read dir; do ... echo "$maxCount"; }

使用 Command Grouping 在回显结果时将 maxCount 保持在与 while 循环相同的范围内。

希望这对以后的其他人有所帮助!