Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the number of files in a directory for all directories in pwd

I am trying to list all directories and place its number of files next to it.

I can find the total number of files ls -lR | grep .*.mp3 | wc -l. But how can I get an output like this:

dir1 34 
dir2 15 
dir3 2 
...

I don't mind writing to a text file or CSV to get this information if its not possible to get it on screen.

Thank you all for any help on this.

like image 820
Abs Avatar asked Dec 10 '22 15:12

Abs


1 Answers

This seems to work assuming you are in a directory where some subdirectories may contain mp3 files. It omits the top level directory. It will list the directories in order by largest number of contained mp3 files.

find . -mindepth 2 -name \*.mp3 -print0| xargs -0 -n 1 dirname | sort | uniq -c | sort -r | awk '{print $2 "," $1}'

I updated this with print0 to handle filenames with spaces and other tricky characters and to print output suitable for CSV.

like image 192
Peter Lyons Avatar answered Feb 23 '23 00:02

Peter Lyons