Search files on Linux and MacOS with the command line

I may just be unaccustomed to using Finder to search for files on the MacBook, but I really feel that its search functionality is way beyond that of Windows’. Apparently, I just don’t seem able to make it behave the way I want it to, which is why I have almost completely ditched it by now and just resort to using the command line.

There are two ways to accomplish this, each with its own benefits and drawbacks. The commands are identical both on MacOS and Linux or any UNIX system, for that matter, with one exception that I’m going to discuss.

First step

Both methods employ the command line, accessed through that scary program called Terminal. To open it in MacOS, click the Launchpad icon on the dock and type Terminal, then click the icon that appears.

On Linux, you can open it by pressing the keyboard shortcut Ctrl + Alt + T.

Searching files with the find command

The first method to look up files and folders in your computer is by using the find command. Find tends to be on the slow side, but has the advantage of not needing a database. If you’re sure the file you’re looking for belongs to your user account, type

find $HOME -iname “file name.extension”

You should mind the caps in $HOME. Let’s see what each part of the command means:

  • find is the name of the command itself;
  • $HOME is the path to your user’s home folder;
  • -iname tells the find command to perform a case insensitive search;
  • “file name.extension” is the name of your file, inclusive of extension;
    • If your file name does not contain spaces, you can drop the quotes;
    • You can use asterisks as wildcards (i.e., placeholders) if you only remember part of the name. For example, “file name*” will list all files matching “file name”, regardless of their extension.

If the file you’re looking for belongs to another user, or if you’re unsure if the file is in your home folder, use the command

sudo find / -iname “file name.extension” 2> /dev/null

Here’s the explaination of what every part of this command does:

  • sudo is a command that tells the operating system to run the command that follows as superuser, that is, an administrator that has access to every resource on the computer. This is fundamental because a normal user may not have access to every file;
  • find is the same as above;
  • / is the root folder, the lowest level possible in the file system structure. You may think of it as the equivalent of Windows’ C drive, with the notable difference that all drives on UNIX are part of the same file system structure (i.e., UNIX does not use drive letters). The implication of this is that find will search every storage device on the computer;
  • “file name.extension” is the same as above and the same considerations apply;
  • 2> is a redirection operator that operates on error messages. Remember the article about Windows’ DIR command? We used > to redirect the output of DIR to a file. 2> works similarly, but does not redirect everything, just error messages. This redirection is important because find will yield so many errors to make its output almost useless.
  • /dev/null is a special device file that is best described as a “black hole”. Everything that enters it disappears. It’s used to discard data that’s considered useful, like the error messages thrown by this command.

Searching files with locate

I really like the locate command: it’s very fast, especially when compared to find. If you want to search for a file, locate should be your first option. Instead of crawling the entire file system to find matches, locate keeps a centralized database of files that’s updated by running the updatedb command. In general, the operating system runs updatedb automatically once a day and the user should not be required to run it manually.

To find a file with locate, use the command

locate file name

Your computer will return the results almost instantaneously. Cool, huh?

There are different ways of updating the database, depending on whether you’re on Linux or MacOS. On Linux, you can run the command

updatedb

On MacOS, you need to run

/usr/libexec/locate.updatedb

About Andrea Luciano Damico 110 Articles
Andrea Luciano Damico is a freelance translator from Italy. Among his interests are linguistics, technology, video games, and generally being a chill guy. He runs Let's Translate.it and Tech4Freelancers.net.