mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-13 22:30:37 +08:00
288 lines
9.3 KiB
Markdown
288 lines
9.3 KiB
Markdown
[#]: subject: "Fuzzy File Search in Linux"
|
|
[#]: via: "https://itsfoss.com/fuzzy-file-search-linux/"
|
|
[#]: author: "Sreenath https://itsfoss.com/author/sreenath/"
|
|
[#]: collector: "lkxed"
|
|
[#]: translator: " "
|
|
[#]: reviewer: " "
|
|
[#]: publisher: " "
|
|
[#]: url: " "
|
|
|
|
Fuzzy File Search in Linux
|
|
======
|
|
|
|
How do you [find files in the Linux command line][1]? You use the [find command][2]. That's the standard answer and there is nothing wrong with it.
|
|
|
|
Usually, you type the command with your search parameters, press enter and it displays the findings.
|
|
|
|
You can improve your file-finding experience in the terminal with fuzzy search.
|
|
|
|
Fuzzy search is an approximate search algorithm or technique. Here, files from a specified location are searched by name and the user will get real-time results.
|
|
|
|
Fuzzy search is popular in web search engines, where a user starts entering the term, and it starts showing results related to the term.
|
|
|
|
In this article, I am going to discuss two CLI tools that give you the ability to perform fuzzy searches in Linux:
|
|
|
|
- Fzf: Fuzzy finder
|
|
- Fzy: Fuzzy selector
|
|
|
|
### Fzf, the Fuzzy Finder in Linux
|
|
|
|
Fzf is a fuzzy search tool available for Linux, where you can search for files interactively.
|
|
|
|
To install `fzf` in Ubuntu, open a terminal and run:
|
|
|
|
```
|
|
sudo apt install fzf
|
|
```
|
|
|
|
While `fzf` itself works properly, it is wise to use it in conjunction with other tools to make most out of it.
|
|
|
|
#### Using fzf
|
|
|
|
Open a terminal and run:
|
|
|
|
```
|
|
fzf
|
|
```
|
|
|
|
This will open a prompt of `fzf` where you can search for files in the current working directory.
|
|
|
|
![Running fzf command in terminal][3]
|
|
|
|
##### Apply a border to fzf
|
|
|
|
You can use the `--border` option of fzf. There are several options like rounded, sharp etc.
|
|
|
|
```
|
|
fzf --border=rounded
|
|
```
|
|
|
|
![Running fzf command wth border option set to rounded and sharp][4]
|
|
|
|
##### Apply background and foreground color
|
|
|
|
Using the color property, you can set ANSI colors to `fzf` either as background, foreground or both.
|
|
|
|
![Colored output for fzf, where the colors are specified by the user][5]
|
|
|
|
```
|
|
fzf --color="bg:black,fg:yellow"
|
|
```
|
|
|
|
You can concatenate the options to make `fzf` visually pleasing.
|
|
|
|
Now, let me show some practical usage of the fuzzy search with fzf.
|
|
|
|
#### Use fzf to search within bash history
|
|
|
|
Of course, there is CTRL+R reverse search in the bash history. But if you want to use `fzf` to get a better look, run:
|
|
|
|
```
|
|
history | fzf
|
|
```
|
|
|
|
![Using fzf fuzzy search to search within the bash history][6]
|
|
|
|
#### Use fzf with tree command
|
|
|
|
[Tree command][7] lists files and directories along with their hierarchical connection.
|
|
|
|
Using `fzf` with `tree` command can help you find the absolute path of a particular file.
|
|
|
|
```
|
|
tree -afR /home/$USER | fzf
|
|
```
|
|
|
|
![Running Tree command and piping the output to Fuzzy search][8]
|
|
|
|
> 💡 The above command will invoke`tree`and list all files (-a) including hidden ones in a recursive fashion (-R). Also, the`-f`option tells tree to list the full path.
|
|
|
|
#### Preview files in fzf
|
|
|
|
Sometimes, it will be helpful if you get a small preview of the file you are searching.
|
|
|
|
Luckily, `fzf` provides a preview option. You can access it by using `--preview`. I am here using `find`command to make it even more useful.
|
|
|
|
```
|
|
find /home/$USER -type f | fzf --preview 'less {}'
|
|
```
|
|
|
|
Here, while you scroll through the result, it will display the text files using less.
|
|
|
|
> 🚧 If you are using other commands like`ls`, etc. do not use options like`-l`, that will display added details (file permissions). These additional details will break the required format needed for`fzf`preview. the hile using preview feature, the input to`fzf`should only be the filename.
|
|
|
|
If you have `bat` installed, you can use it for previewing files as well.
|
|
|
|
```
|
|
find /home/$USER -type f | fzf --preview 'bat --color always {}'
|
|
```
|
|
|
|
![Using bat as the text viewer for FZF preview function][9]
|
|
|
|
For Ubuntu users, bat is available as `batcat`. So run:
|
|
|
|
```
|
|
find /home/$USER -type f | fzf --preview 'batcat --color always {}'
|
|
```
|
|
|
|
> 💡 [Create an alias][10]for these commands, so that you don't want to type these again and again.
|
|
|
|
#### Use fzf to cd into any directory from anywhere (advance)
|
|
|
|
This is a bit trickier than the previous. Here, you cannot just directly pipe `fzf` and `cd` together, because both are different processes.
|
|
|
|
You can create an alias use the command like:
|
|
|
|
```
|
|
cd $(find /home/$USER -type d | fzf)
|
|
```
|
|
|
|
Or, you can follow the method explained below.
|
|
|
|
To do this, you may need to add a function to your bashrc. Let me call this function as `finder`. Now add the following lines to your bashrc.
|
|
|
|
```
|
|
finder() {
|
|
local dir
|
|
dir=$(find required/location/to/search/and/enter -type d | fzf)
|
|
if [[ -n "$dir" ]]; then
|
|
cd "$dir" || return
|
|
fi
|
|
}
|
|
```
|
|
|
|
Now, you should [enter the location][11] where the directories you want to search and enter are present.
|
|
|
|
For example, I have replaced that part with `/home/$USER` to indicate that I have to `cd` into any directories in my Home from anywhere.
|
|
|
|
Once you saved your bashrc, either restart the terminal or run:
|
|
|
|
```
|
|
source ~/.bashrc
|
|
```
|
|
|
|
After this, you can run finder from the terminal and once you located the directory you want to enter, press Enter key.
|
|
|
|
![Use fzf command to enter into any directory with the help of cd command][12]
|
|
|
|
#### Copy the selection to Clipboard
|
|
|
|
Till now, you have seen using `fzf` and in all cases, it gives either a search result or preview.
|
|
|
|
Now, if you want to copy the location of an item, you don't necessarily need to do it manually. There is a solution for that too.
|
|
|
|
First, make sure you have Xclip installed.
|
|
|
|
```
|
|
sudo apt install xclip
|
|
```
|
|
|
|
Now pipe it to xclip like this:
|
|
|
|
```
|
|
fzf | xclip -selection clipboard
|
|
```
|
|
|
|
This will copy whatever lines you have pressed the enter key, on to your clipboard.
|
|
|
|
#### Other Uses
|
|
|
|
Like I said earlier, you can use any command that involves a significant amount of text, and you want to search for a particular thing interactively.
|
|
|
|
- `cat ~/.bashrc | fzf` - Search Inside Bashrc
|
|
- `lsblk | fzf` - Search inside the list of lock devices
|
|
- `ps -aux | fzf` - Search inside process list
|
|
|
|
### Another choice: Fzy, the Fuzzy Selector
|
|
|
|
Unlike `fzf`, `fzy` is a fuzzy selector, where you will be provided a menu to select, depending on the input.
|
|
|
|
For example, if you are using `fzy` in conjunction with `ls` command, it will give you a menu like interface.
|
|
|
|
![FZY command with ls][13]
|
|
|
|
By default, it will show you ten entries in view.
|
|
|
|
#### Enter into a directory using fzy
|
|
|
|
Similar to fzf, fzy can also be used to enter into a directory in the current working directory using:
|
|
|
|
```
|
|
cd $(find -type d | fzy)
|
|
```
|
|
|
|
![][14]
|
|
|
|
#### Open a file using any editor
|
|
|
|
Or open a file using your favorite editor by:
|
|
|
|
```
|
|
nano $(find -type f | fzy)
|
|
```
|
|
|
|
![][15]
|
|
|
|
### Bonus: A Customized file and image preview
|
|
|
|
The below command will open a dedicated customized prompt in **Ubuntu** for fuzzy search, where you can preview text files by scrolling through them.
|
|
|
|
```
|
|
find /home/$USER -type f | fzf --color="bg:black,fg:yellow" --preview 'batcat --color always {}' --preview-window=bottom
|
|
```
|
|
|
|
Create an alias for this in your bashrc for easy access.
|
|
|
|
Or preview an image in fzf while scrolling using `timg` command line image viewer. Install it using:
|
|
|
|
```
|
|
sudo apt install timg
|
|
```
|
|
|
|
> 🚧 Remember that the image viewer will not display a proper image, as that is not the primary purpose of fzf preview
|
|
|
|
```
|
|
fzf --preview 'timg -g 200x100 {}' --preview-window=right:90
|
|
```
|
|
|
|
For those who are tinkerers, try to make this part by refining.
|
|
|
|
### Modern alternatives to rescue
|
|
|
|
Most Linux commands have been inherited from UNIX-era. They are old but work as expected. But this doesn't mean they cannot be improved.
|
|
|
|
I mean, you don't need to reinvent the wheel but you can always work on improving the wheels.
|
|
|
|
Modern tools like fzf and fzy take the file search in Linux terminal to the next level. Here are some other such command line tools you may find interesting.
|
|
|
|
I have tried giving some practical examples for these fuzzy search tools. I hope you find them inspiring enough for you. Let me know in the comments if you are going to use them.
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
via: https://itsfoss.com/fuzzy-file-search-linux/
|
|
|
|
作者:[Sreenath][a]
|
|
选题:[lkxed][b]
|
|
译者:[译者ID](https://github.com/译者ID)
|
|
校对:[校对者ID](https://github.com/校对者ID)
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
[a]: https://itsfoss.com/author/sreenath/
|
|
[b]: https://github.com/lkxed/
|
|
[1]: https://learnubuntu.com:443/find-files/
|
|
[2]: https://linuxhandbook.com:443/find-command-examples/
|
|
[3]: https://itsfoss.com/content/images/2023/06/fzf-default.svg
|
|
[4]: https://itsfoss.com/content/images/2023/06/fzf-border.svg
|
|
[5]: https://itsfoss.com/content/images/2023/06/fzf-colored.svg
|
|
[6]: https://itsfoss.com/content/images/2023/06/search-within-bash-history.svg
|
|
[7]: https://linuxhandbook.com:443/tree-command/
|
|
[8]: https://itsfoss.com/content/images/2023/06/tree-afr.svg
|
|
[9]: https://itsfoss.com/content/images/2023/06/bashrc-preview-in-fzf.png
|
|
[10]: https://linuxhandbook.com:443/linux-alias-command/
|
|
[11]: https://itsfoss.com/change-directories/
|
|
[12]: https://itsfoss.com/content/images/2023/06/fzf-cd-combo.svg
|
|
[13]: https://itsfoss.com/content/images/2023/06/fzy-ls.svg
|
|
[14]: https://itsfoss.com/content/images/2023/06/fzy-cd.svg
|
|
[15]: https://itsfoss.com/content/images/2023/06/fzy-nano.svg
|