所以我正在尝试编写一个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终端
答案 0 :(得分:4)
您可以使用以下命令执行您想要的操作,该命令利用了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
答案 1 :(得分:2)
正确格式化:
#!/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循环相同的范围内。
希望这有助于将来的其他人!
答案 2 :(得分:0)
你可以在纯粹的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
对顶级子目录进行单次扫描。当存在大量目录时,这将很好。