Procurares
Procurares

Reputation: 2249

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

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.

For example:

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.

Update

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

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

Upvotes: 2

Views: 1310

Answers (4)

Zombo
Zombo

Reputation: 1

Start with Dust 4e1180e5 (2020-08-30), you can now do this like so:

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

Example output:

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 ┌─┴ .

Since this was just released, I had to build it myself with this command:

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

Upvotes: 0

Piyush Baijal
Piyush Baijal

Reputation: 281

counter=-1;

for i in 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

done

echo $Directory

Upvotes: 0

tommy.carstensen
tommy.carstensen

Reputation: 9622

You can use cut:

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

Or 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?

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

Upvotes: 2

Mark Armstrong
Mark Armstrong

Reputation: 440

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]

Upvotes: 0

Related Questions