diff --git a/published/20230611.0 ⭐️⭐️ Fuzzy File Search in Linux.md b/published/20230611.0 ⭐️⭐️ Fuzzy File Search in Linux.md new file mode 100644 index 0000000000..4134fa8d81 --- /dev/null +++ b/published/20230611.0 ⭐️⭐️ Fuzzy File Search in Linux.md @@ -0,0 +1,294 @@ +[#]: subject: "Fuzzy File Search in Linux" +[#]: via: "https://itsfoss.com/fuzzy-file-search-linux/" +[#]: author: "Sreenath https://itsfoss.com/author/sreenath/" +[#]: collector: "lkxed" +[#]: translator: "ChatGPT" +[#]: reviewer: "wxy" +[#]: publisher: "wxy" +[#]: url: "https://linux.cn/article-15937-1.html" + +Linux 中的模糊文件搜索 +====== + +![][0] + +> 像 fzf 和 fzy 这样的现代工具将 Linux 终端中的文件搜索提升到了一个新的水平。 + +在 Linux 命令行中,如何 [搜索文件][1]?你可以使用 [find 命令][2]。这是标准答案,没有问题。 + +通常,你键入带有搜索参数的命令,按回车键,然后它会显示搜索结果。 + +你可以通过模糊搜索来提升终端中的文件搜索体验。 + +模糊搜索是一种近似搜索算法或技术。在这种搜索中,通过名称搜索指定位置的文件,并实时显示结果给用户。 + +模糊搜索在网络搜索引擎中很受欢迎,用户开始输入术语后,它会开始显示与该术语相关的结果。 + +在本文中,我将讨论两个命令行工具,它们可以让你在 Linux 中执行模糊搜索: + +- `fzf`:模糊查找工具 +- `fzy`:模糊选择工具 + +### fzf:Linux 中的模糊查找工具 + +`fzf` 是一款可用于 Linux 的模糊搜索工具,你可以通过它进行交互式文件搜索。 + +在 Ubuntu 中安装 `fzf`,打开终端并运行以下命令: + +``` +sudo apt install fzf +``` + +虽然 `fzf` 本身可以正常工作,但最好与其他工具配合使用,以充分发挥其功能。 + +#### 使用 fzf + +打开终端并运行: + +``` +fzf +``` + +这将打开一个 `fzf` 提示符,在当前工作目录中搜索文件。 + +![在终端中运行 fzf 命令][3] + +##### 为 fzf 应用边框 + +你可以使用 `--border` 选项为 `fzf` 应用边框,有多种边框可用,如 `rounded`(圆角)、`sharp`(尖角)等。 + +``` +fzf --border=rounded +``` + +![运行带有边框选项设置为 rounded 和 sharp 的 fzf 命令][4] + +##### 应用背景和前景颜色 + +使用颜色属性,你可以为 `fzf` 设置 ANSI 颜色,可以作为背景、前景或两者都设置。 + +![为 fzf 应用颜色,用户指定了颜色][5] + +``` +fzf --color="bg:black,fg:yellow" +``` + +你可以串联这些选项,使 `fzf` 在视觉上更加美观。 + +现在,让我展示一些 `fzf` 模糊搜索的实际用法。 + +#### 使用 fzf 在 Bash 历史中进行搜索 + +当然,Bash 历史记录中有 `CTRL+R` 的反向搜索功能。但如果你想使用 `fzf` 来获得更好的外观,可以运行以下命令: + +``` +history | fzf +``` + +![使用 fzf 模糊搜索在 bash 历史中进行搜索][6] + +#### 使用 fzf 结合 tree 命令 + +[tree 命令][7] 会列出文件和目录,并显示它们的层级关系。 + +使用 `fzf` 结合 `tree` 命令可以帮助你找到特定文件的绝对路径。 + +``` +tree -afR /home/$USER | fzf +``` + +![运行 Tree 命令并将输出传递给模糊搜索][8] + +> 💡 上述命令会调用 `tree` 并以递归方式列出包括隐藏文件在内的所有文件(`-a`)。同时,`-f` 选项告诉 `tree` 列出完整路径。 + +#### 在 fzf 中预览文件 + +有时,如果你可以获得你搜索的文件的小型预览,那会很有帮助。 + +幸运的是,`fzf` 提供了一个预览选项。你可以使用 `--preview` 来访问它。我在这里使用 `find` 命令使其更加有用。 + +``` +find /home/$USER -type f | fzf --preview 'less {}' +``` + +在这里,当你滚动浏览结果时,它将使用 `less` 显示文本文件。 + +> 🚧 如果你使用其他命令如 `ls` 等,请不要使用 `-l` 等选项,因为这将显示额外的详细信息(文件权限)。这些额外的详细信息会破坏 `fzf` 预览所需的格式。在使用预览功能时,输入到 `fzf` 的应该只是文件名。 + +如果你已安装了 `bat`,也可以使用它来预览文件。 + +``` +find /home/$USER -type f | fzf --preview 'bat --color always {}' +``` + +![使用 bat 作为 FZF 预览功能的文本查看器][9] + +对于 Ubuntu 用户,可以使用 `batcat` 来调用 `bat`。因此运行: + +``` +find /home/$USER -type f | fzf --preview 'batcat --color always {}' +``` + +> 💡 [为这些命令创建别名][10],这样你就不需要反复输入它们。 + +#### 从任何地方使用 fzf 进入任何目录(高级技巧) + +这比以前要复杂一些。在这里,你不能直接将 `fzf` 和 `cd` 连接在一起,因为它们是不同的进程。 + +你可以创建一个别名并使用以下命令: + +``` +cd $(find /home/$USER -type d | fzf) +``` + +或者,你可以按照下面解释的方法进行操作。 + +为此,你可能需要在 `bashrc` 中添加一个函数。让我将这个函数称为 `finder`。现在请添加以下行到你的 `bashrc` 中。 + +``` +finder() { + local dir + dir=$(find required/location/to/search/and/enter -type d | fzf) + if [[ -n "$dir" ]]; then + cd "$dir" || return + fi +} +``` + +现在,你应该 [输入路径][11],其中包含你要搜索并进入的目录。 + +例如,我已经使用 `/home/$USER` 替换了该部分,表示我要从任何位置进入我的主目录中的任何目录。 + +保存你的 `bashrc` 文件后,要么重启终端,要么运行以下命令: + +``` +source ~/.bashrc +``` + +之后,你可以在终端上运行 `finder` 命令,一旦找到要进入的目录,按回车键即可。 + +![使用 fzf 命令结合 cd 命令进入任意目录][12] + +#### 将选择内容复制到剪贴板 + +到目前为止,你已经了解了如何使用 `fzf`,它提供了搜索结果或预览。 + +现在,如果你想要复制某个项目的位置,你不必手动执行此操作。也有相应的解决方案。 + +首先,确保你已经安装了 `xclip`。 + +``` +sudo apt install xclip +``` + +然后像这样将其传递给 `xclip`: + +``` +fzf | xclip -selection clipboard +``` + +这将复制你按下回车键的那些行到剪贴板上。 + +#### 其他用途 + +正如我之前所说,你可以使用任何涉及大量文本,并希望交互式搜索特定内容的命令。 + +- `cat ~/.bashrc | fzf` - 在 Bashrc 文件中搜索 +- `lsblk | fzf` - 在锁定设备列表中搜索 +- `ps -aux | fzf` - 在进程列表中搜索 + +### 另一个选择:Fzy,模糊选择器 + +与 `fzf` 不同,`fzy` 是一个模糊选择器,它会根据输入提供一个菜单供你选择。 + +例如,如果你将 `fzy` 与 `ls` 命令一起使用,它将给你提供一个类似菜单的界面。 + +![使用 ls 命令的 fzy 命令][13] + +默认情况下,它会显示十个条目。 + +#### 使用 fzy 进入目录 + +与 `fzf` 类似,你也可以使用 `fzy` 进入当前工作目录中的目录: + +``` +cd $(find -type d | fzy) +``` + +![][14] + +#### 使用任何编辑器打开文件 + +或者使用你喜欢的编辑器打开文件: + +``` +nano $(find -type f | fzy) +``` + +![][15] + +### 附加内容:自定义文件和图像预览 + +下面的命令将在 **Ubuntu** 中打开一个专门的自定义提示符,用于模糊搜索,你可以通过滚动来预览文本文件。 + +``` +find /home/$USER -type f | fzf --color="bg:black,fg:yellow" --preview 'batcat --color always {}' --preview-window=bottom +``` + +为了方便访问,可以在你的 `bashrc` 文件中为此创建一个别名。 + +或者在使用 `timg` 命令行图像查看器时,在 `fzf` 中进行图像预览并滚动。使用以下命令进行安装: + +``` +sudo apt install timg +``` + +> 🚧 请注意,图像查看器无法正确显示图像,因为这不是 `fzf` 预览的主要目的。 + +``` +fzf --preview 'timg -g 200x100 {}' --preview-window=right:90 +``` + +对于那些喜欢折腾的人,可以尝试对此部分进行优化。 + +### 现代化的替代方案 + +大多数 Linux 命令都是从 UNIX 时代继承下来的。它们虽然老旧,但功能如预期。但这并不意味着它们不能改进。 + +我的意思是,你不需要重新发明轮子,但你总是可以努力改进轮子。 + +像 `fzf` 和 `fzy` 这样的现代化工具将 Linux 终端中的文件搜索提升到了一个新的水平。以下是一些其他有趣的命令行工具。 + +我尝试给出了这些模糊搜索工具的一些实际示例。希望你对它们感到足够有启发性。如果你打算使用它们,请在评论中告诉我。 + +*(题图:MJ/d25e71fa-f24e-49be-9579-e0520a8f6e18)* + +-------------------------------------------------------------------------------- + +via: https://itsfoss.com/fuzzy-file-search-linux/ + +作者:[Sreenath][a] +选题:[lkxed][b] +译者:ChatGPT +校对:[wxy](https://github.com/wxy) + +本文由 [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 +[0]: https://img.linux.net.cn/data/attachment/album/202306/24/001013mc7chh224c7uls8h.jpg \ No newline at end of file diff --git a/sources/tech/20230611.0 ⭐️⭐️ Fuzzy File Search in Linux.md b/sources/tech/20230611.0 ⭐️⭐️ Fuzzy File Search in Linux.md deleted file mode 100644 index 5824e4a212..0000000000 --- a/sources/tech/20230611.0 ⭐️⭐️ Fuzzy File Search in Linux.md +++ /dev/null @@ -1,287 +0,0 @@ -[#]: 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