How to List Folders Containing Specific Extensions on Mac (Only Once)
Beginner-friendly guide on how to list folders containing specific extensions (like .mkv) in Mac’s Terminal without duplicates. Includes BSD-compatible commands.
- Start
- August 18, 2025
- End
- August 18, 2025
- Period
- 1Days
- Last Updated
- August 18, 2025
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
finddoes 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 {} \;: Runsdirnamefor each.mkvfile 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
.mp4or.avito search other video formats.For multiple extensions, use
-o(OR), e.g.:-name "*.mkv" -o -name "*.mp4"Since
findsearches 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!