find . -name test.c

grep -r linuxize.com /etc

$ grep -E A.+a example.txt
Albania
Algeria

You can use square brackets to provide a list of letters:

$ grep -E [AC].+a example.txt
Albania
Algeria
Canada

This works for numbers, too. The results may surprise you:

$ grep [1-9] example.txt
1
3
11

https://linuxize.com/post/how-to-use-grep-command-to-search-files-in-linux/

 

Grep Command in Linux

Updated on Jan 23, 2024


9 min read

 

find grep_nginx

Grep, an acronym for “Global Regular Expression Print”, is one of the most commonly used commands in Linux.

grep is a command-line utility that searches for a specific text string in one or more files. It looks for the pattern in each line of the file and prints out the lines that match it. It is a powerful tool for searching and analyzing large amounts of text data quickly and efficiently. You can use the command to search for specific words, phrases, or regular expressions in files of any size, from small text files to large log files. If no files are specified, grep reads from the standard input, which is usually the output of another command.

 

 

This video cannot be played because of a technical error.(Error Code: 102001)

This article will demonstrate how to use the grep command with practical examples and detailed explanations of the most common GNU grep options.

grep Command Syntax

The syntax for the grep command is as follows:

grep [OPTIONS] PATTERN [FILE...]

Copy

The items in square brackets are optional.

  • OPTIONS - Zero or more options. Grep includes a number of options that control its behavior.
  • PATTERN - Search pattern.
  • FILE - Zero or more input file names.

To be able to search the file, the user running the command must have read access to the file.

Search for a String in Files

The most basic usage of the grep command is to search for a string (text) in a file.

For instance, to find all the lines containing the string “bash” in the /etc/passwd file, you would run the following command:

grep bash /etc/passwd

The output will look something like this:

root:x:0:0:root:/root:/bin/bash
linuxize:x:1000:1000:linuxize:/home/linuxize:/bin/bash

If the string contains spaces, you need to enclose it in single or double quotation marks:

grep "Gnome Display Manager" /etc/passwd

Invert Match (Exclude)

Use the -v ( or --invert-match) option to display the lines that do not match a pattern.

For 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
colord:x:124:124::/var/lib/colord:/bin/false
git:x:994:994:git daemon user:/:/usr/bin/git-shell
linuxize:x:1000:1000:linuxize:/home/linuxize:/bin/bash

Using Grep to Filter the Output of a Command

A command’s output can be filtered with grep through piping, and only the lines matching a given pattern will be printed on the terminal. This can make it much easier to find specific information within a large output.

For instance, to find out which processes are running on your system as user www-data you can use the following ps command:

ps -ef | grep www-data
www-data 18247 12675  4 16:00 ?        00:00:00 php-fpm: pool www
root     18272 17714  0 16:00 pts/0    00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn www-data
www-data 31147 12770  0 Oct22 ?        00:05:51 nginx: worker process
www-data 31148 12770  0 Oct22 ?        00:00:00 nginx: cache manager process

You can also chain multiple pipes in on command. As you can see in the output above, there is also a line containing the grep process. If you wish to exclude that line from the output, you can pass it to another grep instance, as demonstrated below:

ps -ef | grep www-data | grep -v grep
www-data 18247 12675  4 16:00 ?        00:00:00 php-fpm: pool www
www-data 31147 12770  0 Oct22 ?        00:05:51 nginx: worker process
www-data 31148 12770  0 Oct22 ?        00:00:00 nginx: cache manager process

Recursive Search

To recursively search for a pattern, invoke grep with the -r option (or --recursive). When this option is used, grep will search through all files in the specified directory, including its subdirectories, skipping the symlinks that are encountered recursively. This allows you to easily search through your entire directory structure to find exactly what you’re looking for.

To follow all symbolic links instead of -r, use the -R option (or --dereference-recursive).

Here is an example showing how to search for the string linuxize.com in all files inside the /etc directory:

grep -r linuxize.com /etc

The output will include matching lines prefixed by the full path to the file:

/etc/hosts:127.0.0.1 node2.linuxize.com
/etc/nginx/sites-available/linuxize.com:    server_name linuxize.com   www.linuxize.com;

If you use the -R option, grep will follow all symbolic links:

grep -R linuxize.com /etc

