
That line is not printed when grep is invoked with -rbecause files inside the Nginx’s sites-enabled directory are symlinks to configuration files inside the sites-available directory. Notice the last line of the output below. etc/nginx/sites-available/: server_name If you use the -R option, grep will follow all symbolic links: grep -R /etc The output will include matching lines prefixed by the full path to the file: Here is an example showing how to search for the string in all files inside the /etc directory: grep -r /etc , instead of -r, use the -R option (or -dereference-recursive).

When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.

To recursively search for a pattern, invoke grep with the -r option (or -recursive). If you don’t want that line to be shown pass the output to another grep instance as shown below. As you can see in the output above there is also a line containing the grep process. You can also chain multiple pipes in on command. Using Grep to Filter the Output of a Command #Ī command’s output can be filtered with grep through piping, and only the lines matching a given pattern will be printed on the terminal.įor example, to find out which processes are running on your system as user www-data you can use the following psĬommand: ps -ef | grep www-data www-data 18247 12675 4 16:00 ? 00:00:00 php-fpm: pool www To display the lines that do not match a pattern, use the -v ( or -invert-match) option.įor example, to print the lines that do not contain the string nologin you would use: grep -v nologin /etc/passwd root:x:0:0:root:/root:/bin/bashĬolord:x:124:124::/var/lib/colord:/bin/false If the string includes spaces, you need to enclose it in single or double quotation marks: grep "Gnome Display Manager" /etc/passwd Invert Match (Exclude) # The output should look something like this: root:x:0:0:root:/root:/bin/bash The most basic usage of the grep command is to search for a string (text) in a file.įor example, to display all the lines containing the string bash from the /etc/passwdįile, you would run the following command: grep bash /etc/passwd

To be able to search the file, the user running the command must have read access to the file. You can use the grep command for searching and printing any line from any Linux command output that matches with the specified pattern.The items in square brackets are optional. In this part of the article, we’ll cover the most common examples of using grep. But if you do not have it installed on your system, you can do it by using the following command, for example: apt-get updateapt-get install -y grep grep usage examples Usually, grep is installed in most Linux systems and even small Docker containers. There are many different examples of using grep, so, let’s take a look how and when you can use it. Grep command prints out a line to the terminal with the string if it finds a match with the search query. This pattern we mentioned above, is known as a regular expression. The main purpose of this tool is to check for string or patterns in a specified file, or filtering other commands output. Grep is the most useful command on Linux/Unix for system administrators, developers and DevOps engineers. This is a very handy utility when it comes to searching through a large set of text files (logs, for example).

Grep stands for Global Regular Expression Print. In this article we’ll cover the most common use-cases for grep which will definitely simplify your daily routine. Linux provides a great tool which allows us to solve that task with ease – grep. One of the most commonly used operations in Linux terminal are searching through text files and filtering standard output or error streams. Print line number for search pattern match.Number of lines before searched pattern.
