Introduction to fzf command in Linux


Introduction Linux command-line interface is one of most powerful tools available to system administrators and developers for managing and working with their systems. One of challenges with CLI is that it requires memorizing many commands and options. However, there are tools that make it easier to navigate command line, such as fzf command. In this article, we will introduce fzf command, how it works, and its features.

What is fzf?

fzf is a command-line fuzzy finder, which allows you to easily search through files, directories, and other types of data. It is a highly customizable tool that can be used to filter and search through any type of data on your system.

fzf is written in Go programming language, and it is available for Linux, macOS, and Windows operating systems. tool works by using a fuzzy search algorithm that matches input with available data, and then it ranks results based on relevance.

Features of fzf

fzf comes with a lot of features that make it one of best command-line tools available. Some of features include −

  • Fuzzy search − fzf uses a fuzzy search algorithm that matches input with available data, making it easy to find what you're looking for.

  • Multi-select − fzf allows you to select multiple items at once, making it easy to perform batch operations on your data.

  • Interactive preview − fzf provides an interactive preview of selected data, allowing you to see what you're about to work on before you actually perform operation.

  • Customizable − fzf is highly customizable, allowing you to change default behavior of tool to match your preferences.

  • Keyboard shortcuts − fzf comes with a lot of keyboard shortcuts that make it easy to use and navigate.

Examples of Using fzf

Here are some examples of how to use fzf command −

Searching for files in a directory

Suppose you have a directory with a lot of files, and you want to find a specific file. Instead of using traditional ls command to list all files and then searching through output, you can use fzf to search for file.

To search for a file in a directory, you can use following command −

ls | fzf

This command will list all files in directory, and then fzf will allow you to search for file you want.

Using fzf to search through command history

If you want to search through your command history to find a specific command, you can use following command −

history | fzf

This command will display all commands you have executed previously, and then fzf will allow you to search for command you want.

Searching for a process and killing it

If you want to search for a running process and kill it, you can use following command −

ps aux | fzf | awk '{print $2}' | xargs kill -9

This command will list all running processes on your system, and then fzf will allow you to search for process you want to kill. Once you have selected process, awk will extract process ID, which will then be passed to kill command to terminate process.

Using fzf to navigate to a directory

If you want to navigate to a directory quickly, you can use following command −

cd $(find * -type d | fzf)

This command will list all directories in current directory, and then fzf will allow you to search for directory you want to navigate to. Once you have selected directory, cd command will take you to selected directory.

Using fzf to search through a command's output

If you have a command that produces a large output, you can use fzf to search through output and find information you need. For example, if you want to search for a specific process using ps command, you can use following command −

ps aux | fzf --preview 'echo {} | awk "{print \$2}" | xargs ptree' | awk '{print $2}'

This command will display all running processes on your system, and then fzf will allow you to search for process you want to investigate. Once you have selected process, fzf will display a preview of process tree, and awk will extract process ID, which will be printed to console.

Customizing fzf

fzf can be customized to match your preferences by setting environment variables or using command-line options. Some of environment variables you can set include −

  • FZF_DEFAULT_COMMAND − This variable sets command that fzf will use to generate input. For example, if you want fzf to search through files in a specific directory, you can set variable as follows −

export FZF_DEFAULT_COMMAND='find /path/to/dir -type f'
  • FZF_DEFAULT_OPTS − This variable sets default options that fzf will use. For example, if you want fzf to use a specific layout, you can set variable as follows −

export FZF_DEFAULT_OPTS='--layout=reverse --preview-window=right:50%'

Advanced Usage of fzf

In addition to examples we have already covered, there are other advanced ways to use fzf. Let's look at a few of them −

Using fzf with Git

If you are working with Git, you can use fzf to select files to commit, checkout, or diff. For example, to select files to commit, you can use following command −

git status -s | fzf | awk '{print $2}' | xargs git add

This command will show you files that have been modified, and then fzf will allow you to select files you want to commit. Once you have selected files, awk will extract file names, which will be passed to git add command to stage changes.

Using fzf with Vim

If you are a Vim user, you can use fzf to quickly open files, buffers, or tags. For example, to open a file using fzf, you can use following command −

vim $(find * -type f | fzf)

This command will list all files in current directory, and then fzf will allow you to select file you want to open. Once you have selected file, Vim will open file in a new buffer.

Using fzf with Tmux

If you are a Tmux user, you can use fzf to quickly switch between Tmux sessions or windows. For example, to switch between Tmux sessions, you can use following command −

tmux switch-client -n -t $(tmux list-sessions -F '#S' | fzf)

This command will show you a list of all Tmux sessions you have open, and then fzf will allow you to select session you want to switch to. Once you have selected session, tmux switch-client command will switch you to selected session.

Using fzf with Ranger

If you use Ranger, a file manager for command line, you can use fzf to quickly select files to operate on. For example, to move a file to a different directory, you can use following command −

ranger --choosefiles="$(find * -type f | fzf)" --choosedir="$(find * -type d | fzf)"

This command will show you a list of all files in current directory, and then fzf will allow you to select file you want to move. Once you have selected file, fzf will show you a list of all directories in current directory, and then fzf will allow you to select directory you want to move file to. Once you have selected directory, Ranger will move file to selected directory.

Conclusion

fzf is a powerful command-line tool that can make working with Linux command line easier and more efficient. It allows you to search through files, directories, and other types of data quickly and easily, and its highly customizable nature means you can tailor it to your preferences. Whether you're a system administrator, developer, or power user, fzf is a tool you should definitely consider adding to your toolkit.

Updated on: 24-Mar-2023

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements