gpt4 book ai didi

linux - 如何找到某个目录的子目录,其中包含最多的文件

转载 作者:太空狗 更新时间:2023-10-29 12:19:11 25 4
gpt4 key购买 nike

我已经使用 bash 完成了它,但是我如何使用 tcsh 获取此子目录的名称或路径。稍后我需要计算此子目录中所有文件的总大小,请帮助我。

例如:

一些目录
--第一个目录
----文件11.txt
----文件12.txt
----文件13.txt
--secondDirectory
----文件21.txt
--第三目录
----文件31.txt
----文件32.txt
----文件33.txt
----文件34.txt
----file35.txt

结果我想获取到 thirdDirectory 的路径,因为它有大部分文件。

更新

Bash 解决方案

#!/bin/bash -f 

declare -i maxcount=0
declare -i count
declare maxdirectory

find someFolder -type d | while read DIR;
do let count=`(ls $DIR | wc -w)`
if(($count > $maxcount)); then
let maxcount=count
maxdirectory="$DIR"
fi
done

更新

Tcsh-解决方案

cd $1
foreach x(*)
if(-d $x) then
set count = `(ls $x -1 | wc -l)`
if($count > $maxcount) then
set directory = $x
set maxcount = $count
endif
endif
end

最佳答案

你可以使用剪切:

find . | rev | cut -d "/" -f2- | rev | sort | uniq -c | sort -k1n,1 | tail -n1

或者 awk:

find . | awk -F "/" '{for(i=1;i<=NF-1;i++) printf $i"/"; printf "\n"}' | sort | uniq -c | sort -k1n,1 | tail -n1

您可以获取的已识别目录中的文件大小:

du -s

或:

ls -lh | head -n1

有人问过类似的问题,可惜被关了:In Linux, how do I find find directory with the most subdirectories or files?

如果您只想计算文件而不是目录,请添加 -type f 进行查找。

关于linux - 如何找到某个目录的子目录,其中包含最多的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15204980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com