How to Do a Fuzzy File Search in Linux

When searching for files on the Linuz desktop, you can often rely on the search function of your favorite file manager. The conventional search approach is to apply an exact search, so that only files or directories that perfectly match the search string are displayed.

A fuzzy search, on the other hand, performs an approximate search and, instead of identifying exact matches, displays matching results with every keystroke. the fzf utility identifies itself as a general purpose fuzzy finder. Released under the MIT license, the cross-platform tool can be used on all versions of Linux.

Install fzf on Linux

(Image credit: Tom’s Hardware)

The latest version, version 0.29.0, is offered only in the latest versions of distributions such as Fedora. Manjaro, openSUSE Tumbleweed, Ubuntu, Debian, and Arch, while most others offer older versions in their repositories.

To be installed on a Debian / Ubuntu system

1. Open a terminal.

2. Update the list of repositories. This ensures that we get the latest software.

sudo apt update

3. Install f2f using the apt package manager.

sudo apt install f2f

Besides using the software repositories, there are several other ways to install fzf, including using the homebrew package manager for Mac. Of course, you can also use Git to clone the project directory to your hard drive and then install fzf using the install script. The advantage of using Git over package manager is that we get the cutting edge version.

To be installed via Git

1. Open a terminal.

2. Clone the repository.

git clone --depth 1 https://github.com/junegunn/fzf.git

3. Change directory to the downloaded Git repository.

cd fzf

4. Run the install script.

./install

The fzf tool supports auto-completion and various key bindings. The install scripts ask if you want to enable them and also offer to edit your ~/.bashrc file. Once enabled, you will be able to deploy the following hotkeys to invoke fzf:

Keyboard binding A function
CTRL+r Search recent command history based on $HISTFILE
CTRL+t Recursive search for filename in $PWD
Alt+c Recursively search for a directory name under the $PWD, and cd into the selected directory

Working with fzf

(Image credit: Tom’s Hardware)

The fzf tool is just a search tool, so it doesn’t make sense to invoke it by itself. Instead, you can use it in conjunction with other commands like cd , or to open files in a text editor like vim or nano.

Before trying this, let’s start with something simple.

1. Open a terminal.

2. hurry Alt+c to activate the phone book search function. This function will only work in your current working directory (PWD). By default, the terminal opens to our residence phone book.

3. Wait for the interactive prompt to load. The first time it loads, it may take fzf a few moments to cache the data.

4. Start typing the name of a directory, for example Videos. The interactive prompt will start showing directories that match the word.

5. Press Enter to change directory. If there are multiple entries, use the up and down arrows to highlight, then press enter to select.

If nothing is found, exit fzf by pressing CTRL+C Where ESC.

Using fzf tab completion

You can use fzf as a replacement for tab completion with all terminal commands. To invoke fzf tab-completion, you must use the identifier ** with a command. Here we use fzf tab-completion with the CD (change directory command) to list all the directories that match our search.

(Image credit: Tom’s Hardware)

1. Open a terminal.

2. Type it CD command then press space, followed by two **. Then press the TAB key to trigger tab completion.

cd **

3. Search for a directory. In our example, we searched for Images. As you type, fzf will search for directories that match the search term. Arrow keys can be used to scroll through a list of entries.

4. Press Enter to select the directory, then press Enter again to change directory.

Another example of tab completion is to use it with the kill command to target a specific application/process and then kill it.

1. Open a terminal.

2. Use tab completion with the to kill order.

kill -9 **

3. Search for an application/process. Inkscape was running in the background, so we chose to research that.

4. Use arrow keys to select the correct process and press Enter to select.

5. Press Enter to run the kill command with the specified process.

(Image credit: Tom’s Hardware)

By default, fzf starts in expanded search mode, which means you can type space-separated keywords to narrow down the file or directory you’re looking for.

However, you can also use different techniques to improve search results. Here we use filename search to find Python files.

1. Open a terminal.

2. Type less then press CTRL+T to open fzf’s filename search tool. To display a file, we use less as it comes with built-in pagination and tools for scrolling large amounts of text.

3. Type the file extension, in this case .py, and add a dollar sign at the end of the line.

.py$

4. Use arrow keys to select the correct file and press Enter to select.

5. Press Enter to open the file using less. To research

Table of other improvements

Token Example Explanation
string of characters string of characters Search for a channel
‘string of characters Performs an exact match for elements that include the quoted string
^string ^LXF Lists items that begin with LXF performing an exact prefix match
.format$ .odt$ Lists files that end in .odt format by performing an exact suffix match
!string of characters !dhc List items that don’t include dhc by performing a reverse exact match
!^string ~^LXF List elements that do not start with LXF by performing an exact reverse prefix match
!.format$ !.odt$ Lists files that do not end in .odt format by performing an exact reverse suffix match

This tutorial first appeared in issue 287 of Linux Format magazine.

Comments are closed.