Notice the last line of the output below. That line is not printed when grep is invoked with -r because files inside the Nginx’s sites-enabled directory are symlinks to configuration files inside the sites-available directory.

/etc/hosts:127.0.0.1 node2.linuxize.com
/etc/nginx/sites-available/linuxize.com:    server_name linuxize.com   www.linuxize.com;
/etc/nginx/sites-enabled/linuxize.com:    server_name linuxize.com   www.linuxize.com;

Show Only the Filename

To suppress the default grep output and print only the names of files containing the matched pattern, use the -l ( or --files-with-matches) option.

The following command will search for all files ending with .conf in the current working directory and display only the names of the files containing the string linuxize.com:

grep -l linuxize.com *.conf

The output will look something like this:

tmux.conf
haproxy.conf

The -l flag is commonly used together with the recursive option -R:

grep -Rl linuxize.com /tmp

Case Insensitive Search

By default, grep is case-sensitive, meaning the uppercase and lowercase characters are treated as distinct. For example, searching for “linux” will not match “Linux”.

To ignore the case when searching, invoke grep with the -i option (or --ignore-case).

For example, when searching for Zebra without any option, the following command will not show any output i.e, there are matching lines:

grep Zebra /usr/share/words

But if you perform a case insensitive search using the -i option, it will match both upper and lower case letters:

grep -i Zebra /usr/share/words

In the command above “Zebra” will match “zebra”, “ZEbrA” or any other combination of upper and lower case letters for that string.

zebra
zebra's
zebras

Search for Full Words

When searching for a string, grep will display all lines where the string is embedded in larger strings.

For example, if you search for the string “gnu”, grep will match all the lines that contain “gnu” as a standalone string or as part of a larger word such as “cygnus” or “magnum”.

grep gnu /usr/share/words
cygnus
gnu
interregnum
lgnu9d
lignum
magnum
magnuson
sphagnum
wingnut

To return only those lines where the specified string is a whole word (enclosed by non-word characters), use the -w ( or --word-regexp) option.

Word characters include alphanumeric characters (a-zA-Z, and 0-9) and underscores (_). All other characters are considered as non-word characters.

If you run the command with the -w option, the grep command will return only those lines where gnu is included as a separate word.

grep -w gnu /usr/share/words
gnu

Show Line Numbers

The -n ( or --line-number) option tells grep to display the line number of all the lines that match a given pattern. When this option is used, grep outputs the matched lines to the standard output with their corresponding line numbers as a prefix.

For instance, to display the lines from the /etc/services file containing the string bash prefixed with the matching line number, you can use the following command:

grep -n 10000 /etc/services

The output below shows us that the matches are found on lines 10423 and 10424.

10423:ndmp            10000/tcp
10424:ndmp            10000/udp

Count Matches

To print a count of matching lines to standard output, use the -c ( or --count) option.

In the example below, we are counting the number of accounts that have /usr/bin/zsh as a shell.

regular expression
grep -c '/usr/bin/zsh' /etc/passwd
4

Quiet Mode

The -q (or --quiet) tells grep to run in quiet mode and not to display anything on the standard output. If a match is found, the command exits with status 0. This is useful when using grep in shell scripts where you want to check whether a file contains a string and perform a specific action depending on the result.

Here is an example of using grep in a quiet mode as a test command in an if :

if grep -q PATTERN filename
then
    echo pattern found
else
    echo pattern not found
fi

Copy

Basic Regular Expression

GNU Grep has three regular expression feature sets: Basic, Extended, and Perl-compatible.

 

By default, grep interprets the pattern as a basic regular expression where all characters except the meta-characters are actually regular expressions that match themselves.

Below is a list of most commonly used meta-characters:

  • Use the ^ (caret) symbol to match the expression at the start of a line. In the following example, the string kangaroo will only match if it occurs at the beginning of a line.
grep "^kangaroo" file.txt
  • Use the $ (dollar) symbol to match the expression at the end of a line. In the following example, the string kangaroo will only match if it occurs at the end of a line.
grep "kangaroo$" file.txt
  • Use the . (period) symbol to match any single character. For example, to match anything that begins with kan, then has two characters, and ends with the string roo, you could use the following pattern:
