Locate or Find Waldo with the Bash Shell

Locate or Find Waldo with the Bash Shell

That wily Mr. Waldo is off on another one of his adventures. This time he’s taken refuge somewhere in the Linux file tree, and it’s up to us to figure out where he went. There are two excellent bash shell commands at our disposal: find and locate. I’d like to get this job done quick, so lets start with the locate command. Locate is a fast way to search for files, because it relies on a pre-written database containing every known file’s location. To locate a file, simply enter ‘locate’ at the prompt, followed by the string you are searching for.

locate waldo

By default, locate searches by wholename, which includes the file path. As you can see, this returned one too many results for us. We can limit the search to just the file basename with the ‘-b’ option.

locate basename waldo

Hm, something isn’t right. Maybe I’ve bungled the capitalization on Waldo’s name. Locate is case-sensitive after all. We can use the ‘-i’ option to make our search case-insensitive.

locate basename case-insensitive waldo

Bingo! We found him. Lets move him home for safe keeping, shall we?

mv waldo

That’s odd, I swear he was right there! Maybe he moved recently? The locate command does not check to make sure files returned from the database still exist. We need to add the ‘-e’ option to tell it to only return files that exist at the time locate is run.

locate waldo

Curses, Martin Handford! We’re going to need a different strategy for finding Waldo. Hey, that’s it! We’ll use the find command. Find is much slower than locate because it runs the search in real-time, scanning through the file tree for our requested file. Find needs to be told where to look and what to look for. The decoy Waldo was in the Books folder, and I bet the real McCoy isn’t far off. Lets start by searching in that directory.

find waldo

Now we’ve got him! But wait, there are two Waldo’s. There can be only one! I have a feeling one of them is a directory, not a file. We are only interested in Waldo the file, not Waldo the directory. With the ‘-type’ option, we can narrow our search to only include files.

find waldo type of file

At last, we have found the real Waldo! The cool thing about the find command is that we can use it to pipe the files we find into another command with the -exec option.

remove waldo

Now nobody can find Waldo.

no more waldo

Lets review what we learned. We now know how to find files with two different bash command: locate and find. Locate searches a pre-written database, making it faster at the sacrifice of accuracy. Find is more accurate and flexible, but searches in real time, making it slower.

Featured image credit: Liam Quinn

Locate or Find Waldo with the Bash Shell was posted by on . JR is one of the three humans that run the behind-the-scene affairs of The Hello World Program. He is interested in spreading the good robot word over the vast Inter-webs. His contributions to the show include editing, web programming, design, and music composition.

Discuss this article in the Comments below, or and it with your friends. Learn more about Linux by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid
×