10.9 C
New York
Tuesday, November 29, 2022
HomeLinuxHow To Do A Fuzzy File Search in Linux

How To Do A Fuzzy File Search in Linux

In the conventional way of searching files or directories on Linux, we need to use the exact same name of the file for a perfect match. There are many tools and commands like “find command” or “fd command” to do the exact searching tasks on Linux. Here the fuzzy file search in Linux refers to a search without exactly knowing the file or directory name. This fuzzy file search allows the users to do a search for a query and get all the approximately matched search results. 

Fuzzy File Search in Linux


The fuzzy file search on Linux requires having the FZF tool installed on your machine. Then you can easily get started with the tool for fuzzy searches. Through the fuzzy search, you can look for both files and directories. In this post, we will see how to do fuzzy file searches on Linux. 

1. Installing FZF in Linux


The installation process of the FZF tool on a Linux machine is easy and straightforward. The installer files are already available on the official Linux repository. You can execute the below-mentioned commands on your terminal shell to get the application installed on your Linux machine. 

Install FZF on Debian/Ubuntu Linux 

sudo apt update
sudo apt install f2f

Get FZF on Fedora/Red Hat Linux 

$ sudo dnf install fzf

Install FZF on Arch Linux 

$ sudo pacman -S fzf

If you’re facing any issues while installing the app through the above-mentioned conventional way, you can use the Git method from below. First, you will need to clone the Git repository on your system. Then you can browse the directory and hit the installation command with the root permission. 

Fuzzy File Search in Linux

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

2. Keyboard Shortcuts for the Fuzzy Searches 


So far, we have seen how to install the Fuzzy search tool on Linux; here, we are going to see the major keyboard shortcuts that you might need to know. 

  • Ctrl+r Shows the command history.
  • Ctrl+t Search for the filename in the $PWD
  • Alt+c Shows the  directory name 
  • Alt+c  Activate the directory lookup function

Here, you can see a few points that will help you to make your fuzzy searches better on Linux.

Token Example Explanation
string string Searches for a string.
‘string Search files that have a quoted-string value.
^string ^LXF You can get a list of files that starts with the LXF format by an exact match.
.format$ .odt$ You can get a list of files that ends with the .odt format by an exact match.
!string !dhc It does the inverse exact match queries that do not include the DHC.
!^string ~^LXF Performs the prefix exact match searches with a string.
!.format$ !.odt$ Allows you to search in the inverse suffix match for those items that don’t match by the .odt format

3. Get Started with the Fuzzy Search On Linux 


As we already have the FZF tool installed on the Linux system, we can now just type fzf on the terminal shell and get started with the tool. 

$ fzf

You can save the directory or the path as an export on your system that you search through the fuzzy search with the below-mentioned commands. 

$ fzf >file
$ cat file
$ bat file

You can also add multiple commands as a conjugate way to do the fuzzy search on Linux. 

$ find ./bin/ -type f | fzf >file
$ cat file

4. Use Fuzzy Completion in Bash and Zsh


If you become a fan of the Fuzzy searches on Linux, you can add the fuzzy search parameters to your bash script in both temporary and permanent ways. The below-mentioned command will allow you to start the function. 

$ cat **<Tab>

cat star Fuzzy File Search in Linux

The below-mentioned environmental variables can also be used for working with these features. 

$ unset **<Tab>
$ unalias **<Tab>
$ export **<Tab>

You can also perform this command over an SSH or telnet tool for remote access. You can perform the fuzzy search on Linux in a remote connection with auto-fill host names and details by editing the  /etc/hosts and ~/.ssh/config directory and scripts. 

$ ssh **<Tab>

To end all the ongoing processes, you can run the below-mentioned kill command. 

$ kill -9 <Tab>

If you need to make changes permanently, you can edit the below-mentioned file script and add your custom commands. 

/etc/hosts and ~/.ssh/config.

5. Enable Fuzzy File Search in Linux with Vim Plugin


If you’re a fan of the Vim script editor, you can also enable the fuzzy file search parameters with the Vim tool. You can edit the Vim script from the below-mentioned configuration script. 

set rtp+=~/.fzf

You can also update the Fuzzy search tools with the Vim by executing the below-mentioned command. 

upgraded to latest version using following command

$ cd ~/.fzf && git pull && ./install

Insights!


Fuzzy searches on Linux are both fun and efficient. In the entire post, we have seen how to install the fzf tool and how to search files on Linux with fuzzy search. We have also seen a few ways to use the fuzzy search tool with bash and Vim. If you need to know more about the FZF tool, please visit this Github url for a better understanding. 

I hope this post has been valuable and informative for you. If yes, please share this post with your friends and the Linux community. You can also write down your opinions about this post in the communication section.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

spot_img

Latest Post

Must Read