如何查找某些目录的子目录,其中包含大多数文件

时间:2013-03-04 15:25:37

标签: linux csh tcsh

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

例如:

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

结果我想获得第三个目录的路径,因为它有大多数文件。

更新

击的溶液

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

4 个答案:

答案 0 :(得分:2)

你可以使用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

您可以获得的标识目录中的文件大小:

du -s

或:

ls -lh | head -n1

有人问了一个类似的问题,但不幸的是它被关闭了:In Linux, how do I find find directory with the most subdirectories or files?

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

答案 1 :(得分:0)

你可以这样做:

foreach f ( `find someFolder -type d` )
    echo `find $f -maxdepth 1 -type f | wc -w` $f >> tmp
end
set a = `sort -rn tmp | head -n1`
set num = $a[1]
set dir = $a[2]

答案 2 :(得分:0)

计数器= -1;

for ls -ltr | grep ^d | awk '{ print $NF }'

DO

    count=`ls -l $i | grep ^- | wc -l`

    if [[ $count -gt $counter ]]; then
            counter=$count
            Directory=$i
    fi

完成

echo $ Directory

答案 3 :(得分:0)

从尘埃4e1180e5开始(2020-08-30),您现在可以像这样:

-f, --filecount
Directory 'size' is number of child files/dirs not disk size

示例输出:

PS C:\Users\Steven\AppData\Local> dust -f
577            ┌── Default
628          ┌─┴ User Data
629        ┌─┴ Edge
1,154    ┌─┴ Microsoft
1,901    ├── Packages
407      │     ┌── 41ad6w4a.default-release-4
493      │     │   ┌── entries
497      │     │ ┌─┴ cache2
573      │     ├─┴ 7eccqsn1.default-release-1-1597857941226
3,952    │   ┌─┴ Profiles
3,953    │ ┌─┴ Firefox
3,954    ├─┴ Mozilla
3,096    │       ┌── entries
3,100    │     ┌─┴ cache2
3,269    │   ┌─┴ 56uzf1ra.Sunday
10,341   │   │   ┌── entries
10,344   │   │ ┌─┴ cache2
10,428   │   ├─┴ 7nx7hnxa.68-edition-default
13,698   │ ┌─┴ Profiles
13,699   ├─┴ Waterfox
21,181 ┌─┴ .

由于它刚刚发布,所以我必须使用以下命令自己构建它:

$env:RUSTFLAGS = '-C link-arg=-s'
cargo build --release