Register Login

How to Search for Files Containing Specific Text String in Linux

Updated Oct 20, 2023

You all know Linux is an open-source operating system. It directly handles the system's hardware and other resources. Linux provides different commands and is faster and more efficient than a Graphical User Interface.

Using these commands, Linux users can perform operations, such as searching and finding all files containing a specific text. In case when users do not remember the name of a file but only remember their contents, they can use some methods that help find the file containing the specific text in Linux.

So let us look at all the methods to search and find all such files in the Linux operating system.

Methods for searching and finding all files that contain a specific text using Linux commands:

Method 1: Using the grep command

The term grep is short for a global search for regular expression and print out. Using the grep filter, users can search a file for a specific pattern of characters or simply text. This Linux command will display all lines in a file matching the given pattern or text. We term this pattern searched in the file as the regular expression.

Syntax:

grep [options] pattern [files]

Following are some of the options users can use with the grep command as different variations in different scenarios:

  • -c : This option allows printing only a count of the lines that match the given pattern.
  • -h : This option allows displaying all the matched lines. It does not display the filenames.
  • -i : Allows grep command to ignore case sensitivity
  • -l : This option lets the command display the list of filenames only.
  • -n : Displaying the matched lines and the line numbers.
  • -v : This options is for printing out all the lines that do not match the pattern.
  • -e exp : Specifying expression with this option. Users can use it multiple times.
  • -f file : This option allows taking patterns from a file, one per line.
  • -E : Allows treating pattern as an extended regular expression (ERE)
  • -w : Matching the whole word
  • -o : This option allows printing only the matched sections of the matching line. It also prints each such section on a separate output line.
  • -A n : Printing searched line and n lines after the output.
  • -B n : Printing searched line and n line before the output.
  • -C n : Printing searched line and n lines after before the output.

Let us consider we create two files using the touch command, as follows:

$ touch demo1.txt

The content of the file "demo1" is:

“Here, we insert out text.”

And we create a second file, "demo2," and add the contents:

$ touch demo2.txt

The content of the file "demo2" is:

“One must learn the functions of every Linux command.”

Now let us see how to use these commands and the grep command to search for particular text in the files.

$ grep -l Linux demo1.txt demo2.txt

Output:

Explanation:

In the above terminal, it is clear that the command successfully finds the name of that file that contains the text "Linux." Here, the file is demo2, which contains the given string.

But the point which users must note, the string is "Linux," not "Linux." Thus, here it is a matter of concern about the case sensitiveness of the string. In the above example, we performed the search operation keeping the case in mind.

To ignore the case sensitivity, users can use this command and search the file with the particular text:

$ grep -li Linux demo1.txt demo2.txt

Here, the -i option allows grep to ignore the case.

Output:

Users can also use the -r option, another variation of the grep command. This option allows the grep command to recursively search for a particular string in the current directory and the subdirectories.

Output:

Explanation:

We used the -r option to check and find the file in directories and subdirectories in the above Linux code. Hence, it returned the file demo2 containing the given string after it searched both files.

Efficient searching technique:

Users can use other extra techniques like --exclude, --include, --exclude-dir flags to make the search mechanism more efficient and fast.

grep --include=\*.{txt,php} -rnw '/path/to/somewhere/' -e "string_searched"

Here, the include option allows users to add file extensions, and the grep command will search only those files with these extensions.

grep --exclude=\*.c -rnw '/path/to/somewhere/' -e "string_searched"

Here, the exclude option allows users to add file extensions, and the grep command will exclude searching all the files having file extensions .c

grep --exclude-dir={1directory,2directory,*.dst} -rnw '/path/to/search/' –e "string_searched"

This grep --exclude-dir option will allow searching directories to exclude one or more directories. Here, the search will take place excluding the 1directory and 2directory directories.

Further, we can use the following grep commands to search for a file in Linux:

Using grep -ilR

We will use another approach of the grep command with the ril or ilr option to find the file with a particular string:

grep -Ril "text-to-find-here" /

Meaning of the syntax:

  • i: indicates ignoring the case. It is optional in often situations.
  • R: short for recursive.
  • l: short for "show the file name, not the result itself."
  • /: short for starting at the root of the users' machine.

Output:

Using recursive and case-insensitive grep with line numbers
Again, users can use the grep -inr command to specify the case of the searched string.

grep -inr "Text" folder/users/want/to/search/

Meaning of the command:

  • -i: short for case insensitive
  • -n: short for the line number
  • -r: indicates recursive reads of all the files in a subdirectory

Using grep -r

Use the command to search a file with specifies path or directory:

grep -r "string users want to search" /path/to/directory

Output:

Using grep tool

Users can use the grep tool to find recursively the current file. The syntax is:

grep -r "another line" .

Users can also use globbing option for searching inside a specific file.

grep "another line" **/*.php

** is the globbing option, which scans all the files recursively with particular extension or pattern. This method helps users search for a specific extension file like .php, .txt, etc.

Method 2: Using find and grep commands

Using the find command, one can walk their system's file hierarchy. It is a Linux command line utility. Users can use it to search files and directories and execute subsequent operations on them. It allows them to search and find by creation date, file, folder, name, modification date, owner, and permissions.

Syntax:

$ find [adding the starting of the search] [expression defining what users want to find] [-options] [what to find]

Following are some of the options users can use with the find command as different variations in different scenarios:

  • -exec CMD: This option helps search the file that satisfies the given criteria and returns zero (0) for successful command execution as its exit status
  • -ok CMD : It performs the same as -exec. But it prompts the user first.
  • -inum N : This option allows searching for files with inode number 'N.'
  • -links N : Search for files with 'N' links.
  • -name demo :  This option allows searching for files that users specify by 'demo.'
  • -newer file : Allows searching files, which users modified or created after 'file.'
  • -perm octal : Searching for a file if permission is 'octal.'
  • -print : Displaying the path name of the files discovered by operating the other criteria.
  • -empty : Searching for void files and directories.
  • -size +N/-N : This option allows searching for files of 'N' blocks. Users can use this 'N' followed by 'c' to measure the size in characters. Also, they can use '+N,' which implies size > 'N' blocks, and '-N' implies size < 'N' blocks.
  • -user name : Searching those files owned by username or ID 'name.'
  • \(expr \) : This option returns True if 'expr' is true. Users can use it to group criteria connected with OR or AND.
  • ! expr : This option returns True if 'expr' is false.

Let us see the command and understand how it works:

$ find -type f -exec grep -lr "Linux" {} \;

Output:

Explanation:

We combined the find and grep commands with the -type f option and -exec option respectively, to search for files and check all the matched files. Here, the search operation finds demo2.txt in the first attempt and demo1.txt in the second.

These files contain the matching string. Thus, the command displays their names on the screen one after another.

Method 3: Using the ripgrep command

This command helps Linux users to work as an alternative to the grep command. This ripgrep command gets often represented as "rg."

$ rg "insert"

Output:

This command returns that file "demo1" containing the given quoted string, i.e., "insert."

Use this command of ripgrep:

rg 'insert whatever string you want to find' / -l

Output:

Here, the command returned the file name demo2.txt to ensure this file only contains the given string.

Method 4: Using the ack command

It is the last command, which users can use to search and find the files containing provided string.

$ ack "insert"

Output:

Using ack tool

Users can also use the ack tool for searching:

ack -i insert_searched_string folder_path/*

Here, i specify the case-sensitivity of the searched string.

Output:

Real-life examples of searching files in Linux:

  • Command-based searching makes users more efficient to work with different files and folders.
  • Users can search files with names.
  • Users can search files in the home directories.
  • They can find directories using their names.
  • Users can also ease the searching technique by specifying the file extension using different commands in Linux.
  • They can find all the executable files using Linux commands.
  • Using grep and find command, users can search for the exact matched words and find texts in files.
  • Users can find email addresses, phone numbers, URLs, and other different information using the grep command.
  • Users can count the number of patterns existing in a particular file using some Linux commands like grep, ps, etc.  

Conclusion

We hope this article has given a crisp idea of the different methods gone through various Linux commands to search and find files for text strings. These commands are grep, a combination of find and grep commands, ripgrep, and ack commands.

All these methods are fast and easy to use, and according to the user's requirements, they can choose the desired options with the commands accordingly to get the desired output file.


×