176

I want to find a text file in my hard disk which contains a specific word.

Prior to Ubuntu 12.4 I used to start in the dash an application, I think it was called "Search for file...", whose icon was a magnifying glass.I can't find that simple application any more.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
SomeoneMe
  • 1,761

7 Answers7

274

You can use the grep command from terminal:

 grep -r word *

This command will find all occurrences of "word" in all the files under the current directory (or subdrectories).

Marco Ceppi
  • 48,101
Steve Chapel
  • 2,791
74

Install gnome-search-tool.

sudo apt-get install gnome-search-tool

Open Search for files select Select More Options and


enter image description here
Achu
  • 21,237
hingev
  • 6,596
  • 1
    do you have to restart the OS to get this to work? or maybe it doesn't work in 12? – jcollum Apr 18 '13 at 20:55
  • have you done install part ? I am pretty sure it must work, it works on 13.04. – hingev Apr 19 '13 at 11:29
  • which gnome-search-tool = /usr/bin/gnome-search-tool... but when I open the search option in gnome (Go, Search for files...) there's no option for "Select More Options" – jcollum Apr 19 '13 at 17:25
  • 1
    open by typing in terminal: gnome-search-tool and I am sure you will see it. – hingev Apr 20 '13 at 12:49
  • You can launch gnome-search-tool via the dash "Search for files", so you don't need the terminal. – Bernard Decock Oct 22 '13 at 07:41
  • as weird as it may sound, but launching applications from gnome or unity launch different apps, like gnome-settings for example – hingev Oct 22 '13 at 19:16
  • 6
    unfortunately, gnome-search-tool was removed from ubuntu – Yrogirg Sep 18 '19 at 00:55
  • 1
    As suggested here, use mate-search-tool instead which seems to offer identical functionality. Install via sudo apt install mate-utils. – fdetsch Jul 28 '21 at 08:11
23

Here's an overview of different methods that one can use for searching files for specific strings of text, with a few options added specifically to work only with text files, and ignore binary/application files.

One should note,however,that searching for word can get a little complex, because most line-matching tools will try to find a word anywhere on the line. If we're talking about a word as string that could appear in the beginning or end of line, or alone on the line, or surrounded by spaces and/or punctuation - that's when we'll need regular expressions, and especially those that come from Perl. Here, for example, we can use -P in grep to make use of Perl regular expressions to surround it.

$ printf "A-well-a don't you know about the bird?\nWell, everybody knows that the bird is a word" | grep -noP '\bbird\b'                                               
1:bird
2:bird

Simple grep

$ grep -rIH  'word'
  • -r for recursive search down from current directory
  • -I to ignore binary files
  • -H to output filename where match is found

Suitable for searching only.

find + grep

$ find -type f -exec grep -IH 'word' {} \;
  • find does the recursive search part
  • -I option is to ignore binary files
  • -H to output filename where line is found
  • good approach for combining with other commands within subshell, like:

    $ find -type f -exec sh -c 'grep -IHq "word" "$1" && echo "Found in $1"' sh {} \;
    

Perl

#!/usr/bin/env perl
use File::Find;
use strict;
use warnings;

sub find_word{
    return unless -f;
    if (open(my $fh, $File::Find::name)){
        while(my $line = <$fh>){
            if ($line =~ /\bword\b/){
                printf "%s\n", $File::Find::name;
                close($fh);
                return;
            }
        }
    }
}

# this assumes we're going down from current working directory
find({ wanted => \&find_word, no_chdir => 1 },".")

poor-mans recursive grep in recursive bash script

This is the "bash way". Not ideal, probably no good reason to use this when you have grep or perl installed.

#!/usr/bin/env bash
shopt -s globstar
#set -x
grep_line(){
    # note that this is simple pattern matching 
    # If we wanted to search for whole words, we could use
    # word|word\ |\ word|\ word\ )
    # although when we consider punctuation characters as well - it gets more
    # complex
    case "$1" in
        *word*) printf "%s\n" "$2";;
    esac
}
readlines(){
    #  line count variable can be used to output on which line match occured

    #line_count=1
    while IFS= read -r line;
    do
        grep_line "$line" "$filename"
        #line_count=$(($line_count+1))
    done < "$1"
}

is_text_file(){
    # alternatively, mimetype command could be used
    # with *\ text\/* as pattern in case statement
    case "$(file -b --mime-type "$1")" in
        text\/*) return 0;;
        *) return 1;;
    esac
}

main(){
    for filename in ./**/*
    do
        if [ -f "$filename" ] && is_text_file "$filename"
        then
            readlines "$filename"
        fi
    done
}
main "$@"
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
6

Question is quite old... anyway... currently (2016) there is a gnome app called tracker (you can find it in ubuntu repositories) that can be installed to search for text inside files (tried odt-ods-odp-pdf). The package comes with 4 other packages to be installed (tracker-extract, tracker-gui, tracker-miner-fs, tracker-utils) Namastè :)

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
cico
  • 61
  • 1
    Tracker is good software but it requires the index to already contain info about the file you are interested to hit it with a search. It uses less resources than Recoll, I'm not sure about the index size. But if you are in need to search a file with a specific text and want to do it with a gui, gnome-search-tool solves the problem without an index. It was a default app in previous Ubuntu versions, I don't know why they removed it without a replacement. – Hatoru Hansou Sep 27 '16 at 01:25
3

You can specify wildcards in case if you want to search in specific files.

For example: If you want to to find what are all *.conf files those have word SSLCertificateFile in them, you can run this on root:

sudo grep -rIH  'SSLCertificateFile' --include \*.conf
Atul
  • 161
  • 6
3

Yes, I know you was looking for gui application and this is old post, but maybe this help someone. I found ack-grep util. At first install it via sudo apt-get install ack-grep and then run command ack-grep what_you_looking_for in the directory where you want search. This show you all files in which is your text and also show preview from this files. And this is so important for me.

spajdo
  • 191
1

Even simpler and faster is "silver searcher" sudo apt-get install silversearcher-ag. Take a look at https://github.com/ggreer/the_silver_searcher for reassons on why it is better than ack* among others.