grep "kan..roo" file.txt
  • Use [ ] (brackets) to match any single character enclosed in the brackets. For instance, to find the lines that contain accept or “accent, you could use the following pattern:
grep "acce[np]t" file.txt
  • Use [^ ] to match any single character not enclosed in the brackets. The following pattern will match any combination of strings containing co(any_letter_except_l)a, such as cocacobalt, and so on, but will not match the lines containing cola,
grep "co[^l]a" file.txt

 

To escape the special meaning of the next character, use the \ (backslash) symbol.

Extended Regular Expressions

To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp) option. Extended regular expressions include all of the basic meta-characters, along with additional meta-characters to create more complex and powerful search patterns. Below are some examples:

  • Match and extract all email addresses from a given file:
grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" file.txt
  • Match and extract all valid IP addresses from a given file:
grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' file.txt

The -o option is used to print only the matching string.

Search for Multiple Strings (Patterns)

Two or more search patterns can be joined using the OR operator |.

By default, grep interprets the pattern as a basic regular expression where the meta-characters such as | lose their special meaning, and their backslashed versions must be used.

In the example below, we are searching all occurrences of the words fatalerror, and critical in the Nginx log error file:

 

grep 'fatal\|error\|critical' /var/log/nginx/error.log

If you use the extended regular expression option -E, then the operator | should not be escaped, as shown below:

grep -E 'fatal|error|critical' /var/log/nginx/error.log

Print Lines Before a Match

To print a specific number of lines before matching lines, use the -B ( or --before-context) option.

For example, to display five lines of leading context before matching lines, you would use the following command:

grep -B 5 root /etc/passwd

Print Lines After a Match

To print a specific number of lines after matching lines, use the -A ( or --after-context) option.

For example, to display five lines of trailing context after matching lines, you would use the following command:

grep -A 5 root /etc/passwd

Conclusion

The grep command allows you to search for a pattern inside of files. If a match is found, grep prints the lines containing the specified pattern.

 

There’s much more to learn about Grep on Grep User’s Manual page.

If you have any questions or feedback, feel free to leave a comment.

https://linuxize.com/post/how-to-use-grep-command-to-search-files-in-linux/

 

########################################################################################################3

 

How to use the Linux grep command

Learn the basics on searching for info in your files, then download our cheat sheet for a quick reference guide to grep and regex.

By Seth Kenlon (Team, Red Hat)

March 18, 2021 | 9 Comments | 8 min read

 

119 readers like this.

 

 

find grep_linux_02

Image by:

Internet Archive Book Images. Modified by Opensource.com. CC BY-SA 4.0

One of the classic Unix commands, developed way back in 1974 by Ken Thompson, is the Global Regular Expression Print (grep) command. It's so ubiquitous in computing that it's frequently used as a verb ("grepping through a file") and, depending on how geeky your audience, it fits nicely into real-world scenarios, too. (For example, "I'll have to grep my memory banks to recall that information.") In short, grep is a way to search through a file for a specific pattern of characters. If that sounds like the modern Find function available in any word processor or text editor, then you've already experienced grep's effects on the computing industry.

Far from just being a quaint old command that's been supplanted by modern technology, grep's true power lies in two aspects:

  • Grep works in the terminal and operates on streams of data, so you can incorporate it into complex processes. You can not only find a word in a text file; you can extract the word, send it to another command, and so on.
  • Grep uses regular expression to provide a flexible search capability.

Learning the grep command is easy, although it does take some practice. This article introduces you to some of its features I find most useful.

[Download our free grep cheat sheet]

Installing grep

If you're using Linux, you already have grep installed.

On macOS, you have the BSD version of grep. This differs slightly from the GNU version, so if you want to follow along exactly with this article, then install GNU grep from a project like Homebrew or MacPorts.

Basic grep

The basic grep syntax is always the same. You provide the grep command a pattern and a file you want it to search. In return, it prints each line to your terminal with a match.

$ grep gnu gpl-3.0.txt
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
<http://www.gnu.org/licenses/>.
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

By default, the grep command is case-sensitive, so "gnu" is different from "GNU" or "Gnu." You can make it ignore capitalization with the --ignore-case option.

$ grep --ignore-case gnu gpl-3.0.txt
                    GNU GENERAL PUBLIC LICENSE
  The GNU General Public License is a free, copyleft license for
