How to Find a File on Mac Terminal

You can use the find command on Mac Terminal to track down almost any file on your Mac. The command line interface used by Mac Terminal uses Z shell which is based on UNIX/LINUX shell environments and thankfully it provides this convenient to quickly find files. 

My name is Eric, and as a software engineer, command line interfaces such as Terminal are one of the main tools I use every day. A command such as the find can seem a little complicated, but I can show you the basics of how to use it.

If you would like to learn how to use the find command to find a file on Mac Terminal and hear a little bit about its power beyond finding files, stick around and we will discuss this further.

The Find Command

If you’re looking to find a file on Mac Terminal, the find command is what you will be using to accomplish this task. At first thought, you may think that this will be a simple command with not much to it. Just type the command and the name of the file you want to find. Right?

Well, the find command can seem simple, but it is actually one of the more complicated commands you will find with Terminal. The number of parameters, options, and ways to use it can be overwhelming and frustrating if you’re unsure of what you’re doing.

If there is any doubt, just look at the man page for the find command. You can do this by typing the following.

man find

You will see multiple pages of information on how to use it. The good thing is that once you get some of the basics down and get comfortable, it’s not so bad and it becomes one of the very powerful tools you have available to you in the command line environment.

A bit later, we will briefly discuss some of the more complicated and powerful things you can do with find, but for our purposes here, we are just going to learn the basics. We will simply review how to find a file on our disk drive. So let’s get started.

Find a File

In order to locate a file using the find command, you will need to know at least two parameters for the command. The first is the name of the file and the second is the starting search location. The find command takes the following format.

find <location to start from> -name <filename>

This seems fairly simple. You know the name of the file you want to search for but you will need to decide where you want to begin searching. The command will recurse or move through each subdirectory below the starting point looking for your file.

You can start from your current directory, the root directory, or a specific path and directory that you provide. The current and root directory are denoted as shown below.

. denotes the current directory

/ denotes the root directory

So, if I am looking for a file called test.exe and I want to search my entire disk, I would want my starting point to be the root directory, so my command would be as shown below.

find / -name test.exe

If I only want to search from my current directory I would use the period to define my current directory. like the command below.

find . -name test.exe

To search the applications directory, I would use the following command.

find /users/Eric/Applications -name test.exe

Making the Search Readable

The command above works– it will search and find files for you but with one major problem. Your file system has some locations that cannot be searched for one reason or another. These areas will not contain your file but the output from the command will show error or warning messages.

If you start your search from the root directory, you will find so many errors and warning messages scrolling on the screen that you cannot clearly see the results you are looking for. If you have tried running the command above, you will probably notice this.

The solution to this problem is to redirect the error and warning messages so that we do not see them on the screen. To do this, run the command as shown below.

find / -name test.exe 2>/dev/null

The 2>dev/null tells the system to redirect any error or warning messages to null instead of your screen. Now you will have a much cleaner output that you are able to read and see the locations of the file name that you specified.

The Power of Find

You can already see the power of the find command just by the simple find that we have used in the previous section. This is only the beginning of what you can do with the command. Once you learn the command and become comfortable with it, a whole new world will open up to you.

Using the command you can of course create customized searches. Search by things such as file date and time, permissions, owner, size, file content, file type, folders, wild cards, and more. Any trait that a file or folder has, you can use to search upon.

Once you learn how to search and produce lists of files, you can use the find command to run commands on the resulting files. Using the -exec parameter, you can execute other commands on the files you find. 

This allows you to create scripts, automate processes and handle large groups of files all at once. It can ultimately save time and make you more productive when trying to use Mac Terminal to process large amounts of data.

Examples

Let’s take a look at just a couple of quick examples. One of the most commonly used ways to use the find command with the -exec parameter is to find files that contain a specific string of text.

For this example, I would like to find and list all txt files in my current directory and its subdirectories that contain the word magic. I would use the command below to do this.

find . -name ‘*.txt’ -exec grep -l ‘magic’ {} \; 2>/dev/null

In the above example, I am searching my current directory (.) for any file with the .txt extension, then telling it to execute the grep command on that file and tell me if it contains the text magic. If so, it then prints the name and path of that file.

The {} in the command represents the filename found with the find command, and the -exec parameter executes the given command (grep -l in our case) on the found file. The \; puts a line ending on each command and as above, the 2>/dev/null redirects warning messages.

Let’s use a similar command but instead of searching for a text string in the file, we will copy all of the resulting files to a temporary directory. The command would then look like this.

find . -name ‘*.txt’ -exec cp {} tmp \; 2>/dev/null

As you can see, the command copied every txt file it could find from my current directory and below into a new directory called tmp. 

This same process could have been done using the wildcard and the cp command alone, but I have done it using the find command to show how it can be used to process multiple files found in a search. 

As you can see, the find command can get a list of files and process them with any command you specify. Just be careful when using commands that remove, move or edit files. Make sure that you run the find command by itself first to see the list of files that will be affected.

FAQs

Below are some common questions about finding files on Mac Terminal.

Why not always search from the root directory?

You may wonder, why search from a specific area or folder? Why not just start from the root directory? Searching from the root is great if you don’t have any idea of where the file might be but there are so many files and folders it can take a long time to get results.

If you already have an idea of where the file is located but can’t exactly pinpoint it, it’s best to narrow your search down to a more localized area. You will see search results much quicker.

Can I use the Find command to find a Folder?

Yes, you definitely can. Just put the name of the folder into the find command instead of the file name, and the find command will search for it and find it if it is there.

What if I see lots of permission denied warnings?

If you’re redirecting warning output to null you may not see the warnings. But if you run it without redirecting and do see lots or permission denied warnings, you don’t have permission to look into those folders. If you want to search them, you can with sudo.

Place the sudo command before the find command, and if you have admin permissions on your computer, it will search those directories as well. You will need to enter your password when running with sudo.

sudo find / -name test123

Conclusion

The best way to find files on Mac Terminal is to use the find command. While this command can be a bit complicated, it is very powerful and in its simplest forms you can use it to do basic searches of any file or folder on your system.

I hope the information provided above has helped you to get started using the find command. As usual, let me know if you have any questions or comments. I would love to hear from you.

Leave a Reply

Your email address will not be published. Required fields are marked *