How to List Folders Containing Specific Extensions on Mac (Only Once)
Have you ever wanted to list only the folders that contain files with a specific extension (for example, .mkv
) on your Mac?
With a UNIX-based shell, you can easily achieve this using the find
command.
However, note that macOS and Linux have slightly different command options, so here we’ll explain the correct syntax for macOS as well.
✅ Goal
List folder names that contain .mkv
files, only once (no duplicates).
🔧 Basic Command (Linux / GNU find)
find . -type f -name "*.mkv" -printf '%h\n' | sort -u
Explanation
Part | Description |
---|---|
find . | Search under the current directory (. ) |
-type f | Target only files (ignore directories) |
-name "*.mkv" | Find files with the .mkv extension |
-printf '%h\n' | Print the parent directory (folder) |
sort -u | Sort results and remove duplicates |
🔸 This works for GNU find (Linux, etc.). macOS
find
does not support-printf
.
🍎 For macOS Users: Correct BSD find
Syntax
On macOS, replace -printf
with -exec dirname {} \;
.
find . -type f -name "*.mkv" -exec dirname {} \; | sort -u
Notes
-exec dirname {} \;
: Runsdirname
for each.mkv
file found, outputting the parent folder.sort -u
: Removes duplicates, giving you a unique list of folders.
▶ Example Output
./Movies
./Downloads/Anime
./ExternalDrive/Series
This way, you can list all folders containing .mkv
files.
🧠 Show Only Folder Names (Without Paths)
If you want just the folder names (no path), use xargs
and basename
.
find . -type f -name "*.mkv" -exec dirname {} \; | sort -u | xargs -n1 basename
Example Result:
Movies
Anime
Series
🔸 Recommended if you only want the folder names themselves.
🎯 Summary: Choose Based on Your Needs
Desired Output | Command Example | ||
---|---|---|---|
Folders with relative/full paths | `find . -type f -name “*.mkv” -exec dirname {} ; | sort -u` | |
Folder names only | `find . … | sort -u | xargs -n1 basename` |
📌 Extra: If -printf
Works on Linux…
If you’re on Linux or installed GNU find on macOS via Homebrew, you can use -printf '%h\n'
, which is slightly faster.
💡 Pro Tips for find
Change the extension to
.mp4
or.avi
to search other video formats.For multiple extensions, use
-o
(OR), e.g.:-name "*.mkv" -o -name "*.mp4"
Since
find
searches deeply, it may take time in very large directories.
With Terminal on macOS, file searches and organization become much more efficient. Even if you’re new to the command line, give it a try—you’ll find it surprisingly powerful!