Home Beginner's Guide How to find string in a file on Linux

How to find string in a file on Linux

by Nitish.S
find string in a file

Sometimes you might need to search a particular word or a string inside a file. To do this, almost every text editor has some GUI implementation to help you out. But for Linux users, it is much more productive and convenient to do these searches from the command-line.

In fact, Linux has a powerful and convenient command-line utility – the grep command for this purpose. Using this, you can search for a particular string not just in one file but multiple files for a more comprehensive search.

However, before you can use the grep command, you’ll first need to know how it works and its syntaxes. And so, to help you out, we have put together a detailed guide on using the grep command to help you find strings in a file on Linux. Not just that, but we will also show you how to search for strings in files through the command-line text editor and the GUI text editor.

Note: For this tutorial, we have created two demo text files – demofile.txt and demofile02.txt. We will be using the grep command to find strings in these files. Here is a look at the content of both of these files so you know how the commands are working.

Content of demofile.txt:

This is a demo file that I have created for demonstration purposes.
This contains a bunch of words to create sentences that finally end with a period.
This is for the grep example.

Content of demofile02.txt:

This is another demo file that I'll use for this demonstration.
This one is similar to the last one as it contains three lines with some words.
This second file will help to provide some diversified examples.

Finding strings in a file via the command line (GREP method)

GREP is short for Global Regular Expression Print. It’s “Global” because it can search your entire Linux system. “Regular Expression” is what we call the text search pattern that we will use with the command. And “Print” signifies that it will print the search results as soon as it finds a match based on the provided regular expression.

Now that you have an idea of what the grep command is, let’s see how we can use it. Here is the syntax for using the grep command to find a string in a file:

$ grep string filename

You will need to replace the expression “string” with the “regular expression” you want to use and the “filename” with the name of the file in which you want to search for the string.

#1. Using GREP to find a string in one file

So, let’s say if you want to search for the word “demonstration” in the file “demofile.txt,” you will need to use this command:

$ grep demonstration demofile.txt

Which gives the output:

1. Using GREP to Find a String in One File

Using GREP to Find a String in One File

As you can see, the command fetches the entire line containing the provided string. If the specified string occurs in multiple lines, it will print all of them. Let’s see if it works by searching for the string “This” in the “demofile.txt” file.

$ grep This demofile.txt

And the output is:

2. grep is case-sensitive

grep is case-sensitive

Note how you used the string “this” first, and it didn’t print anything. It’s only when you enter “This” that you get a match. As such, you should remember that the grep command is case-sensitive.

To ignore the case, you’ll need to enter this command instead:

$ grep -i this demofile.txt
3. how to ignore case in grep command

How to ignore case in grep command

As you can see now, even though the provided string is “this,” the command matches for “This” and prints that answer.

#2. Using GREP to find the same string in multiple files

To search through multiple files for the provided string, all you need to do is append the command with all the filenames.

Here is an example. Here we are searching for the string “words” in the files “demofile.txt” and “demofile02.txt”:

$ grep words demofile.txt demofile02.txt

And the output is:

4. Using GREP to Find the Same String in Multiple Files

Using GREP to Find the Same String in Multiple Files

As you can see, the grep command will not only show you the matched result but also conveniently label which file it’s from.

#3. Using GREP to find multiple strings in multiple files

You can also use grep to find multiple strings in single or multiple files. This is the syntax that you will have to use:

$ grep -E 'pattern1|pattern2|pattern3|...' filename1 filename2 filename3 ...

The -E option that we entered treats the following pattern as an extended regular expression, which allows us to search for multiple strings.

Now let’s use what we learned to search for the strings “file” and “words” in the two files: “demofile.txt” and “demofile02.txt”:

$ grep -E 'file|word' demofile.txt demofile02.txt

And the output is:

5. Using GREP to Find Multiple Strings in Multiple Files

Using GREP to Find Multiple Strings in Multiple Files

#4. Tips and tricks for using the GREP command to find strings

Despite its overall simplicity, the grep command is extremely powerful. Here are a few ways you can use the grep command for advanced string searches.

For example, did you know you can use the grep command with wildcards? Let’s consider this command:

$ grep word demofile*

Which gives the output:

6. grep and wildcards

grep and wildcards

As you can see, the command has searched for and is now showing the results for both files “demofile.txt” and “demofile02.txt”. This is because we used the asterisk (*) wildcard used to represent one or many occurrences of any character.

For more information regarding the different types of wildcards and how to use them, you can check out this read: Wildcards in Linux explained with 10 examples.

Moving on, the grep command also supports tons of command options. We have already seen two of them by now:

  • -i: used to ignore case.
  • -E: used to treat the following string as an extended regular expression.

There are plenty of other grep command options like:

  • -r: do a recursive search.
  • -c: count the total number of times the string appeared.
  • -n: print the line number where the string occurred.
  • -o: only print the matching string and don’t print the entire line.

Everything we discussed up till now should be plenty to help you search for strings in files. However, if you want to master the GREP command, we recommend you check out these two articles:

Find strings in a file via the command line (Using command-line text editors)

The grep command is a powerful tool that can help you perform advanced searches to match strings with several files and directories on your Linux PC. However, if you already have the file open in a command-line text editor, then there’s no need to exit the file editor and use the grep command.

In fact, most command-line text editors already come with a built-in search-functionality.

Now, it is beyond this tutorial’s scope to cover every single command-line text editor and how to find strings in a file opened in that text-editor. As such, we will be looking at the two most popular command-line text editors that you are bound to use:

How to find a text string in nano?

Let’s say you have the file opened in the nano text editor. In that case, all you need to do is press Ctrl+W, and you will find a new prompt to type in the string.

Once done, hit Enter, and the cursor will land on the first character of the first occurrence of the searched string. You can keep hitting Enter to jump to the other string instances if any.

7. How to find a text string in nano

How to find a text string in nano

How to find a text string in vim?

If you’re using the vim text editor, you can type in :/ followed by the string you wish to search and then hit Enter. Similar to before, it will land the cursor on the first occurrence of the searched string. Here too, you can keep hitting Enter to jump to the other instances of the string.

8. How to find a text string in vim

How to find a text string in vim

Find strings in a file via the command line (GUI method)

There’s even a GUI method for finding strings in a file. In essence, it is similar to the “find and replace” function you get to see on Microsoft Word; however, it will vary based on which GUI text editor you are using.

Now, as you know, there are hundreds of text editors, and it’s not practical to cover all of them here. However, all of them function similarly, so you won’t have any issue working with others once you know how one of them works.

For this tutorial, we will be using the default text editor that comes with Ubuntu, called – “Text Editor.”

9. How to find string using GUI text editors

How to find string using GUI text editors

As you can see from the image, we have opened the demofile.txt on the editor. Now, we click on the drop-down menu, which reveals options for “Find” and “Find and Replace,” which we can use to find any strings in the opened file.

Wrapping up

So that brings us to the end of this short tutorial on finding strings in a file on Linux. We hope you find this useful and that it helped you become more productive with how you search for specific strings on your Linux system.

If you have any questions, do leave us a comment, and we will be sure to help you out with your queries.

If you are starting with Linux, don’t forget to check out our Learn Linux series. It is filled with tons of awesome tutorials, how-tos, and guides, designed for beginners and more seasoned users to help you become a true Linux power user.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.