Linux控制台中的模糊文件搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9439121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:48:24  来源:igfitidea点击:

Fuzzy file search in linux console

linuxbashfile-search

提问by nab

Does anybody know a way to perform a quick fuzzy search from linux console?

有人知道从 linux 控制台执行快速模糊搜索的方法吗?

Quite often I come accross situation when I need to find a file in a project but I don't remember the exact filename. In Sublime text editorI would press Ctrl-P and type a part of the name, which will produce a list of the files to select from. That's an amazing feature I'm quite happy with. The problem is that in most cases I have to browse a code in a console on remote machines via ssh. So I'm wondering is there a tool similar to "Go Anywhere" feature for Linux console?

当我需要在项目中查找文件但我不记得确切的文件名时,我经常会遇到这种情况。在Sublime 文本编辑器中,我会按 Ctrl-P 并输入名称的一部分,这将生成一个可供选择的文件列表。这是一个我很满意的惊人功能。问题是,在大多数情况下,我必须通过 ssh 在远程机器上的控制台中浏览代码。所以我想知道是否有类似于 Linux 控制台的“Go Anywhere”功能的工具?

采纳答案by Junegunn Choi

You may find fzfuseful. It's a general purpose fuzzy finder written in Go that can be used with any list of things: files, processes, command history, git branches, etc.

您可能会发现fzf很有用。这是一个用 Go 编写的通用模糊查找器,可用于任何事物列表:文件、进程、命令历史、git 分支等。

Its install script will setup CTRL-Tkeybinding for your shell. The following GIF shows how it works.

它的安装脚本将为CTRL-T您的 shell设置键绑定。下面的 GIF 展示了它是如何工作的。

回答by Tony Laidig

You might want to try AGREPor something else that uses the TRERegular Expression library.

您可能想尝试使用 AGREP或其他使用TRE正则表达式库的东西。

回答by Adiel Mittmann

I don't know how familiar you are with the terminal, but this could help you:

我不知道您对终端有多熟悉,但这可以帮助您:

find | grep 'report'
find | grep 'report.*2008'

Sorry if you already know grepand were looking for something more advanced.

抱歉,如果您已经知道grep并且正在寻找更高级的东西。

回答by Snake007uk

You can do the following

您可以执行以下操作

grep -iR "text to search for" .

where "." being the starting point, so you could do something like

在哪里 ”。” 作为起点,所以你可以做类似的事情

grep -iR "text to search" /home/

This will make grep search for the given text inside every file under /home/ and list files which contain that text.

这将使 grep 在 /home/ 下的每个文件中搜索给定文本,并列出包含该文本的文件。

回答by mmrtnt

I usually use:

我通常使用:

ls -R | grep  -i [whatever I can remember of the file name]

From a directory above where I expect the file to be - the higher up you go in the directory tree, the slower this is going to go.

从上面我希望文件所在的目录开始 - 在目录树中越往上走,这个过程就越慢。

When I find the the exact file name, I use it in find:

当我找到确切的文件名时,我在查找中使用它:

find . [discovered file name]

This could be collapsed into one line:

这可以折叠成一行:

for f in $(ls --color=never -R | grep --color=never -i partialName); do find -name $f; done

(I found a problem with ls and grep being aliased to "--color=auto")

(我发现 ls 和 grep 别名为“--color=auto”有问题)

回答by Yanick Girouard

You could use find like this for complex regex:

对于复杂的正则表达式,您可以像这样使用 find:

find . -type f -regextype posix-extended -iregex ".*YOUR_PARTIAL_NAME.*" -print

find . -type f -regextype posix-extended -iregex ".*YOUR_PARTIAL_NAME.*" -print

Or this for simplier glob-like matches:

或者这对于更简单的 glob-like 匹配:

find . -type f -name "*YOUR_PARTIAL_NAME*" -print

find . -type f -name "*YOUR_PARTIAL_NAME*" -print

Or you could also use find2perl (which is quite faster and more optimized than find), like this:

或者你也可以使用 find2perl(它比 find 更快,更优化),如下所示:

