Table of Contents
Introduction
How many times have you wondered how to find text in a file using the linux command line?
In this article you will find all the answers you are looking for!
We will see how to use the grep
command to search for a text within a file or directory.
Don’t worry, the post will be full of examples and within everyone’s reach!
Preliminary operations
To find text in a file using the linux command line we will use the grep
command.
To put yourself in a position to try this command in your local environment, some simple preliminary operations are necessary.
I ask you to create a file in a directory of your liking.
If you don’t know how to move from one directory to another you can use the cd command.
To do this, use the editor of your choice. I did it from the command line by simply typing the following command:
nano file.txt
This created a file called file.txt
and then I wrote this sample text into it.
This is a simple txt file .
There are several lines and
it will help me to learn how to use
GREP command.
Byee
Once this is done we can proceed with using the grep
command to find a text in a file using linux.
Grep linux command
To find text inside a file using the linux command line we use the grep
command.
Its structure is as follows:
grep <options> <pattern> <files/directory>
Here an example:
grep -i "hello world" file.txt
This command allows you to search for the text hello world
inside the file file.txt
ignoring case (option -i)
The grep
command really has many options and in this article we will see the main ones and the ones that I think are the most useful.
I recommend that you take a look at grep’s help
command so that you see all the possible options.
grep --help
How to find text in file using linux command line
The grep command helps us find text in a single file, for example, suppose we want to find the text “help me” in the file file.txt
.
grep "help me" file.txt
If you try to run this command you will see that the command line will return the line where the text you searched for is present.
Viceversa, if we try to search for the text “hello world” you will see that there will be no output. This means that the text is missing from the file.
grep "hello world" file.txt
As I already told you in the previous chapters, the grep command really has a lot of options.
I point out the following:
Option | Description |
---|---|
-i or –ignore-case | Ignore case during the search |
-w, –word-regexp | Match whole words only |
-n, –line-number | Print the line number with output lines |
-B, –before-context=NUM | Print NUM lines of previous context |
-A, –after-context=NUM | Print NUM lines of following context |
In the next chapters we will see a little insight into the most useful parameters.
Grep -i or –ignore-case option
By default the grep
command is case sensitive.
For this reason, if you are looking for a word it must be exactly the same, even in upper and lower case.
However, there is an option to ignore case which is -i or –ignore-case.
To use it you can use this command:
grep -i "grep" file.txt
Output:
GREP command.
Grep -n, –line-number option
In some cases it is useful to know where in the file we are looking for the match is present.
The Linux grep
command provides the -n or –line-number parameter which also prints the line where the match was found.
Look at this example to better understand how to use it:
grep -n "grep" file.txt
Output:
4:GREP command.
Grep -A and -B option
Sometimes finding the match isn’t enough.
Think for example of a case in which the match is conditioned by the position in which it is found within the file.
It would be useful to see a few lines before and maybe even a few lines after the match, right?!
Well, the Linux grep
command also gives us these two options.
The -A and -B parameters allow us to decide how many lines we want to see after and before the match line.
grep -B 1 -A 1 "GREP" file.txt
Obviously you can also decide to use all the parameters that I have shown you together.
Take a look at this example:
grep -in -B 1 -A 1 "grep" file.txt
Output:
3-it will help me to learn how to use
4:GREP command.
5-
How to find text in a directory using linux command line
With the grep command it is also possible to search for text in more than one file, for example in a directory.
Let’s see how the command changes compared to the one seen in the previous section:
grep "help me" <my_directory>
As you can see the pattern is the same, but instead of putting a single file you can put a directory.
The working mechanism and also all the options do not change.
grep -in -B 1 -A 1 "grep" <my_directory>
Conclusion
I hope this post was clear and that you now know how to search for text within files or directories.
If you have any other doubts or something is not clear to you, feel free to write in the comments below or contact me directly!
If, on the other hand, everything is clear, continue with the latest articles!