the GNU General Public License is intended to guarantee your freedom to
GNU General Public License for most of our software; it applies also to
[...16 more results...]
<http://www.gnu.org/licenses/>.
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

You can also make the grep command return all lines without a match by using the --invert-match option:

$ grep --invert-match \
--ignore-case gnu gpl-3.0.txt
                      Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
[...648 lines...]
Public License instead of this License.  But first, please read

Pipes

It's useful to be able to find text in a file, but the true power of POSIX is its ability to chain commands together through "pipes." I find that my best use of grep is when it's combined with other tools, like cut, tr, or curl.

For instance, assume I have a file that lists some technical papers I want to download. I could open the file and manually click on each link, and then click through Firefox options to save each file to my hard drive, but that's a lot of time and clicking. Instead, I could grep for the links in the file, printing only the matching string by using the --only-matching option:

$ grep --only-matching http\:\/\/.*pdf example.html
http://example.com/linux_whitepaper.pdf
http://example.com/bsd_whitepaper.pdf
http://example.com/important_security_topic.pdf

The output is a list of URLs, each on one line. This is a natural fit for how Bash processes data, so instead of having the URLs printed to my terminal, I can just pipe them into curl:

$ grep --only-matching http\:\/\/.*pdf \
example.html | curl --remote-name

 

SKIP TO CONTENT

More Linux resources

This downloads each file, saving it according to its remote filename onto my hard drive.

My search pattern in this example may seem cryptic. That's because it uses regular expression, a kind of "wildcard" language that's particularly useful when searching broadly through lots of text.

Regular expression

Nobody is under the illusion that regular expression ("regex" for short) is easy. However, I find it often has a worse reputation than it deserves. Admittedly, there's the potential for people to get a little too clever with regex until it's so unreadable and so broad that it folds in on itself, but you don't have to overdo your regex. Here's a brief introduction to regex the way I use it.

First, create a file called example.txt and enter this text into it:

Albania
Algeria
Canada
0
1
3
11

The most basic element of regex is the humble . character. It represents a single character.

$ grep Can.da example.txt
Canada

The pattern Can.da successfully returned Canada because the . character represented any one character.

The . wildcard can be modified to represent more than one character with these notations:

  • ? matches the preceding item zero or one time
  • * matches the preceding item zero or more times
  • + matches the preceding item one or more times
  • {4} matches the preceding item four (or any number you enter in the braces) times

Armed with this knowledge, you can practice regex on example.txt all afternoon, seeing what interesting combinations you come up with. Some won't work; others will. The important thing is to analyze the results, so you understand why.

Advanced regex requires the --extended-regexp or -E option.

For instance, this fails to return any country:

$ grep -E A.a example.txt

It fails because the . character can only ever match a single character unless you level it up. Using the * character, you can tell grep to match a single character zero or as many times as necessary until it reaches the end of the word. Because you know the list you're dealing with, you know that zero times is useless in this instance. There are definitely no three-letter country names in this list. So instead, you can use + to match a single character at least once and then again as many times as necessary until the end of the word:

$ grep -E A.+a example.txt
Albania
Algeria

You can use square brackets to provide a list of letters:

$ grep -E [AC].+a example.txt
Albania
Algeria
Canada

This works for numbers, too. The results may surprise you:

$ grep [1-9] example.txt
1
3
11

Are you surprised to see 11 in a search for digits 1 to 9?

What happens if you add 13 to your list?

These numbers are returned because they include 1, which is among the list of digits to match.

As you can see, regex is something of a puzzle, but through experimentation and practice, you can get comfortable with it and use it to improve the way you grep through your data.

Download the cheatsheet

The grep command has far more options than I demonstrated in this article. There are options to better format results, list files and line numbers containing matches, provide context for results by printing the lines surrounding a match, and much more. If you're learning grep, or you just find yourself using it often and resorting to searching through its info pages, you'll do yourself a favor by downloading our cheat sheet for it. The cheat sheet uses short options (-v instead of --invert-matching, for instance) as a way to get you familiar with common grep shorthand. It also contains a regex section to help you remember the most common regex codes. Download the grep cheat sheet today! 

https://opensource.com/article/21/3/grep-cheat-sheet