Number of files in all (sub-)directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Number of files in all (sub-)directories
# 1  
Old 06-19-2012
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
# 2  
Old 06-19-2012
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

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 06-19-2012
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 10:19 AM.. Reason: Added sort
# 4  
Old 06-19-2012
Hi jayan_jay,

This works, thanks!
Can you please add the total number of bytes in this directory on the same row as well?
# 5  
Old 06-19-2012
Quote:
Originally Posted by rbatte1
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 10:48 AM.. Reason: Clarification of du
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 06-19-2012
@rbatte1,

Can you add this in the command from jayan_jay?

Many thanks,

Wim
# 7  
Old 06-29-2012
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])}'

This User Gave Thanks to jayan_jay For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

2. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

3. Shell Programming and Scripting

Script to count number of files in directories

Hi All! I would like to have a script that will count the number of files at the top of the hour of soome directories and mail the results to me. I was thinking on : a=`/directory/subdirectory/ | wc -l` echo "/directory/subdirectory :$a" b=`/another_dir/subdir/ | wc -l` echo... (12 Replies)
Discussion started by: fretagi
12 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. Shell Programming and Scripting

Need help with examine the number files in directories given as arguments

Hi , this is homework .. I have to finish it tomorrow, I did my best , but I found it so difficult .. Can You HELP Me ??! Write a bash shell script filestatic. The script should examine the number files in directories given as arguments (parameters) to this script. a. if one argument is... (1 Reply)
Discussion started by: abo-el-sos
1 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. UNIX for Dummies Questions & Answers

How to get number of files and directories?

Hi All, can you please tell me how to get no.of files and directories in the given directory.:confused: (4 Replies)
Discussion started by: raju110384
4 Replies

8. Shell Programming and Scripting

Help with command to Move files by X number to seperate directories

Hello, I need help finding a script that will allow me to move files from one directory to another directory 10k files at a time. I have a directory that has 100 K files in it. I need to have those 100k files broken apart to separate directories each with 10k files in them. Here is the... (8 Replies)
Discussion started by: Geo_Bean
8 Replies

9. Shell Programming and Scripting

Bash scripting to compare N number of files located in two directories

I want to compare "N" (around 2000+) number of huge files located in a directory A against "N" files located in a different directory using Bash scripting. Please help me with any scripts available. Thanks. (2 Replies)
Discussion started by: Sangtha
2 Replies
Login or Register to Ask a Question