Fuzzy file search in linux console

LinuxBashFile Search

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

You may find fzf useful. 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.

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

Solution 2 - Linux

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.

Solution 3 - Linux

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.

Solution 4 - Linux

find . -iname '*foo*'

Case insensitive find of filenames containing foo.

Solution 5 - Linux

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")

Solution 6 - Linux

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.

Solution 7 - Linux

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

Demo from the GitHub project page:

Solution 8 - Linux

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

(From their site:)

TRE is a lightweight, robust, and efficient POSIX compliant regexp matching library with some exciting features such as approximate (fuzzy) matching.
At the core of TRE is a new algorithm for regular expression matching with submatch addressing. The algorithm uses linear worst-case time in the length of the text being searched, and quadratic worst-case time in the length of the used regular expression. In other words, the time complexity of the algorithm is O(M2N), where M is the length of the regular expression and N is the length of the text. The used space is also quadratic on the length of the regex, but does not depend on the searched string. This quadratic behaviour occurs only on pathological cases which are probably very rare in practice.

TRE is not just yet another regexp matcher. TRE has some features which are not there in most free POSIX compatible implementations. Most of these features are not present in non-free implementations either, for that matter.

Approximate pattern matching allows matches to be approximate, that is, allows the matches to be close to the searched pattern under some measure of closeness. TRE uses the edit-distance measure (also known as the Levenshtein distance) where characters can be inserted, deleted, or substituted in the searched text in order to get an exact match. Each insertion, deletion, or substitution adds the distance, or cost, of the match. TRE can report the matches which have a cost lower than some given threshold value. TRE can also be used to search for matches with the lowest cost.

Solution 9 - Linux

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.

Solution 10 - Linux

You can try c- (Cminus), a fuzzy dir changing tool of bash script, which using bash completion. It is somehow limited by only matching visited paths, but really convenient and quite fast.

enter image description here

GitHub project: whitebob/cminus

Introduction on YouTube: https://youtu.be/b8Bem53Cz9A

Solution 11 - Linux

You could use find like this for complex regex:

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

Or this for simplier glob-like matches:

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 . -type f -name "*YOUR_PARTIAL_NAME*" -print | perl

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

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

#! /bin/bash
FIND_BASE="$1"
GLOB_PATTERN="$2"
if [ $# -ne 2 ]; then
    echo "Syntax: $(basename $0) <FIND_BASE> <GLOB_PATTERN>"
else
        find2perl "$FIND_BASE" -type f -name "*$GLOB_PATTERN*" -print | perl
fi

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

Solution 12 - Linux

Search zsh for file or folder in terminal and open or navigate to it with combination of find, fzf, vim and cd.

Install fzf in zsh and add script to ~/.zshrc, then reload shell source ~/.zshrc

fzf-file-search() {
   item="$(find '/' -type d \( -path '/proc/*' -o -path '/dev/*' \) -prune -false -o -iname '*' 2>/dev/null | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --rev    erse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@")"
  if [[ -d ${item} ]]; then
    cd "${item}" || return 1
  elif [[ -f ${item} ]]; then
    (vi "${item}" < /dev/tty) || return 1
  else
    return 1
  fi
   zle accept-line
}
zle     -N   fzf-file-search
bindkey '^f' fzf-file-search

Press keyboard shortcut 'Ctrl+F' to run it, this can be changed in bindkey '^f'. It searchs (find) through all files/folders (fzf) and depending on file type, navigate to directory (cd) or open file with text editor (vim).

Also quickly open recent files/folders with fasd:

fasd-fzf-cd-vi() {
   item="$(fasd -Rl "$1" | fzf -1 -0 --no-sort +m)"
  if [[ -d ${item} ]]; then
    cd "${item}" || return 1
  elif [[ -f ${item} ]]; then
    (vi "${item}" < /dev/tty) || return 1
  else
    return 1
  fi
   zle accept-line
}
zle -N fasd-fzf-cd-vi
bindkey '^e' fasd-fzf-cd-vi

Keyboard shortcut 'Ctrl+E'

Check other usefull tips and tricks for fast navigation inside terminal https://github.com/webdev4422/.dotfiles

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnabView Question on Stackoverflow
Solution 1 - LinuxJunegunn ChoiView Answer on Stackoverflow
Solution 2 - LinuxdlonieView Answer on Stackoverflow
Solution 3 - LinuxChris FarmiloeView Answer on Stackoverflow
Solution 4 - Linuxuser1338062View Answer on Stackoverflow
Solution 5 - LinuxmmrtntView Answer on Stackoverflow
Solution 6 - LinuxAdiel MittmannView Answer on Stackoverflow
Solution 7 - Linuxuser1338062View Answer on Stackoverflow
Solution 8 - LinuxTony LaidigView Answer on Stackoverflow
Solution 9 - LinuxSnake007ukView Answer on Stackoverflow
Solution 10 - LinuxwhitebobView Answer on Stackoverflow
Solution 11 - LinuxYanick GirouardView Answer on Stackoverflow
Solution 12 - Linuxwebdev4422View Answer on Stackoverflow