簡體   English   中英

如何查找某個目錄的子目錄,其中包含大多數文件

[英]How to find subdirectory of some directory, which have most files

我已經使用bash完成了它,但是如何使用tcsh獲取該子目錄的名稱或路徑。 稍后我需要計算此子目錄中所有文件的總大小,請幫助我。

例如:

某個目錄
--第一個目錄
----file11.txt
----file12.txt
----file13.txt
--第二個目錄
----file21.txt
--第三目錄
----file31.txt
----file32.txt
----file33.txt
----file34.txt
----file35.txt

結果我想獲得第三個目錄的路徑,因為它有大多數文件。

更新

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

有人問過類似的問題,但不幸的是它被關閉了: 在 Linux 中,如何找到包含最多子目錄或文件的目錄?

如果您只想計算文件而不是目錄,請添加 -type f 以查找。

你可以這樣做:

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]

計數器=-1;

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

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

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

完畢

回聲$目錄

從 Dust 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM