Count files by type in a folder
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:
- Select in Finder the folder (or folders) you want to know how many files of each type contains
- Right-click on the selection and select the action from the menu
- Read the info on a pop-up window
The previous can be easily achieved with Automator.
- Open Automator and create a new document of type “Quick Action”
- When prompted select Quick Action and click Choose
- Set the workflow to receive folders as input in Finder.app
- Search for the action “Get Value of Variable” and drag it to the flow.
- Drag a variable of type path to the Variable field
- Search for the action “Run Shell Script” and drag it to the flow. Change pass input to “as arguments”
- Type the following in the script field
find "$1" -type f | grep -o ".[^.]\+$" | sort | uniq -c
- 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
- 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.
- Finally save the action with the name you want it to have on the finder popup menu.
The workflow should look something like this:
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.