TechToFreedom
Published in

TechToFreedom

You have 2 free member-only stories left this month.

5 Ways To Find a String Inside a File on Linux

Without any GUI tools of course

Fantasy art

On your browser, to find a specific string on a webpage, all you need to know is the “CTRL+F” trick.

But as a backend or DevOps engineer, you have to handle something only on a Linux terminal instead of GUI tools. Life is not fair, is it?

But don’t worry, if you really master Linux skills, you will agree with me that using commands is more efficient than any GUI tool. And another bonus is it can make you become cool (Imagine a hacker in a fantasy movie who uses terminals all the time).

This article will introduce 5 convenient and efficient ways to find a string in a file on your Linux terminal.

Basically, there are two possible situations when you need to find a string inside a text file:

  1. You haven’t opened the file yet.
  2. You opened the file already.

For the first situation, we can use the famous three text processing weapons on Linux: grep, sed, and awk.

For the second situation, we can leverage the power of the less or vi.

Before diving into details, let’s use the draft version of my poetry “The Zen of Writing on Medium”, which is a file named zen_of_medium.txt, as the example file for the 5 ways.

The Zen of Writing on Medium

1. Using grep Command

The grep command is an intuitive and easy-to-use tool for string searching. Its basic structure is just grep string file_name.

For example, if we would like to find where is the word “better” inside the zen_of_medium.txt file. The command and results are as follows:

Results of grep command

The above grep command prints all the lines that contain the word “better”.

However, if it’s really a long-text file, finding the exact location of the results through the above approach is still hard. Fortunately, there is a -n option in grep which can print the line numbers of every line of result:

Results of grep -n command

2. Using sed Command

The sed command is another powerful tool for text processing. For our task, we can use it as follows:

Results of the sed command

Of course, the sed command can do further, such as deleting or replacing the string after finding it. Those are beyond the scope of this article.

3. Using awk Scripting

There is one command in Linux which is too powerful to be called a command — awk. It’s literally a scripting language for text handling in Linux.

— Yang (“8 Levels of Using awk in Linux”)

Based on the above saying, you can imagine that the awk language can be used to find strings as well.

Now, we’ll follow the syntax of the awk:

awk ‘pattern {action}’ filename

In our case, the pattern is just the word “better”, and the action is to print out the line numbers and content of every matched line.

As shown above, the awk command gives us the exactly correct results as the grep and sed commands.

4. Checking the File with less

If a file’s content has been displayed on the terminal already using the less command, we can just use the tricks of less.

To search characters with less, type / followed by the string we want to search for, and then press “Enter”.

If there are many matched words, we can press n to find the next occurrence or N to find the previous occurrence.

5. Checking and Editing the File with vi

Sometimes, we need to edit the characters after finding them in the file. In this case, the vi editor is a good choice.

And what makes our life easier is the syntax of vi for finding a string is the same with less.

  • Type / followed by the string, and then press “Enter”.
  • Press n to find the next occurrence or N to find the previous occurrence.

Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more great articles. 🙂

More articles about Linux and DevOps:

Linux Master

30 stories
5 Uses of Cut Commands in Linux
Cover Image: Rcok’n’roll

Enjoy the read? Reward the writer.Beta

Your tip will go to Yang Zhou through a third-party platform of their choice, letting them know you appreciate their story.

Get an email whenever Yang Zhou publishes.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Medium sent you an email at to complete your subscription.

Technology gives us more and more freedom. Start learning today.

Share your ideas with millions of readers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store