Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve AJAX(interactive) kind of SEARCH in LINUX to FIND files?

I am interested in typing a search keyword in the terminal and able to see the output immediately and interactively. That means, like searching in google, I want to get results immediately after every character or word keyed-in.

I tought of doing this by combining WATCH command and FIND command but unable to bring the interactivenes.

Lets assume, to search for a file with name 'hint' in filename, I use the command

$ find | grep -i hint

this pretty much gives me the decent output results.

But what I want is the same behaviour interactively, that means with out retyping the command but only typing the SEARCH STRING.

I tought of writing a shell script which reads from a STDIN and executes the above PIPED-COMMAND for every 1 sec. Therefore what ever I type it takes that as an instruction every time for the command. But WATCH command is not interactive.

I am interested in below kind of OUTPUT:

$ hi
./hi
./hindi
./hint

$ hint
./hint

If anyone can help me with any better alternative way instead of my PSUEDO CODE, that is also nice

like image 510
Jagadish Talluri Avatar asked Jul 09 '15 10:07

Jagadish Talluri


People also ask

How do I search for a file in a bash script?

You need to utilize the “-L” option and the path and “-name” option in your command. The “*” in the name specification is used for searching “all” the bash files with “.


1 Answers

Stumbled aross this old question, found it interesting and thought I'd give it a try. This BASH script worked for me:

#!/bin/bash
# Set MINLEN to the minimum number of characters needed to start the    
# search. 
MINLEN=2
clear
echo "Start typing (minimum $MINLEN characters)..." 
# get one character without need for return 
while read -n 1 -s i
do
    # get ascii value of character to detect backspace
    n=`echo -n $i|od -i -An|tr -d " "`
    if (( $n == 127 )) # if character is a backspace...
    then 
        if (( ${#in} > 0 )) # ...and search string is not empty
        then 
            in=${in:0:${#in}-1} # shorten search string by one
            # could use ${in:0:-1} for bash >= 4.2 
        fi
    elif (( $n == 27 )) # if character is an escape...
    then
        exit 0 # ...then quit
    else # if any other char was typed... 
        in=$in$i # add it to the search string
    fi
    clear 
    echo "Search: \""$in"\"" # show search string on top of screen
    if (( ${#in} >= $MINLEN )) # if search string is long enough...
    then    
        find "$@" -iname "*$in*" # ...call find, pass it any parameters given
    fi
done

Hope this does what you intend(ed) to do. I included a "start dir" option, because the listings can get quite unwieldy if you search through a whole home folder or something. Just dump the $1 if you don't need it. Using the ascii value in $n it should be easily possible to include some hotkey functionality like quitting or saving results, too.

EDIT:

If you start the script it will display "Start typing..." and wait for keys to be pressed. If the search string is long enough (as defined by variable MINLEN) any key press will trigger a find run with the current search string (the grep seems kind of redundant here). The script passes any parameters given to find. This allows for better search results and shorter result lists. -type d for example will limit the search to directories, -xdev will keep the search on the current file sytem etc. (see man find). Backspaces will shorten the search string by one, while pressing Escape will quit the script. The current search string is displayed on top. I used -iname for the search to be case-insensitive. Change this to `-name' to get case-sensitive behaviour.

like image 161
some-non-descript-user Avatar answered Oct 05 '22 10:10

some-non-descript-user