Pampa
Pampa

Reputation: 133

Unix - Count Number of file types recursively

I am new to Stack Overflow and am somewhat of a newbie with Linux. I have been trying to filter specific files within a parent directory and it's children using the following command as an example:

ls -R | grep '*.jpg' | wc -l

Which I have found great when looking for individual files but I will need to do this on a monthly basis and looking for quicker ways to list several types in one command. I purposely want to exclude hidden files.

I have tried this but to no avail — Count number of specific file type of a directory and its sub dir in mac

I've seen different methods across the web from list, find, tree, echo etc. so any help with this would be much appreciated and if there is a better way of doing this than what I am currently doing then that's not a problem as I am open to suggestions. I'm just not sure what's the best way to skin this cat at the moment!

Thank you very much

Upvotes: 11

Views: 13106

Answers (4)

Stecman
Stecman

Reputation: 3070

If you arrive here looking for more of a summary, here's a way to count all file extensions recursively in a folder:

find . -type f -name '*.*' -not -name '.*' | sed -Ee 's,.*/.+\.([^/]+)$,\1,' | sort | uniq -ci | sort -n

This gives a summary like:

    422 mov
   1043 mp4
   3266 png
   6738 CR3
   9417 RAF
  29679 cr2
  60949 jpg

Upvotes: 10

Pampa
Pampa

Reputation: 133

Thank you all for contributing, in case this proves to be useful to someone out there, I've had some help from a developer friend who's kindly looked into it for me and what I've found that works best in my particular case is the following:

find . -type f \( -iname "*.jpg" ! -iname ".*.png" ! -path "*/.HSResource/*" \) |wc -l

This skips over the resource folders and hidden files and appears to return me the correct results.

Upvotes: 0

Dieselist
Dieselist

Reputation: 945

you can do this with help of find as it was mentioned under the link from your initial post. Just something like this:

find . -name \*.jpg -or -name \*.png -not -path \*/\.\* | wc -l

Upvotes: 15

Nidhoegger
Nidhoegger

Reputation: 5232

You can have grep filter for more than one pattern. You should learn about manpages in linux, just type man grep in a terminal and you will see of what this program is capable of and how.

For your issue, you could e.g. use this to filter for png and jpeg files (ingoring case-sensitivity, thus getting PNG and png files):

ls -R | grep -i '*.jpg\|*.png' | wc -l

the -i will ignore the case of the names, the \| is an or-concatenation.

Upvotes: 0

Related Questions