Linux find word in all files

grep (GNU or BSD)

You can use grep tool to search recursively the current folder, like:

grep -r "class foo" .

Note: -r — Recursively search subdirectories.

You can also use globbing syntax to search within specific files such as:

grep "class foo" **/*.c

Note: By using globbing option (**), it scans all the files recursively with specific extension or pattern. To enable this syntax, run: shopt -s globstar. You may also use **/*.* for all files (excluding hidden and without extension) or any other pattern.

If you’ve the error that your argument is too long, consider narrowing down your search, or use find syntax instead such as:

find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'

Alternatively, use ripgrep.

ripgrep

If you’re working on larger projects or big files, you should use ripgrep instead, like:

rg "class foo" .

Checkout the docs, installation steps or source code on the GitHub project page.

It’s much quicker than any other tool like GNU/BSD grep, ucg, ag, sift, ack, pt or similar, since it is built on top of Rust’s regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.

It supports ignore patterns specified in .gitignore files, so a single file path can be matched against multiple glob patterns simultaneously.


You can use common parameters such as:

  • -i — Insensitive searching.
  • -I — Ignore the binary files.
  • -w — Search for the whole words (in the opposite of partial word matching).
  • -n — Show the line of your match.
  • -C/--context (e.g. -C5) — Increases context, so you see the surrounding code.
  • --color=auto — Mark up the matching text.
  • -H — Displays filename where the text is found.
  • -c — Displays count of matching lines. Can be combined with -H.

Do you want to find all files that contain a particular word or string of text on your entire Linux system or a given directory. This article will guide you on how to do that, you will learn how to recursively dig through directories to find and list all files that contain a given string of text.

A simple way to work this out is by using grep pattern searching tool, is a powerful, efficient, reliable and most popular command-line utility for finding patterns and words from files or directories on Unix-like systems.

Read Also: 11 Advanced Linux ‘Grep’ Commands on Character Classes and Bracket Expressions

The command below will list all files containing a line with the text “check_root”, by recursively and aggressively searching the ~/bin directory.

$ grep -Rw ~/bin/ -e 'check_root'

Find a Word in Directory

Find a Word in Directory

Where the -R option tells grep to read all files under each directory, recursively, following symbolic links only if they are on the command line and option -w instructs it to select only those lines containing matches that form whole words, and -e is used to specify the string (pattern) to be searched.

You should use the sudo command when searching certain directories or files that require root permissions (unless you are managing your system with the root account).

 
$ sudo grep -Rw / -e 'check_root'	

To ignore case distinctions employ the -i option as shown:

$ grep -Riw ~/bin/ -e 'check_root'

If you want to know the exact line where the string of text exist, include the -n option.

$ grep -Rinw ~/bin/ -e 'check_root'

Find String with Line Number

Find String with Line Number

Assuming there are several types of files in a directory you wish to search in, you can also specify the type of files to be searched for instance, by their extension using the --include option.

This example instructs grep to only look through all .sh files.

$ grep -Rnw --include=*.sh ~/bin/ -e 'check_root'

In addition, it is possible to search for more than one pattern, using the following command.

$ grep -Rinw ~/bin/ -e 'check_root' -e 'netstat'

Find Multiple Words in Files

Find Multiple Words in Files

That’s It! If you know any other command-line trick to find string or word in files, do share with us or ask any questions regarding this topic, use the comment form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.


Posted:
September 23, 2022

Searching for patterns of text in files or text streams is one of the most common tasks you’ll perform in your sysadmin career. This is a valuable skill that allows you to check a variety of system configurations, analyze data, troubleshoot logs, and perform many other activities.

The most common way to find text in a Linux system is using the command-line utility grep. This utility was originally developed for the Unix operating system in the early 1970s. Grep evolved over the years, and the most common version available today for Linux, GNU grep, has additional features such as colored output. However, its main functionality is still the same.

Using grep, you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin using the shell pipe operator.

This article covers how to use the grep command to find text.

Find text in a file

The most basic way to use grep is searching for text in a single file. To do this, type grep followed by the text pattern to search for and the file name to search in. For example, to find which port the Secure Shell (SSH) daemon uses, search for Port in file /etc/ssh/sshd_config:

$ grep Port /etc/ssh/sshd_config
Port 22
#GatewayPorts no

Notice that grep finds all lines that match the text pattern regardless of where the pattern is located.

