LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-21-2013, 01:37 AM   #1
ashokingole
LQ Newbie
 
Registered: Apr 2012
Posts: 22

Rep: Reputation: Disabled
How to find which directory has the most number of files?


Please suggest,

How to find which directory has the most number of files ?
 
Old 03-21-2013, 01:39 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Show us what code you've done so far
 
Old 03-21-2013, 02:06 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Are you looking for any command or a script? Once go through manual of find command here.
 
Old 03-21-2013, 10:58 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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...
 
Old 03-22-2013, 07:53 AM   #5
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
Quote:
Originally Posted by ashokingole View Post
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.

Last edited by Madhu Desai; 03-22-2013 at 07:55 AM.
 
Old 03-22-2013, 09:31 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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).

Last edited by jpollard; 03-22-2013 at 09:46 AM.
 
Old 03-22-2013, 09:48 AM   #7
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
Quote:
Originally Posted by jpollard View Post
Code:
find . -depth -type d -exec sh -c "echo -n  {} :; ls -C1 {} | wc -l " ';'
This goes into my notes. Thanks!!!
 
Old 03-22-2013, 05:30 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script to read through text files and find largest number in files pomico Programming 15 09-13-2012 01:07 PM
Number of files in directory bullium Linux - General 13 08-31-2011 08:50 AM
Maximum number of files in a directory neranjana General 14 05-13-2010 01:33 PM
how to finding number of files in a directory a.toraby Programming 11 11-13-2007 04:08 AM
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 06:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration