如何找到某个目录的子目录,哪个目录有最多的文件

时间:2021-04-16 15:05:17

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.

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

For example:

例如:

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

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

因此我想要获得指向thirdDirectory的路径,因为它有大部分文件。

Update

更新

Bash-solution

Bash-solution

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

Update

更新

Tcsh-solution

Tcsh-solution

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

3 个解决方案

#1


2  

You can use cut:

你可以使用:

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

Or awk:

或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

or:

或者:

ls -lh | head -n1

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

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

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

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

#2


0  

You could do something like this:

你可以这样做:

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]

#3


0  

counter=-1;

counter = 1;

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

因为我在ls ltr | grep ^ d | awk“{打印$ NF }”

do

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

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

done

完成

echo $Directory

echo $目录

#1


2  

You can use cut:

你可以使用:

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

Or awk:

或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

or:

或者:

ls -lh | head -n1

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

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

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

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

#2


0  

You could do something like this:

你可以这样做:

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]

#3


0  

counter=-1;

counter = 1;

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

因为我在ls ltr | grep ^ d | awk“{打印$ NF }”

do

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

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

done

完成

echo $Directory

echo $目录