[ Download the Linux grep command cheat sheet. ]

Extend grep with regular expressions

In the previous example, when you searched for Port in the SSH configuration file, grep returned two lines. The line you were looking for, Port 22, and an additional line containing the search pattern. In some cases, that’s exactly what you want. In other cases, grep could find too many entries that you’re not interested in, requiring you to sort through them to find the desired information.

To avoid that, you can use regular expressions to be more specific about what you’re looking for. For example, to find only lines that start with the word Port, you can use the regular expression operator ^, like this:

$ grep ^Port /etc/ssh/sshd_config
Port 22

This time grep returned only the line that started with Port since, in the second line, the expression Port is in the middle.

You can also use extended regular expressions with the command-line parameter -E. For example, to search for a pattern that contains the word Port followed by numbers, use this regular expression:

$ grep -E "Port [1-9]+" /etc/ssh/sshd_config
Port 22

You can also look for lines that end with a text pattern by using the $ operator. For example, to find all lines that end with none in sshd_config, use grep like this:

$ grep none$ /etc/ssh/sshd_config
#RekeyLimit default none
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#ChrootDirectory none
#VersionAddendum none
#Banner none

Regular expressions are a big part of grep, making it powerful and flexible. However, regular expressions are a huge topic. For additional information, look at Regular expression on Wikipedia or Regular expressions 101.

Find text in multiple files and directories

Similar to finding text patterns in a single file, you can use grep to find text in multiple files or directories. To find text in multiple files simultaneously, specify which files to search from after the first file name, or use a shell wildcard such as * for all files. For example, to search for a configuration in two files:

$ grep Port /etc/ssh/sshd_config /etc/ssh/ssh_config
/etc/ssh/sshd_config:Port 22
/etc/ssh/sshd_config:#GatewayPorts no
/etc/ssh/ssh_config:#   Port 22

When you use multiple files, grep shows the name of the file where it found a match before showing the matched line.

[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]

To run the search recursively in multiple subdirectories, use the command line flag -R:

$ grep -R ^Port /etc
/etc/ssh/sshd_config:Port 22

The grep command is fast and returns results quickly, but it may take a long time if you specify too many files or subdirectories to search.

Find text in another command’s output

Similar to other Unix utilities, grep also acts on stdin when you pipe the output of another command into it. This is a fast and useful way to filter a command’s output to match the text pattern you’re looking for.

For example, if you want to check whether the package openssh is installed in your Fedora or Red Hat Enterprise Linux (RHEL) operating system, you can pipe the output of command rpm -qa, which lists all installed packages, into grep to search for the pattern:

$ rpm -qa | grep ssh
libssh-config-0.9.6-4.fc36.noarch
libssh-0.9.6-4.fc36.x86_64
openssh-8.8p1-1.fc36.1.x86_64

You can filter long command outputs with grep, making finding useful information easier.

[ Get the guide to installing applications on Linux. ]

Additional useful options

The grep command provides many options to change how it searches for patterns or displays results. So far in this article, you’ve seen some of them. While I can’t list all options, here are some other useful examples:

  • Use option -i for a case-insensitive search.

  • Use option -v to invert the search and display lines that do not match the pattern.
  • Use option -w to search for entire words only instead of patterns in the middle of other words.
  • Use option --color for colored output, making it easier to spot the matched pattern.

For a complete list of grep options, consult the man pages.

What’s next?

The GNU grep utility is flexible and useful, helping you accomplish many tasks in your daily sysadmin activities. The more you use grep, the more comfortable you will become, and soon you’ll notice you’re relying on it all the time.

For more information about grep, look at some of these links:

  • How to use grep
  • Linux grep command cheat sheet
  • Grep wiki
  • Grep manual at GNU

You can also find more information about grep in your Linux system by using man grep or quick, valuable examples with the tldr tool.

Contents

1. Search text files with the grep tool
2. SearchMonkey
3. Recoll
4. DocFetcher
5. Regexxer

For Linux users, the need to search for words in one or multiple files arises quite often.

There are many different applications that can do a good job for you. Which application will be most suitable for you depends on your needs and preferences. The applications are mainly distinguished by the following characteristics:

  • Command line tools vs. tools with a graphical interface.
  • Whether they are able to search only in text files, or in all commonly used document types.
  • Whether they use indexing or not.

In this article we will introduce you to different types of search applications, so that you can choose the most suitable one for you.

Note: If you are a Mac user, you can take a look at our article How to Search Text in Your Documents on Mac.

1. Search text files with the grep tool

About the grep tool

The most popular command-line tool for searching text in Linux is grep. It was created more than 45 years ago! Its developer is the American computer scientist Ken Thompson.

Grep can search in all kinds of text files (.txt, .html, .xml, etc.), but cannot search in other types of documents such as OpenOffice files, PDF files and others.

In fact, the grep command is in the top 15 of the most used commands in Linux, according to Hostinger.

The utility is supported by the UNIX, Linux, and Mac operating systems.

There is also a Windows alternative to grep called findstr, which you can read more about in our article How to Search Files in Windows 10.

How to use the grep tool

The grep tool is built into the operating system, so you don’t need to install it.

The simplest syntax to find a string in a single file using the grep command is:
grep ‘searched-word’ filename

By default, the search with grep is case sensitive.

Let’s gradually add the most useful options to our search.

 
1. Make the search case-insensitive with the «-i» option:
grep -i ‘searched-word’ filename

 
2. Search recursively in all files in a given directory with the «-r» option:
grep -ir ‘searched-word’ ‘/directory’

 
3. Search whole words only with the «-w» option:
grep -irw ‘searched-word’ ‘/directory’

 
4. Print the line numbers in which the searched word was found with the «-n» option:
grep -irwn ‘searched-word’ ‘/directory’

 
5. Search for multiple words syntax:
grep -ir ‘word1|word2|word3’ ‘/directory’

Many other useful grep options can be found in the official Grep Manual.

Advantages and disadvantages of the grep tool

 
Like any software tool, along with the convenience it gives to you, grep has some drawbacks. Let’s summarize its pros and cons.
 

Pros:

  • Great flexibility through a wide range of options.
  • Works effectively with regular expressions.
  • It is built into the operating system.

Cons:

  • Cannot search in any other types of documents except text files.
  • Doesn’t have a graphical interface.
  • Difficult reading of long lines of text in the results.
  • Not so powerful for searching in source code, compared to ack.

2. SearchMonkey

SearchMonkey is a free and open source GUI application for Linux OS, which uses the capabilities of the grep tool. SearchMonkey is also available for Windows and Mac users.

In addition to searching in text files, SearchMonkey supports:

  • OpenOffice and LibreOffice files (.odt).
  • Microsoft Office files (.docx).
  • PDF files (.pdf).

The tool can search a directory recursively and returns the file names and the highlighted content that you search.

Go through the following steps to install and use the tool:

 
1. Install the application with the following command in the Terminal:
sudo apt install searchmonkey

 
2. Launch the application.

 
3. Choose the folder to be searched.

 

 
4. Check the «Containing» checkbox, enter your search words and press «Enter.»

 

 
5. Click on a file from the list on the left pane to see the fragments of text found.

 

If you want to search a specific directory, click on the “Advanced” tab and choose the desired folder.

In the «Advanced» pane, you can use additional criteria to filter the results such as by the file size, date of last modification, and others.

With SearchMonkey, you can also use regular expressions in the same way as in the grep tool.
 

Pros:

  • Completely free and open source.
  • Lightweight and easy to use.
  • Doesn’t use indexing.
  • Many options for refinement of the search.
  • Works with regular expressions.

Cons:

  • Slow performance, not suitable for big directories.
  • Does not support RTF, Excel (.xlsx, .xls), PowerPoint (.pptx, .ppt), old Word (.doc) and other popular document types.
  • Cannot search for a combination of words that occur in one sentence.
  • Doesn’t show all lines from the results in one place, you have to check each file one by one.

3. Recoll

Recoll is a desktop search tool, available for Linux, Windows, and MacOS. The Linux version is free. It can search not only in text files, but also in most common document formats including:

  • OpenOffice and LibreOffice files (.odt).
  • Microsoft Office files (.docx, .xlsx, .xls, .ppt).
  • PDF files (.pdf).
  • AbiWord files (.abw).

By default, Recoll indexes the files on your «home» directory, which can take some time.

Here are the steps to install and use the program:

 
1. Install Recoll with the command:
sudo apt-get install recoll

 
2. Launch the application.

 
3. On the «First indexing setup» window click on «Start indexing now.»

 

 
4. On the «Missing helper programs» message click «OK.»

 

 
5. Type your search terms and press «Enter.»

