如何查找某个目录中包含最多文件的子目录 [英] How to find subdirectory of some directory, which have most files

查看:165
本文介绍了如何查找某个目录中包含最多文件的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have already done it using bash, but how can i get name or path to this subdirectory using tcsh. Later i need to count total size of all files in this subdirectory, please help me.

例如:

someDirectory

--firstDirectory

---- file11.txt

---- file12.txt

---- file13.txt

- secondDirectory

---- file21.txt

--thirdDirectory

---- file31.txt

---- file32.txt

---- file33.txt

---- file34.txt

---- file35 .txt

someDirectory
--firstDirectory
----file11.txt
----file12.txt
----file13.txt
--secondDirectory
----file21.txt
--thirdDirectory
----file31.txt
----file32.txt
----file33.txt
----file34.txt
----file35.txt

结果,我想获取ThirdDirectory的路径,因为它包含的文件最多。

And as result i want to get path to the thirdDirectory, cause it have most files.

更新

解决方案

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


推荐答案

您可以使用cut:

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

您可以使用的已识别目录中的文件大小:

The file size in the identified directory you can get with:

du -s

或:

ls -lh | head -n1

有人问了类似的问题,但不幸的是它已关闭:在Linux中,如何找到包含最多子目录或文件的目录?

Somebody asked a similar question, but unfortunately it was closed: In Linux, how do I find find directory with the most subdirectories or files?

如果只想计算文件而不是目录,则添加-type f以查找。

Add -type f to find, if you only want to count files and not directories.

这篇关于如何查找某个目录中包含最多文件的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