Skip to content Skip to sidebar Skip to footer

Grep Command Running Over and Over Again How to Stop a Grep Command

 You're looking for that i file, the one that contains the all important data for your side by side 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 utilize to search within 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 instance nosotros tin 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 nosotros will apply the grep control and commonly added arguments to search for specific strings of data within files. We'll begin by setting up a pocket-size 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 volition piece of work on well-nigh Linux machines. We've used a Ubuntu 20.04 install just you could run this how-to on a Raspberry Pi. All of the how-to is performed via the Concluding. You can open a concluding window on virtually Linux machines by pressing ctrl, alt and t.

Set Up a Test Environment for grep

(Image credit: Tom'due south Hardware)

As grep tin can be used in lots of dissimilar means we need to gear up a directory and some content that permit u.s. to explore its uses. In this section we volition create this examination environment.

i. Set up a test directory and change directory and so that yous are inside it.

          mkdir test cd test        

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

          touch test1 test2 test3 test4 ls        

three. Edit test1 using nano to contain the post-obit 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 so press enter.

          nano test1        

4. Add the following text to the file. And so press CTRL + X, then Y and Enter to save and exit.

          ali mohamed claire aled steve        

v. Edit test2 using nano. In test2 nosotros will add together a single longer line of test containing the proper name steve.

          nano test2        

half-dozen. Add together the post-obit text to the file. Then press CTRL + X, and then Y and Enter to save and leave.

          this is a long line of examination that contains the name steve        

7. Edit test3 in nano. Similar to test1 we will add together a listing of names on carve up lines but this listing will include the name steven.

          nano test3        

8. Add the following text to the file. And so press CTRL + X, then Y and Enter to relieve and exit.

          alice geoff murbarak mohamed steven        

nine. Finally edit test4 to complete our exam environment. Annotation 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. And so press CTRL + X, and then Y and Enter to salvage and get out.

          Steve ?        

Elementary Searches with grep

(Image credit: Tom's Hardware)

Searching a file for a specific string is extremely useful. For instance when debugging an mistake in a log file. Let'southward beginning using grep in its most basic form.
The grep command syntax is simply grep followed by any arguments, then the string nosotros wish to search for and so finally the location in which to search.

i. Search test1 for the string steve using grep. The search criteria is case sensitive so ensure that you lot're searching correctly. Notice that the word steve is simply returned in ruby to indicate it has been institute in the file.

          grep steve test1        

2. Search for the same cord in multiple files. We can only add a list of files to the grep control for it to search. Discover that, with multiple search areas, the returned results are tagged with each filename from which the result is found. As well discover that grep returns the complete line of text that contains the searched string.

          grep steve test1 test2        

3. Search all files inside the directory. Adding an asterisk forces grep to search all files within the electric current directory. Detect that the returned results include the result from test3 where the search cord steve is independent within Steven. Notation besides that these results don't contain the effect from test4 as in its basic form the grep command is case sensitive.

          grep steve *        

4. Add together the argument -i to brand grep case insensitive. This will return results from all 4 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 employ it to search for patterns / keywords.

1. Open a new terminal window.

2. Utilise lsusb to list all of the USB devices attached to your machine. This volition also listing internal USB devices, such as laptop webcams. The output volition differ depending on your car, but yous should be met with a wall of text.

          lsusb        

(Image credit: Tom'due south Hardware)

three. Use the lsusb command once again, just this time use grep to search for Linux. By adding a pipe between lsusb and grep the output of the outset command is used as the input of the second.

          lsusb | grep Linux        

(Prototype 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'll utilise dmesg and grep to audit the Kernel Ring Buffer (essentially the kernel's log file). We are going to search for a keyword in dmesg, "secureboot", and confirm that it is enabled.

i. Open a last and run the dmesg command equally sudo. This will impress a wall of panel output to the last, something that we can search using grep.

          sudo dmesg        

(Image credit: Tom's Hardware)

2. Utilise the grep command to search for "secureboot" in the dmesg output. Utilize the -i statement to plow off case-sensitivity so that we take hold of every occurrence of secureboot. The output will show the lines at which secureboot appears in dmesg.

          sudo dmesg | less | grep -i secureboot        

(Image credit: Tom's Hardware)

Other Uses of grep

(Epitome credit: Tom'due south Hardware)

Like many Linux commands in that location are many useful additions and variants for the grep command. Let's look at a couple of interesting examples.

one. Perform an inverted search using the -v argument. This will return a list of every line from the test surround files that doesn't contain the search string steve. This statement is useful to dismiss occurrences of strings in logs or files when debugging an issue. Note that over again the results are instance sensitive and therefore they include the line containing the capitalized Steve ? from test4.

          grep -v steve *        

2. Combine the -v and -i arguments to exclude all matching strings regardless of example.

          grep -six steve *        

3. Search for a string that contains not alphanumeric text or spaces. If yous include a search string with a infinite or other non alphanumeric text this can intermission the grep command syntax, to create a search cord containing these you demand to utilize quotes to contain the string. In this step we are searching for "Steve ?" which is contained in the test4 file.

          grep "Steve ?" *        

Searching Subdirectories with grep

(Image credit: Tom's Hardware)

Like many Linux commands there are many useful additions and variants for the grep control. Let'south look at a couple of interesting examples.

one. Perform an inverted search using the -v statement. This will render a list of every line from the exam environment files that doesn't comprise the search string steve. This statement is useful to dismiss occurrences of strings in logs or files when debugging an consequence. Notation that again the results are instance sensitive and therefore they include the line containing the capitalized Steve ? from test4.

          grep -v steve *        

2. Combine the -v and -i arguments to exclude all matching strings regardless of case.

          grep -6 steve *        

3. Search for a string that contains not alphanumeric text or spaces. If you include a search string with a space or other non alphanumeric text this can break the grep command syntax, to create a search string containing these y'all need to utilise quotes to contain the cord. In this pace nosotros are searching for "Steve ?" which is contained in the test4 file.

          grep "Steve ?" *        

Searching Subdirectories with grep

(Epitome credit: Tom's Hardware)

Oft we will want to search for a string in the files contained in sub directories. Nosotros can do this simply by adding the -r recursive argument to the grep command.

1. Create a subdirectory containing a examination file within the examination directory.

          mkdir sub_directory cd sub_directory touch test5        

ii. Open test5 using the nano text editor and add the text "steve in a sub directory" to the file. Then press CTRL + X, so Y and Enter to save and go out.

          nano test5.        

3. Return to the exam directory and perform a search adding the -r choice. Notice 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 in Linux, you accept a practiced drove of approaches for searching out file contents across your system. As with many Linux commands it tin can be worthwhile looking at the help carte to come across all the varied arguments you can add to grep. Run grep --h in a terminal emulator to check out all the options.

MORE: How To Check Disk Usage in Linux

MORE: How To Impale a Process in Linux

MORE: How To Find Files in Linux

robinsonving1958.blogspot.com

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

Post a Comment for "Grep Command Running Over and Over Again How to Stop a Grep Command"