Fuzzy file search in linux console

2019-01-21 18:39发布

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

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 editor I 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?

11条回答
Luminary・发光体
2楼-- · 2019-01-21 19:19

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.

查看更多
爷、活的狠高调
3楼-- · 2019-01-21 19:21

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

Demo from the GitHub project page:

查看更多
Anthone
4楼-- · 2019-01-21 19:22

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 grep and were looking for something more advanced.

查看更多
戒情不戒烟
5楼-- · 2019-01-21 19:30

You might want to try AGREP or something else that uses the TRE Regular Expression library.

查看更多
Explosion°爆炸
6楼-- · 2019-01-21 19:31

The fasd shell script is probably worth taking a look at too.

fasd offers 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.

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

查看更多
smile是对你的礼貌
7楼-- · 2019-01-21 19:35

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.

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:

function joinstr { local IFS="$1"; shift; echo "$*"; }
function fcd { cd $(joinstr \* $(echo "$*" | fold -w1))* }

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

/home/dave/results/sample/today

I can just type any of the following:

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

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.

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:

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

which creates the fuzzy search string.

查看更多
登录 后发表回答