Suppose you want to know how many files of each type there are in a given folder and all its subfolders on MacOS. There is no default way to do this. You could try do it by hand but it might be extremely time consuming. Googling a bit around I came up with an elegant solution here by Peter Freitag.

Open Terminal app and write the following command replacing /some/dir with the folder you care for.

find /some/dir -type f | grep -o ".[^.]\+$" | sort | uniq -c

This will return the info you are looking for. Refer to Peter Freitag original post for an explanation on what each of the parts of the command does.

But it quickly gets pretty tiresome if you have to fire up terminal every time you have to do it. Plus forgetting the command is pretty easy. The simplest way would be to create a menu shortcut that could do all of that for you. The workflow would look like the following:

  1. Select in Finder the folder (or folders) you want to know how many files of each type contains
  2. Right-click on the selection and select the action from the menu
  3. Read the info on a pop-up window

The previous can be easily achieved with Automator.

Workflow start 2

  1. Open Automator and create a new document of type “Quick Action”
  2. When prompted select Quick Action and click Choose
  3. Set the workflow to receive folders as input in
  4. Search for the action “Get Value of Variable” and drag it to the flow.
  5. Drag a variable of type path to the Variable field
  6. Search for the action “Run Shell Script” and drag it to the flow. Change pass input to “as arguments”
  7. Type the following in the script field find "$1" -type f | grep -o ".[^.]\+$" | sort | uniq -c
  8. Search for the action “Set Value of Variable” and drag it to the flow. Change the variable to a new variable under a different name: i.e. output
  9. Search for the action ” Ask for Confirmation” and add it to the flow. Drag the new variable to the “message” field and under options select ignore ouput.
  10. Finally save the action with the name you want it to have on the finder popup menu.

The workflow should look something like this:

Complete workflow

Now, when you select a folder and right-click on it, the popup menu contains the action under Services.

There are two problems with the above script. Files without extension are shown with the complete path and extensions with different cases are counted separately.

Leave a Reply

Your email address will not be published. Required fields are marked *

four + seven =