Fuzzy Search Directory in Terminal

09-08-2020

Sometimes we need to find a file or folder in a directory, but struggle to remember the exact name that we’re trying to find. Tab auto-completion is great but it also assumes that we know the first few letters of the thing we need to find. Here is a simple terminal command that will do a “fuzzy” search of the current directory. If you’re not aware a fuzzy search is one that looks through an entire word or phrase for a matching value. The search term may appear anywhere in the title of the file or folder.

1
ls | grep -i <search term>

Example

1
ls | grep -i scrap

If you’re a little more daring, you can search all nested folders recursively by using the -R flag. (Use this with caution.)

Recursive Example

1
ls -R | grep -i scrap

The command breakdown:

ls: List all files and folders by name in the directory
|: The pipe command passes data received from left side to the right side of the argument
grep: Use to search the results
-i: Lists all files which contain the search term