How to Use Grep Command in Linux to Find Files Inside

You are looking for that one file, the one that contains all the important information for your next meeting. Are you manually searching all your files? It will take time. Instead, we use a bit of Linux command-line magic. Grep is a pattern matching command that we can use to search files and directories for specific text. Grep is commonly used with the output of a command, piped to be the input to the grep command. For example, we can search a file for a specific text string using the less command, and redirect the output to grep.

In this guide, we will use the grep commonly added command and arguments to search for specific data strings in files. We’ll start by setting up a small directory of test files, because searching an entire filesystem can take a long time and create a lot of results.

All commands in this tutorial will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through 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

(Image credit: Tom’s Hardware)

Like grep can be used in different ways, we need to create a directory and content that allows us to explore its uses. In this section, we will create this test environment.

1. Set up a test directory and change directory to find yourself there.

mkdir test
cd test

2.Create 4 files, test1, test2, test3 and test4.

touch test1 test2 test3 test4
ls

3. To modify test1 using nano to contain subsequent names on separate lines. Note that in test1 none of the names contain capital letters. After editing in nano hurry control x to exit, press there to confirm the save then press Walk in.

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. To modify test2 using nano. In test2 we will add a single longer test line 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. To modify test3 in nano. Similar to test1 we will add a list of names on separate lines but this list will include the name Steve.

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 use a capital letter at the beginning of Steve.

nano test4

ten. Add the following text to the file. Then press CTRL + X, then Y and Enter to save and exit.

Steve ?

Simple searches with grep

(Image credit: Tom’s Hardware)

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 the command syntax is simply grep followed by all the arguments, then the string we want to search for, and finally the location to search in.

1. Research test1 for the string Steve using grep. Search criteria are case sensitive, so make sure you search correctly. Notice that the word Steve is simply returned in red to indicate that it was 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. Note that with multiple search boxes, the results returned are tagged with each filename from which the result is found. Also notice that grep returns the complete line of text containing the search string.

grep steve test1 test2

3. Search all files in the directory. Adding an asterisk forces grep to search for all files in the current directory. Note that the returned results include the result of test3 where the search string Steve is contained in Steven. Also note that these results do not contain the result of test4 as in its basic form the grep the command is case sensitive.

grep steve *

4. Add Argument -I To do grep case insensitive. This will return the results of all four test files in the directory.

grep -i steve *

Piping output to grep

The strongest use case for grep is when combined with another command. Using channels, we send the output of a command to grep and use it to search for patterns/keywords.

1. Open a new terminal window.

2. Utilize lsusb to list all USB devices connected to your device. This will also list internal USB devices, such as laptop webcams. The output will be different depending on your machine, but you should be faced with a wall of text.

lsusb

(Image credit: Tom’s Hardware)

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

(Image credit: Tom’s Hardware)

Using dmesg and grep to inspect the Kernel Ring Buffer

Let’s try something a little more complex. This time we will use dmesg and grep to inspect the Kernel Ring Buffer (essentially the kernel log file). We will search for a keyword in dmesg“secureboot” and confirm that it is enabled.

1. Open a terminal and run the dmesg command as sudo. This will print a console output wall to the terminal, something we can search for using grep.

sudo dmesg

(Image credit: Tom’s Hardware)

2. Use the grep command to search for “secureboot” in the dmesg production. Use the -I argument to turn off case sensitivity so that we type every occurrence of secureboot. The output will show the lines where secureboot appears in dmesg.

sudo dmesg | less | grep -i secureboot

(Image credit: Tom’s Hardware)

Other uses of grep

(Image credit: Tom’s Hardware)

Like many Linux commands, there are many useful additions and variations for the grep ordered. Let’s look at some interesting examples.

1. Perform a reverse search using the -v argument. This will return a list of all lines from the test environment files that do not contain the search string Steve. This argument is useful for ignoring occurrences of strings in logs or files when debugging a problem. Note that again the results are case sensitive and therefore include the line containing the capital letter Steve? from test4.

grep -v steve *

2. Combine them -v and -I arguments to exclude all matching strings, regardless of case.

grep -vi steve *

3. Finds a string containing text or non-alphanumeric spaces. If you include a search string with a space or other non-alphanumeric text, it may break the grep command syntax, to create a search string containing them, you must use quotes to contain the string. In this step, we are looking for “Steve?” which is contained in the test4 case.

grep “Steve ?” *

Searching subdirectories with grep

(Image credit: Tom’s Hardware)

Like many Linux commands, there are many useful additions and variations for the grep ordered. Let’s look at some interesting examples.

1. Perform a reverse search using the -v argument. This will return a list of all lines from the test environment files that do not contain the search string Steve. This argument is useful for ignoring occurrences of strings in logs or files when debugging a problem. Note that again the results are case sensitive and therefore include the line containing the capital letter Steve? from test4.

grep -v steve *

2. Combine them -v and -I arguments to exclude all matching strings, regardless of case.

grep -vi steve *

3. Finds a string containing text or non-alphanumeric spaces. If you include a search string with a space or other non-alphanumeric text, it may break the grep command syntax, to create a search string containing them, you must use quotes to contain the string. In this step, we are looking for “Steve?” which is contained in the test4 case.

grep “Steve ?” *

Searching subdirectories with grep

(Image credit: Tom’s Hardware)

Often we will want to search for a string in the files contained in the subdirectories. We can do this simply by adding the -r recursive argument to grep ordered.

1. Create a subdirectory containing a test file in the test phone book.

mkdir sub_directory
cd sub_directory
touch test5

2. Open test5 using nano text editor and add the text “steve in a subdirectory” to the file. Then press CTRL + X, then Y and Enter to save and exit.

nano test5.

3. back to test phone book and search by adding -r option. Note that the result for test5 includes the location of the file listed in the output.

cd ..
grep -r steve *

(Image credit: Tom’s Hardware)

With grep on Linux, you have a good set of approaches to finding the contents of files on your system. As with many Linux commands, it may be worth checking out the help menu to see all the various arguments you can add to grep. Course grep –h in a terminal emulator to check all options.

AFTER: How to Check Disk Usage in Linux

AFTER: How to Kill a Process in Linux

AFTER: How to Find Files in Linux

Brandon D. James