find2perl . -type f -name "*YOUR_PARTIAL_NAME*" -print | perl

find2perl . -type f -name "*YOUR_PARTIAL_NAME*" -print | perl

If you just want to see how Perl does it, remove the | perlpart and you'll see the code it generates. It's a very good way to learn by the way.

如果您只是想看看 Perl 是如何做到的,请删除该| perl部分,您将看到它生成的代码。顺便说一句,这是一个很好的学习方式。

Alternatively, write a quick bash wrapper like this, and call it whenever you want:

或者,编写一个像这样的快速 bash 包装器,并在需要时调用它:

#! /bin/bash
FIND_BASE=""
GLOB_PATTERN=""
if [ $# -ne 2 ]; then
    echo "Syntax: $(basename 
function joinstr { local IFS=""; shift; echo "$*"; }
function fcd { cd $(joinstr \* $(echo "$*" | fold -w1))* }
) <FIND_BASE> <GLOB_PATTERN>" else find2perl "$FIND_BASE" -type f -name "*$GLOB_PATTERN*" -print | perl fi

Name this something like qsearchand then call it like this: qsearch . something

将其命名为类似的名称qsearch,然后像这样调用它:qsearch . something

回答by Chris Farmiloe

The fasdshell script is probably worth taking a look at too.

胎儿酒精中毒综合症shell脚本可能是值得考虑看看了。

fasdoffers quick access to files and directories for POSIX shells. It is inspired by tools like autojump, z and v. Fasd keeps track of files and directories you have accessed, so that you can quickly reference them in the command line.

fasd为 POSIX shell 提供对文件和目录的快速访问。它的灵感来自 autojump、z 和 v 等工具。Fasd 会跟踪您访问过的文件和目录,以便您可以在命令行中快速引用它们。

It differs a little from a complete find of all files, as it only searches recently openedfiles. However it is still very useful.

它与完整查找所有文件略有不同,因为它只搜索最近打开的文件。不过还是很有用的。

回答by dlonie

Most of these answers won't do fuzzy searching like sublime text does it -- they may match part of the answer, but they don't do the nice 'just find all the letters in this order' behavior.

这些答案中的大多数不会像 sublime text 那样进行模糊搜索——它们可能会匹配答案的一部分,但它们并没有很好地“按此顺序查找所有字母”行为。

I think this is a bit closer to what you want. I put together a special version of cd ('fcd') that uses fuzzy searching to find the target directory. Super simple -- just add this to your bashrc:

我认为这更接近你想要的。我整理了一个特殊版本的 cd ('fcd'),它使用模糊搜索来查找目标目录。超级简单——只需将其添加到您的 bashrc 中:

/home/dave/results/sample/today

This will add an * between each letter in the input, so if I want to go to, for instance,

这将在输入的每个字母之间添加一个 *,所以如果我想去,例如,

fcd /h/d/r/spl/t
fcd /h/d/r/s/t
fcd /h/d/r/sam/t
fcd /h/d/r/s/ty

I can just type any of the following:

我可以只输入以下任何一项:

$(joinstr \* $(echo "$*" | fold -w1))*

Using the first as an example, this will execute cd /*h*/*d*/*r*/*s*p*l*/*t*and let the shell sort out what actually matches.

以第一个为例,这将执行cd /*h*/*d*/*r*/*s*p*l*/*t*并让 shell 找出实际匹配的内容。

As long as the first character is correct, and one letter from each directory in the path is written, it will find what you're looking for. Perhaps you can adapt this for your needs? The important bit is:

只要第一个字符是正确的,并且在路径中的每个目录中写入一个字母,它就会找到您要查找的内容。也许您可以根据自己的需要调整它?重要的一点是:

find . -iname '*foo*'

which creates the fuzzy search string.

这将创建模糊搜索字符串。

回答by user1338062

##代码##

Case insensitive find of filenames containing foo.

查找包含foo.

回答by user1338062

fdis a simple, fast and user-friendly alternative to find.

fd是一种简单、快速且用户友好的查找替代方案。

Demo from the GitHub project page:

来自 GitHub 项目页面的演示: