How to Use the Grep Command in Linux to Search Inside Files
You’re looking for that one file, the one that contains the all important information for your next meeting. Do you manually search all of your files? That will take time. Instead we use a little Linux command line magic. Grep is a pattern matching command that we can use to search inside files and directories for specific text. Grep is commonly used with the output of one command, piped to be the input of the grep command. For example we can search inside a file for a specific string of text using the less command, and pipe the output to grep.
In this how-to we will use the grep command and commonly added arguments to search for specific strings of data within files. We’ll begin by setting up a small directory of test files as searching an entire filesystem can take some time and create a lot of results.
All the commands in this how-to will work on most Linux machines. We’ve used a Ubuntu 20.04 install but you could run this how-to on a Raspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.
Set Up a Test Environment for grep

As grep can be used in lots of different ways we need to set up a directory and some content that allow us to explore its uses. In this section we will create this test environment.
1. Set up a test directory and change directory so that you are inside it.
mkdir test
cd test
2.Create 4 files, test1, test2, test3 and test4.
touch test1 test2 test3 test4
ls
3. Edit test1 using nano to contain the following names on separate lines. Note that in test1 none of the names contain capitals. After editing in nano press control x to exit, press y to confirm the save and then press enter.
nano test1
4. Add the following text to the file. Then press CTRL + X, then Y and Enter to save and exit.
ali
mohamed
claire
aled
steve
5. Edit test2 using nano. In test2 we will add a single longer line of test containing the name steve.
nano test2
6. Add the following text to the file. Then press CTRL + X, then Y and Enter to save and exit.
this is a long line of test that contains the name steve
7. Edit test3 in nano. Similar to test1 we will add a list of names on separate lines but this list will include the name steven.
nano test3
8. Add the following text to the file. Then press CTRL + X, then Y and Enter to save and exit.
alice
geoff
murbarak
mohamed
steven
9. Finally edit test4 to complete our test environment. Note that in this file we are using a capital letter at the start of Steve.
nano test4
10. Add the following text to the file. Then press CTRL + X, then Y and Enter to save and exit.
Steve ?
Simple Searches with grep

Searching a file for a specific string is extremely useful. For example when debugging an error in a log file. Let’s start using grep in its most basic form.
The grep command syntax is simply grep followed by any arguments, then the string we wish to search for and then finally the location in which to search.
1. Search test1 for the string steve using grep. The search criteria is case sensitive so ensure that you’re searching correctly. Notice that the word steve is simply returned in red to indicate it has been found in the file.
grep steve test1
2. Search for the same string in multiple files. We can simply add a list of files to the grep command for it to search. Notice that, with multiple search areas, the returned results are tagged with each filename from which the result is found. Also notice that grep returns the complete line of text that contains the searched string.
grep steve test1 test2
3. Search all files within the directory. Adding an asterisk forces grep to search all files within the current directory. Notice that the returned results include the result from test3 where the search string steve is contained within Steven. Note also that these results don’t contain the result from test4 as in its basic form the grep command is case sensitive.
grep steve *
4. Add the argument -i to make grep case insensitive. This will return results from all four of the test files in the directory.
grep -i steve *
Piping output to grep
The strongest use case for grep is when it is paired with another command. Using pipes we send the output of a command to grep and use it to search for patterns / keywords.
1. Open a new terminal window.
2. Use lsusb to list all of the USB devices attached to your machine. This will also list internal USB devices, such as laptop webcams. The output will differ depending on your machine, but you should be met with a wall of text.
lsusb

3. Use the lsusb command again, but this time use grep to search for Linux. By adding a pipe between lsusb and grep the output of the first command is used as the input of the second.
lsusb | grep Linux

Read more: https://www.tomshardware.com/how-to/grep-command-linux