Search files by contents from terminal

    I think many people have repeatedly encountered such a problem as the need to find a file in a folder with files (and sometimes even subfolders, in which it would also be nice to search), knowing a piece of its text (well, or guessing about it). I also stumbled over this need many times, and now I finally got together and wrote a small bash script that performed this task.

    image



    The script is called with the line.
    textfind %name%
    You can also set the file name template (the second argument) and the color that displays the names of the files in which the searched passage is found (green by default, to set the color you need to install the set_color utility, under ubuntu - sudo apt-get install fish) .

    Here is the script itself: You can download it from the link: Script

    #!/bin/bash
    cmd="find -type f -print "
    set_color_cmd="set_color"
    if [ $2 ]; then cmd="$cmd -name \"$2\""; fi
    color="green"
    if [ $3 ]; then color=$3; fi

    is_colored=1
    hh=$(which "$set_color_cmd")
    if [ $? -ne 0 ]; then is_colored=''; fi

    $cmd | while read f; do
    cnt=$(grep -c "$1" "$f")
    if [ $cnt -gt 0 ]; then
    if [ $is_colored ]; then "$set_color_cmd" "$color"; fi
    echo "$f"
    if [ $is_colored ]; then "$set_color_cmd" normal; fi
    grep -n "$1" "$f"
    fi
    done




    Also popular now: