Skip to content

MacOS find command

The MacOS find command is a versatile tool for locating files and directories on your Mac computer. It allows you to search your system based on various criteria such as file name, size, and modification date. By using different options and flags, you can narrow down your search results and find exactly what you are looking for. The find command is useful for managing your files, organizing your data, and troubleshooting issues on your Mac. Mastering this command can help you become more efficient in navigating your file system and locating important files with ease.

find Syntax:

Terminal window
find [path] [expression]

Options:

OptionDescription
-HFollows symbolic links on the command line
-LFollows symbolic links when checking directories
-PDoes not follow symbolic links
-EUses extended regular expressions in the expression

Parameters:

ParameterDescription
pathStarting directory for the search
expressionDefines the search criteria

Search for a File by Name

Terminal window
find . -name "example.txt"

Searches the current directory and all subdirectories for a file named “example.txt”.

Find Files Modified in the Last 7 days

Terminal window
find . -type f -mtime -7

Locates all files in the current directory modified within the last 7 days.

Search for Directories Only

Terminal window
find . -type d

Finds all directories in the current directory and subdirectories.

Exclude Files with a Specific Extension

Terminal window
find . -type f ! -name "*.log"

Searches for files in the current directory and subdirectories but excludes files with the extension “.log”.

How do I use find in MacOS?

To use the find command in MacOS, execute the following command:

Terminal window
find --option <value>

How can I find a file by name in MacOS?

To find a file by name in MacOS, you can use the following command:

Terminal window
find . -name "file_name"

How do I search for files by extension in MacOS?

To search for files by extension in MacOS, you can use the following command:

Terminal window
find . -name "*.txt"

How can I find files modified in the last X days in MacOS?

To find files modified in the last X days in MacOS, you can use the following command:

Terminal window
find . -mtime -X

How do I search for empty files and directories in MacOS?

To search for empty files and directories in MacOS, you can use the following command:

Terminal window
find . -empty

How can I search for files based on their size in MacOS?

To search for files based on their size in MacOS, you can use the following command:

Terminal window
find . -size +1M

How do I execute a command on the files found by find in MacOS?

To execute a command on the files found by find in MacOS, you can use the following command:

Terminal window
find . -name "file_name" -exec command {} \;

Applications of the find command

  • Searching for files and directories
  • Finding files based on different criteria such as name, size, type, and modified time
  • Executing actions on the files found, such as deleting, moving, or copying
  • Searching for files within a specific directory or a directory tree
  • Locating specific files or directories on the Mac filesystem