DEV Community

Cover image for 4 Useful Terminal Commands for Awkward Finder Tasks (Bite-size Article)
koshirok096
koshirok096

Posted on

4 Useful Terminal Commands for Awkward Finder Tasks (Bite-size Article)

Introduction

Today’s article is a short one, but I’d like to share four useful Terminal commands I recently learned for Finder-related tasks (Mac).

Until not long ago, I used to check these things manually, but doing them in Terminal is much faster, so I’ve been using these commands a lot lately. I decided to write this article partly as a memo for myself, and also to share them with people who may not know them yet. Much of this is fairly basic, so some of you may already know these, but if you’re interested, I hope you’ll give it a quick read.

1. Show Only Recently Updated Files

find . -type f -mtime -7
Enter fullscreen mode Exit fullscreen mode

This command lists files that have been updated recently under the current directory and its subdirectories.

Finder does have a “Recent” section in the sidebar, but in my case—perhaps because of my own PC—it can sometimes take time to load, or miss files I expect to see. So this feels like a more reliable method.

The -7 part represents the number of days in the past, so it’s convenient that you can change it to any value you want.

2. Count Files

First, here is the simplest version as a basic way to count files.

find . -type f | wc -l
Enter fullscreen mode Exit fullscreen mode

This command counts the number of files under the current directory (excluding folders).

If you want to include folders as well, you can use:

find . | wc -l
Enter fullscreen mode Exit fullscreen mode

Note: In this form, the starting point . itself is also counted as one item. If you do not want to include it, use find . -mindepth 1 | wc -l instead.


Next, based on these commands, here is an example of excluding a specific folder.

find . -type f ! -path '*/node_modules/*' | wc -l
Enter fullscreen mode Exit fullscreen mode

This counts files while excluding node_modules. In this case, the added condition is “exclude this folder.”

If you want to count only files that contain a specific string in their filename, you can change it like this:

find . -type f -name '*test*' | wc -l
Enter fullscreen mode Exit fullscreen mode

This counts only files whose names contain test.

Here, the added condition is “filter by name.”

Finder already shows the number of items in a directory at the bottom by default, but by combining commands like these, you can investigate with much more specific conditions, which makes them very useful.

3. Find Empty Folders

find . -type d -empty
Enter fullscreen mode Exit fullscreen mode

This command lists only empty directories.

Sometimes you create a folder during work and forget to put anything in it, or empty folders remain after reorganizing files. It is possible to look for them in Finder, but it becomes quite troublesome once the folder hierarchy gets deep.

In those situations, this command is convenient because it lets you check unnecessary empty folders all at once. It is a small thing, but quite useful when cleaning up a project.

4. Quickly See Which Folders Are Heavy

du -sh ./* | sort -h
Enter fullscreen mode Exit fullscreen mode

This command displays the sizes of files and folders directly under the current directory in an easy-to-read order.

du -sh ./* shows the size of each item, and sort -h arranges them in order by size. Because of that, it is useful when you want to get a rough idea of which folders are taking up the most space.

In Finder, you can check information for individual items with Command + I, but comparing multiple folders in a list is a bit inconvenient. When you want to identify unused large data and delete it, it can be very productive to go to a suspicious directory first and run this command.

Note: In this example, hidden files and hidden folders whose names begin with a dot are not included.

Conclusion

Today I introduced four Finder-related Terminal commands that I have been using a lot recently.

Personally, many of these tasks could already be checked manually, so for a long time I never really had the idea of looking into whether there was a faster way to do them. However, once I started using them in real work, I found them extremely convenient, and I feel that they noticeably improve the speed of small verification tasks, even if only in subtle ways.

Finder can already do many things quite well, but when you want to check things with more specific conditions, Terminal still feels more useful. If I come across more commands like these, I’d like to keep sharing them here as notes for myself as well.

Thank you very much for reading!

Top comments (0)