Recursively Counting files by Extension on Mac or Linux
Back in 2004 I wrote up a blog entry showing how to get a count of files by a specific extension. For example you want to know how many js files are in a directory, you can run this:
find /some/dir | fgrep -c '.js'
-c in grep tells it to count the matches, I'm using
fgrep here because I'm not using a regex (to avoid escaping the dot).
The above would also match a file, or a directory had
.js anywhere in the path, so we could improve that script by using a regular expression
$ character, for example:
find /some/dir | grep -c '\.js$'
Now we are limiting the .js to show up only at the end of the file.
What if you want a listing of all file extensions and the count of files in a directory?
Here's one way to print out a list of extensions and the number of files of each type:
find /some/dir -type f | grep -o ".[^.]\+$" | sort | uniq -c
This will print out a nice list like this:
5 .js 3 .html 1 .css
How it works
First we have
find /some/dir -type f which just limits find to output all the files in the directory recursively. The
-type f omits directories from showing up in the list.
Next we have
grep -o ".[^.]\+$" the
-o tells grep to only output lines that match the pattern, and only output the match. The pattern is just a regex that says look for a dot followed by one or more chars that are not a dot
[^.]\+, at the end of a line
Next we pipe into the
sort command which just puts every thing in order.
Finally we pipe into
uniq -c which counts each unique line (the file extensions) and prints out the results. Cool!
Like this? Follow me ↯Tweet Follow @pfreitag
You might also like:
- Counting IP Addresses in a Log File - October 11, 2019
- The 15 Essential UNIX commands - July 29, 2005
- Searching for files by file name on Mac or Linux - January 7, 2020
- Howto Backup your Mac incrementally over SSH - March 10, 2006
- Linux and OS X Shell Commands - March 7, 2005
- Passing Environment Variables to Sudo Command - September 23, 2019
- Difference between cd - vs cd ~- - September 19, 2019
- Why is my cron.daily script not running? - January 10, 2018