Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-19-2012
Registered User
 
Join Date: Jun 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Number of files in all (sub-)directories

Hi,

I need a list of the number of files in all (sub-)directories e.g.:

/a/b/c 1364
/a/b 125
/a 362

etc.

Should be nice to have the list sorted from high to low.

Regards,

Wim
Sponsored Links
    #2  
Old 06-19-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts
One way.. (Include the highlighted part for high to low)

Code:
$ find /dir/location -type f | nawk -F\/ '{$NF=""; print}' OFS=\/ | sort | uniq -c | sort -nr

The Following User Says Thank You to jayan_jay For This Useful Post:
deleriumdog (06-29-2012)
Sponsored Links
    #3  
Old 06-19-2012
Registered User
 
Join Date: Jun 2007
Location: North-West UK
Posts: 521
Thanks: 73
Thanked 85 Times in 81 Posts
Have you considered finding all the directories and then counting the files of each from there:-


Code:
for dir in `find $top_dir -type d | sort`
do
   echo "$dir \c"
   find $dir -type f | wc -l
done | sort -nr +1

That will give you al the files from each directory and below, but of course the higher directories will have all the subdirectories included, so might not be what you want. If you need to , then perhaps you could sort this output and then for each directory, reduce the count by any direct subdirectories (i.e. take away /a/b from /a, but ignore /a/b/c)


Does this help at all, or have I missed the point?



Robin
Liverpool/Blackburn

Last edited by rbatte1; 06-19-2012 at 09:19 AM.. Reason: Added sort
    #4  
Old 06-19-2012
Registered User
 
Join Date: Jun 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Hi jayan_jay,

This works, thanks!
Can you please add the total number of bytes in this directory on the same row as well?
Sponsored Links
    #5  
Old 06-19-2012
Registered User
 
Join Date: Jun 2007
Location: North-West UK
Posts: 521
Thanks: 73
Thanked 85 Times in 81 Posts
Quote:
Originally Posted by rbatte1 View Post
Have you considered finding all the directories and then counting the files of each from there:-


Code:
for dir in `find $top_dir -type d | sort`
do
   echo "$dir \c"
   find $dir -type f | wc -l
done | sort -nr +1

That will give you al the files from each directory and below, but of course the higher directories will have all the subdirectories included, so might not be what you want. If you need to , then perhaps you could sort this output and then for each directory, reduce the count by any direct subdirectories (i.e. take away /a/b from /a, but ignore /a/b/c)


Does this help at all, or have I missed the point?



Robin
Liverpool/Blackburn
If you are happy with the above, you can adjust it to add the du command like so:-


Code:
for dir in `find $top_dir -type d | sort`
do
   echo "$dir `find $dir -type f | wc -l` `du -ks $dir`"
done | sort -nr +1

Is that any good? This gives you the Kb for each directory on it's own. If you want to count all Kb from each directory and all sub-directories, drop the s flag from the du command.


Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 06-19-2012 at 09:48 AM.. Reason: Clarification of du
The Following User Says Thank You to rbatte1 For This Useful Post:
deleriumdog (06-29-2012)
Sponsored Links
    #6  
Old 06-19-2012
Registered User
 
Join Date: Jun 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
@rbatte1,

Can you add this in the command from jayan_jay?

Many thanks,

Wim
Sponsored Links
    #7  
Old 06-29-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts

Code:
$ find dir_path -type f -exec ls -ltr {} \; | nawk -F\/ '{$NF=""; print}' OFS=\/ | 
nawk '{a[$NF]++ ; b[$NF]=b[$NF]+$5}END{for(i in a) print(i, a[i], b[i])}'

The Following User Says Thank You to jayan_jay For This Useful Post:
deleriumdog (06-29-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help with examine the number files in directories given as arguments abo-el-sos Shell Programming and Scripting 1 04-22-2011 07:00 AM
List directories and sub directories recursively excluding files pointers UNIX for Dummies Questions & Answers 3 02-23-2011 08:39 PM
How to get number of files and directories? raju110384 UNIX for Dummies Questions & Answers 4 12-12-2010 06:13 AM
Help with command to Move files by X number to seperate directories Geo_Bean Shell Programming and Scripting 8 03-26-2010 06:10 AM
Bash scripting to compare N number of files located in two directories Sangtha Shell Programming and Scripting 2 07-08-2009 02:48 PM



All times are GMT -4. The time now is 04:56 AM.