6. From the results list, click on «snippets» to see all text fragments containing the searched words.

 

7. On the «Snippets» window, you see the pages and the lines in which your searched words are found.

 

If you need to search in a folder outside your «home» directory, go to «File» -> «Special indexing» and click on the «Browse» button to select the desired directory.

Pros

  • Free for Linux users.
  • Supports most common document types.
  • Easy-to-use graphical interface.
  • Supports advanced search options.
  • Allows document preview without opening the file.

Cons

  • Although the application uses indexing, the searches are relatively slow.
  • Does not support the Word 97-2003 «.doc» format or the PowerPoint «.pptx» file format.
  • Cannot search for a combination of words.
  • You have to click on each result’s «snippets» to see the fragments found.

4. DocFetcher

DocFetcher is a free and open source desktop search tool, available for Linux, Windows, and MacOS. In addition to text file formats, it supports almost all of the document formats which you may use:

  • Microsoft Office 97-2003 files (.doc, .xls, .ppt).
  • Microsoft Office 2007-365 files (.docx, .xlsx, .pptx, .docm, .xlsm, .pptm).
  • Microsoft Outlook email files (.pst).
  • OpenOffice files (.odt, .ods, .odg, .odp, .ott, .ots, .otg, .otp).
  • PDF files (.pdf).
  • Rich Text Format (.rtf).
  • AbiWord files (.abw, .zabw).
  • And others.

Here are brief instructions for using DocFetcher:

 
1. Install DocFetcher with the command:
sudo snap install docfetcher

2. Launch DocFetcher
 

3. Right-click on the «Search Scope» pane at the bottom left and choose «Create index from»->»Folder.»

 

4. Choose a folder, give a name to the index and click the «Run» button.

 

5. Wait until the index is created. When it is ready, you will see a check mark in the «Search Scope» pane on the left.

 

 
6. Type your search words and press «Enter.»

 
7. Click on a file name to see the fragments of the text containing your searched words. You can navigate in the results with the «up» and «down» arrows.

 

Pros

  • Free and open source application.
  • It has a portable version.
  • It can search in archive formats (.zip, .7z, .rar, .tar).
  • Offers additional search options – wildcards, similar words, proximity search and others.

Cons

  • Creating an index can take a long time for large folders.
  • Cannot perform case-insensitive search.
  • The navigation in the text results is not user-friendly.
  • Cannot search for a combination of words in a sentence.

5. Regexxer

The regexxer application provides rich possibilities for using perl-style regular expressions. It is a free and open source application with a user-friendly graphical interface, created in 2002.

Regexxer is available only for Linux, and you can most easily install it in Debian and Ubuntu.

Here’s how you can use Regexxer:
 

1. Install the application with the command:
sudo apt-get install regexxer

 
2. Launch the application, select a search folder and make sure the «recursive» checkbox is selected.

3. Click the «Find files» button.

 

4. Type your search terms in the «Search» box and click the «Find» button.

 

 
5. Use the arrows at the bottom to navigate in the results.

 

 
6. In the left pane you can see the number of results found in the corresponding files, and in the right pane, the text containing the searched words.

Pros:

  • Completely free and open source.
  • Easy to install and use.
  • Simple and intuitive graphical interface.
  • Supports all types of perl-style regular expressions.
  • Highlights the searched words in the results.
  • Doesn’t use indexing.

Cons:

  • Regexxer is slower than other similar applications.
  • Sometimes becomes unresponsive or closes unexpectedly.
  • Cannot search in any other types of documents except text files.
  • Cannot search for a combination of words that occur in one sentence.

Conclusion

I hope you enjoyed this guide to finding text files in Linux.

I am fully aware that many more things can be added for the presented applications, and also that there are many other useful tools in this niche.

Now I would like to hear from you: which of the presented applications is most useful for you? Do you have any problems using these tools? Do you want to add anything else that is important on this topic?

Let me know your thoughts by posting a comment in the comments section below.

About the author

Dimitar is a software developer and entrepreneur with over 20 years of experience in software development and online marketing.

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 'bbirdb'                                               
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 =~ /bwordb/){
                printf "%sn", $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 "%sn" "$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 "$@"

Like this post? Please share to your friends:
  • Linux excel to html
  • Link excel files in word
  • Linux and microsoft word
  • Link excel and sql server
  • Links to files in excel