

You may be searching for a very short, yet specific word. By using a regular expression we can express this in short notation (e.g. Think of something like “I know the word should be starting with the letter ‘a’, but after that everything is fine”. Regular expressions can be considered ‘logic rules’ for matching text strings. The grep utility is a powerful tool and can use regular expressions. To show all files that do not match your target, use the capitalized version: grep -L. There is the grep -l command to do achieve this. Sometimes you just want to see the files that match a particular text string. Tip: if you don’t want the filenames in the output, add the -h option. Use the capitalized option, grep -R, if you want to include any possible symlinked file to be searched as well. This may take much longer and could result in other file systems to be searched as well. Depending on the target and the existence of symlinks, you might want to use the first one if you do not want to follow them. To search in one directory, there are the -r and -R options to achieve this. Grep -c monkey zoo.txt Recursive search through directories and files This count is displayed when using grep -c or grep -c -v to show the number of non-matching lines. It may be useful to know the number of occurrences of your specified word. Grep -i -v -E 'banana|monkey' zoo.txt Match counting Optionally make it case insensitive with the -i as listed above. Exclude multiple words with grep by adding -E and use a pipe (|) to define the specific words. To exclude particular words or lines, use the –invert-match option. Use the -n option to have grep show the related line numbers. Grep -i root /etc/passwd Show line numbersĭepending on your search, you may have many occurrences of the text you were searching for. We can tell grep to ignore case-sensitive searches with the -ioption. So only the right match will be displayed. By default, the grep command will be case-sensitive.

What we don’t always know is if one or more occurrences of the word are using capitals. Now that we have performed a basic grep command, we can start to change its behavior. Often we already know the word or words we are looking for. These files can be found in your home directory. Otherwise, add it to the respective profile file.

bashrc file if you are using the bash shell. As this would mean you have to type it in each time, using an alias would save you from a lot of typing. If the command above did not show colored output on your system, you might want to enable that. If you would like to find the root user in your /etc/passwd file, just tell it to search for ‘root’ and the file name itself. Use the short notation in your daily tasks and on the command line, to save on typing and speed up your work. Use the long format when using them in scripts, so that it becomes obvious what the grep command is doing. The long format has two dashes, followed by a word or words. The options that grep uses typically have a long and short format. The syntax of grep consists of four parts. The grep utility does not need much to starts doing its work. Need a particular job to be done with the grep command and can’t get it to work? Use the comments and share what you have tried. Let’s start with the basics and become a ‘grep master’.īasic usage examples of grep Use grep for simple actions

You can easily find out what version you have with grep -version. Some things in this guide may not be working, or need specific tailoring. If you are using grep on another platform than Linux, you may not have the GNU version of grep. Especially if you have to deal often with data, investing some time in doing things efficiently will pay off. And we will include specific use-cases which are common for system administrators and security professionals. Although these sources are a great reference, we will be showing the grep command by example. The man page is very extensive, so is the online help documentation. Why learn the grep command and regular expressions?Īs with every tool, it is often easy to start using it, but hard to really master it. It is common to use it for checking configuration files and searching through log files. We want to leverage the full potential of the grep command, as it can be used in many work-related or personal related activities. But with all information scattered, most people don’t take the time to really learn the most basic commands. One of the reasons to create this blog post is that there are a lot of examples available for the grep command. Searching inside compressed data (avoid using gunzip!).Find lines ending with a specific string.Find lines starting with a specific string.Recursive search through directories and files.Why learn the grep command and regular expressions?.
