Search for the maximum number of files in a subdirectory

so I'm trying to write a bash script that would look at all the subdirectories in the specified folder and return the maximum number of files in one subdirectory. Here is what I have right now:

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

Firstly, the fileCount variable is not configured properly. Search result for "$ dir" -type f | wc -l is still set to stdout and as such the script continues to return zero.

Example of current output:

1
1
2
1
1
1
0

Where the last zero is the output for the echo "$ maxCount"

Not quite sure what I'm doing wrong. Thank!

Using xfce4 terminal

+4
source share
3 answers

Proper formatting:

#!/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"; }

Changes:

fileCount=${find "$dir" -type f | wc -l}

, fileCount

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

Command Grouping, maxCount , while, .

, !

+2

, , , 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
+4

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. , .

0

Source: https://habr.com/ru/post/1688044/


All Articles