Linux 101: How to search for files from the Linux command line

Jack Wallen shows you how to locate files on the Linux directory hierarchy using the find command.

linuxhero1.jpg

Image: Jack Wallen

Where did you leave that one particular configuration file you were just working on? You could start poking around in the directories that you assume might house the file using the ls command, but that could take far more time than you'd like to spend on this quest. Your best bet is to make use of the built-in CLI search tools, such as find

The find command is a powerful search command that can easily help you find the files you're looking for. Let me show you how to use this built-in command.

SEE: A guide to The Open Source Index and GitHub projects checklist (TechRepublic Premium)

How to use the built-in find command

Let's say you have a file named dev.conf and you can't remember where you saved it. Using the find command you could do a quick search through the entire filesystem with the command: 

find / dev.conf

The problem with that command is it's not only going to search every directory in the hierarchy, it'll also include directories (such as /proc) that most certainly won't house your file. Because of this, the output of the command will be far too long to be useful. 

To fix that we have to make use of a few options to prune away directories like /proc and get a bit more specific. Such a command might look like:

find / -path /proc -prune -false -o -name 'dev.conf'

What that command does is search the entire filesystem for a file named dev.conf, but leaves out the /proc directory in the process. Although this will still include directories like /run and /snap (which would unlikely house your file), the output would be significantly easier to comb through and locate your file. 

If you want to prune multiple directories, you'd have to make use of a regular expression like this: 

find / \( -path /proc -o -path /run -o -path /snap \)  -prune -false -o -name 'dev.conf' 

That find command would prune out /proc, /run, and /snap to make it even easier to locate the file you're looking for. 

Using the find command isn't the most intuitive means of locating files from the command line, but once you get used to it, you'll find it incredibly powerful and useful. For more information, make sure to read the man page with the command man find

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see