LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to find which directory has the most number of files? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-which-directory-has-the-most-number-of-files-4175454952/)

ashokingole 03-21-2013 01:37 AM

How to find which directory has the most number of files?
 
Please suggest,

How to find which directory has the most number of files ?

chrism01 03-21-2013 01:39 AM

Show us what code you've done so far

shivaa 03-21-2013 02:06 AM

Are you looking for any command or a script? Once go through manual of find command here.

jpollard 03-21-2013 10:58 PM

possibly the simplest way:

Code:

find . -depth -type d -print -a -exec sh -c "ls -C1 {} | wc -l " ';'
This will find all directories (starting with the current directory) and print the directory name, and on the next line print the number of files in that directory...

Madhu Desai 03-22-2013 07:53 AM

Quote:

Originally Posted by ashokingole (Post 4915725)
How to find which directory has the most number of files ?

original author: Shell Script To recursively display (list) directories

i've modified this shell script which display no of files in each directory.

PHP Code:

#!/bin/sh
#filecount.sh - Counts number of files in each directory recursively.
DIR="."

function list_files()
{
    
cd "$1"
    
fullpath="$(pwd)"
    
echo "$fullpath :" $(find "$fullpath/" -noleaf -maxdepth 1 -type f  wc -l) ;
    for 
i in *
    do
        if 
test -"$i#if dictionary
           
then
              list_files 
"$i#recursively list files
              
cd ..
          
fi
    done
}

if [ $
# -eq 0 ]
then list_files .
exit 
0
fi 

Code:

# pwd
/var/ftp

# ./filecount.sh
/var/ftp : 1
/var/ftp/pub : 0
/var/ftp/pub/abcd : 0
/var/ftp/pub/abcd/configs : 3
/var/ftp/pub/abcd/others : 0
/var/ftp/pub/centos64 : 9
/var/ftp/pub/centos64/EFI : 1
/var/ftp/pub/centos64/EFI/BOOT : 4
/var/ftp/pub/centos64/images : 4
/var/ftp/pub/centos64/images/pxeboot : 3
/var/ftp/pub/centos64/isolinux : 11
/var/ftp/pub/centos64/Packages : 6382
/var/ftp/pub/centos64/Packages/repodata : 9
/var/ftp/pub/centos64/repodata : 10

you can build further from this.

jpollard 03-22-2013 09:31 AM

missed the directory entries (which are also files...)

ah - a contest :)

And you can get the same from:

Code:

find . -depth -type d -exec sh -c "echo -n  {} :; ls -C1 {} | wc -l " ';'
Or if you wanted to sort the list afterwards....
Code:

find . -depth -type d -exec sh -c 'echo `ls -C1 {} | wc -l ` {}' ';' | sort -n -r
That way the output has the entry for the largest number of files at the top of the list,
and empty directories at the bottom (the -r option on the sort).

Madhu Desai 03-22-2013 09:48 AM

Quote:

Originally Posted by jpollard (Post 4916544)
Code:

find . -depth -type d -exec sh -c "echo -n  {} :; ls -C1 {} | wc -l " ';'

This goes into my notes. Thanks!!! :hattip:

theNbomr 03-22-2013 05:30 PM

Might as well spruce it up a little:
Code:

find . -depth -type d -exec sh -c "echo -n  {} ' '; ls -C1 {} | wc -l " ';' | sort -n -k2
--- rod.


All times are GMT -5. The time now is 11:21 AM.