Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Using the Find Command to Search for Files

August 18, 2023 — Gregg Szumowski

One of the most useful and flexible GNU utilities is the find command. Understanding how to use this command is very important to make you Linux life more efficient.

The general syntax of the find command is:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

That looks like a lot, but most of the time you may only need 2 things:

find [path] [expression]

where the path is a starting point or the top of a directory tree to be searched, and expression is a property and value pair of what you’re trying to find. This may be a file name, last access time, last modification time, size, and/or ownership.

For example, if you’re looking for the file stdlib.h, use the following command:

find / -name stdlib.h

If you run this as a normal user, using find from the root directory will often result in a lot of error messages being output to the terminal because the normal user doesn’t have access to view some of the directories in the search. Therefore you may want to pipe the stderr output to /dev/null to avoid seeing those messages. You can do that like this:

find / -name stdlib.h 2>/dev/null

Tags: cli, find, motd