Movatterモバイル変換


[0]ホーム

URL:


How-To Geek logo

How to Use the grep Command on Linux

A terminal open on UbuntuCredit: Hannah Stryker / How-To Geek

 
4
By Dave McKay
Updated 
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he became a full-time technology journalist. His first published article, describing macros in 6502 assembly language, was published in the December 1985 edition of the UK magazine Personal Computer World. His work has appeared on How-To Geek since March 2019. Despite graduating from Sheffield University as an archaeologist, during his career he's worked as a freelance programmer, manager of an international software development team, project manager for IT installations, and a Data Protection Officer. Dave is a Linux evangelist and open source advocate. Since 1997, all of Dave's computers have run Linux. He is a frequent guest speaker at national conferences, usually talking about topics such as artificial intelligence, data protection legislation, and cybersecurity. Expect detailed how-to's, distribution reviews, and Linux-centric editorials. 
Sign in to yourHow-To Geek account
Jump links

Jump Links

follow
Follow
followed
Followed
Here is a fact-based summary of the story contents:
Try something different:

Summary

  • The Linux grep command is a useful tool for string and pattern matching, allowing you to search through text files using various options.
  • With grep, you can perform simple searches, recursive searches, search for whole words, use multiple search terms, count matches, add context, and even pipe the output to other commands for further manipulation.

The Linuxgrep command is a string and pattern matching utility that displays matching lines from multiple files. It also works withpiped output from other commands. We show you how.

The Grep Command in Linux

Thegrep command is famous in Linux andUnix circles for three reasons. Firstly, it is tremendously useful. Secondly, thewealth of options can be overwhelming. Thirdly, it was written overnight to satisfy a particular need. The first two are bang on; the third is slightly off.

Ken Thompson had extracted theregular expression search capabilities from theed editor (pronounced ee-dee) and created a little program — for his own use — to search through text files. His department head atBell Labs,Doug Mcilroy, approached Thompson and described the problem one of his colleagues,Lee McMahon, was facing.

McMahon was trying to identify the authors of theFederalist papers through textual analysis. He needed a tool that could search for phrases and strings within text files. Thompson spent about an hour that evening making his tool a general utility that could be used by others and renamed it asgrep. He took the name from theed command stringg/re/p , which translates as "global regular expression search."

You can watch Thompson talking toBrian Kernighan about the birth ofgrep.

Simple Searches with the grep Command

To search for a string within a file, pass the search term and the file name on the command line:

grep dave /etc/password in a terminal widnow

Matching lines are displayed. In this case, it is a single line. The matching text is highlighted. This is because on most distributionsgrep is aliased to:

alias grep='grep --colour=auto'

Let's look at results where there are multiple lines that match. We'll look for the word "Average" in an application log file. Because we can't recall if the word is in lowercase in the log file, we'll use the-i (ignore case) option:

grep -i Average geek-1.log

grep -i Average geek-1.log ina terminal window

Every matching line is displayed, with the matching text highlighted in each one.

Output from grep -i Average geek-1.log in a terminal window

We can display the non-matching lines by using the -v (invert match) option.

grep -v Mem geek-1.log

grep -v Mem geek-1.log in a terminal window

There is no highlighting because these are the non-matching lines.

output from grep -v Mem geek-1.log in a terminal window

We can causegrep to be completely silent. The result is passed to the shell as a return value fromgrep. A result of zero means the string was found, and a result of one means it was not found. We can check the return code using the$?special parameters:

grep -q average geek-1.log

echo $?

grep -q howtogeek geek-1.log

echo $?

grep -q average geek-1.log in a terminal window

Recursive Searches Using grep

To search through nested directories and subdirectories, use the -r (recursive) option. Note that you don't provide a file name on the command line, you must provide a path. Here we're searching in the current directory "." and any subdirectories:

grep -r -i memfree .

grep -r -i memfree . in a terminal window

The output includes the directory and filename of each matching line.

output from grep -r -i memfree . in a terminal window

We can makegrep follow symbolic links by using the-R (recursive dereference) option. We've got a symbolic link in this directory, calledlogs-folder. It points to/home/dave/logs.

ls -l logs-folder

ls -l logs-folder in a terminal window

Let's repeat our last search with the-R (recursive dereference) option:

grep -R -i memfree .

grep -R -i memfree . in a terminal window

The symbolic link is followed and the directory it points to is searched bygrep too.

Output from grep -R -i memfree . in a terminal window

Searching for Whole Words with the grep Command

By default,grep will match a line if the search target appears anywhere in that line, including inside another string. Look at this example. We're going to search for the word "free."

grep -i free geek-1.log

grep -i free geek-1.log in a terminal window

The results are lines that have the string "free" in them, but they're not separate words. They're part of the string "MemFree."

Output from grep -i free geek-1.log in a terminal window

To forcegrep to match separate "words" only, use the-w (word regexp) option.

grep -w -i free geek-1.log

echo $?

grep -w -i free geek-1.log in a terminal window

This time there are no results because the search term "free" does not appear in the file as a separate word.

Using Multiple Search Terms

The-E (extended regexp) option allows you to search for multiple words. (The-E optionreplaces the deprecatedegrep version ofgrep.)

This command searches for two search terms, "average" and "memfree."

grep -E -w -i "average|memfree" geek-1.log

grep -E -w -i "average|memfree" geek-1.log in a terminal window

All of the matching lines are displayed for each of the search terms.

Output from grep -E -w -i "average|memfree" geek-1.log in a terminal window

You can also search for multiple terms that are not necessarily whole words, but they can be whole words too.

The-e (patterns) option allows you to use multiple search terms on the command line. We're making use of the regular expression bracket feature to create a search pattern. It tellsgrep to match any one of the characters contained within the brackets "[]." This meansgrep will match either "kB" or "KB" as it searches.

grep -e MemFree -e [kK]B geek-1.log in a terminal window

Both strings are matched, and, in fact, some lines contain both strings.

Output from grep -e MemFree -e [kK]B geek-1.log in a terminal window

Matching Lines Exactly

The-x (line regexp) will only match lines where the entire line matches the search term. Let's search for a date and time stamp that we know appears only once in the log file:

grep -x "20-Jan--06 15:24:35" geek-1.log

grep -x "20-Jan--06 15:24:35" geek-1.log in a terminal window

The single line that matches is found and displayed.

The opposite of that is only showing the lines that don't match. This can be useful when you're looking at configuration files. Comments are great, but sometimes it's hard to spot the actual settings in amongst them all. Here's the/etc/sudoers file:

Contents of the /etc/sudoers file in a terminal window

We can effectively filter out the comment lines like this:

sudo grep -v "#" /etc/sudoers

sudo grep -v "#" /etc/sudoers in a terminal window

That's much easier to parse.

Only Displaying Matching Text

There may be an occasion when you don't want to see the entire matching line, just the matching text. The-o (only matching) option does just that.

grep -o MemFree geek-1.log

grep -o MemFree geek-1.log in a terminal window

The display is reduced to showing only the text that matches the search term, instead of the entire matching line.

output from grep -o MemFree geek-1.log in a terminal window

Counting With grep

grep isn't just about text, it can provide numerical information too. We can makegrep count for us in different ways. If we want to know how many times a search term appears in a file, we can use the-c (count) option.

grep -c average geek-1.log

grep -c average geek-1.log in a terminal window

grep reports that the search term appears 240 times in this file.

You can makegrep display the line number for each matching line by using the-n (line number) option.

grep -n Jan geek-1.log

grep -n jan geek-1.log in a terminal window

The line number for each matching line is displayed at the start of the line.

Output from grep -n jan geek-1.log in a terminal window

To reduce the number of results that are displayed, use the-m (max count) option. We're going to limit the output to five matching lines:

grep -m5 -n Jan geek-1.log

grep -m5 -n Jan geek-1.log in a terminal window

Adding Context with grep

Being able to see some additional lines — possibly non-matching lines —for each matching line is often useful. it can help distinguish which of the matched lines are the ones you are interested in.

To show some lines after the matching line, use the -A (after context) option. We're asking for three lines in this example:

grep -A 3 -x "20-Jan-06 15:24:35" geek-1.log

grep -A 3 -x "20-Jan-06 15:24:35" geek-1.log in a terminal window

To see some lines from before the matching line, use the-B (context before) option.

grep -B 3 -x "20-Jan-06 15:24:35" geek-1.log

grep -B 3 -x "20-Jan-06 15:24:35" geek-1.log in a terminal window

And to include lines from before and after the matching line use the-C (context) option.

grep -C 3 -x "20-Jan-06 15:24:35" geek-1.log

grep -C 3 -x "20-Jan-06 15:24:35" geek-1.log in a terminal window

Showing Matching Files

To see the names of the files that contain the search term, use the-l (files with match) option. To find out which C source code files contain references to thesl.h header file, use this command:

grep -l "sl.h" *.c

grep -l "sl.h" *.c in a terminal window

The file names are listed, not the matching lines.

Output from grep -l "sl.h" *.c in a terminal window

And of course, we can look for files that don't contain the search term. The-L (files without match) option does just that.

grep -L "sl.h" *.c

grep -L "sl.h" *.c in a terminal window

Start and End of Lines

We can forcegrep to only display matches that are either at the start or the end of a line. The "^" regular expression operator matches the start of a line. Practically all of the lines within the log file will contain spaces, but we're going to search for lines that have a space as their first character:

grep "^ " geek-1.log

grep "^ " geek-1.log in a terminal window

The lines that have a space as the first character — at the start of the line — are displayed.

Output grep "^ " geek-1.log in a terminal window

To match the end of the line, use the "$" regular expression operator. We're going to search for lines that end with "00."

grep "00$" geek-1.log

grep "00$" geek-1.log in a terminal window

The display shows the lines that have "00" as their final characters.

output of grep "00$" geek-1.log in a terminal window

Using Pipes with grep

Of course, you can pipe input togrep , pipe the output fromgrep into another program, and havegrep nestled in the middle of a pipe chain.

Let's say we want to see all occurrences of the string "ExtractParameters" in our C source code files. We know there's going to be quite a few, so we pipe the output intoless:

grep "ExtractParameters" *.c | less

grep "ExtractParameters" *.c | less in a terminal window

The output is presented inless.

Output from grep "ExtractParameters" *.c | less in a terminal window

This lets you page through the file listing and to useless's search facility.

If we pipe the output fromgrep intowc and use the-l (lines) option, wecan count the number of lines in the source code files that contain "ExtractParameters". (We could achieve this using thegrep-c (count) option, but this is a neat way to demonstrate piping out ofgrep.)

grep "ExtractParameters" *.c | wc -l

grep "ExtractParameters" *.c | wc -l in a terminal window

With the next command, we're piping the output fromls intogrep and piping the output fromgrep intosort . We're listing the files in the current directory, selecting those with the string "Aug" in them,and sorting them by file size:

ls -l | grep "Aug" | sort +4n

ls -l | grep "Aug" | sort +4n in a terminal window

Let's break that down:

  • ls -l: Perform a long format listing of the files usingls.
  • grep "Aug": Select the lines from thels listing that have "Aug" in them. Note that this would also find files that have "Aug" in their names.
  • sort +4n: Sort the output from grep on the fourth column (filesize).

We get a sorted listing of all the files modified in August (regardless of year), in ascending order of file size.

grep in Linux: Less a Command, More of an Ally

grep is a terrific tool to have at your disposal. It dates from 1974 and is still going strong because we need what it does, and nothing does it better.

Couplinggrep with someregular expressions-fu really takes it to the next level.

Linux Commands

Files

tar·pv·cat·tac·chmod·grep ·diff·sed·ar·man·pushd·popd·fsck·testdisk·seq·fd·pandoc·cd·$PATH·awk·join·jq·fold·uniq·journalctl·tail·stat·ls·fstab·echo·less·chgrp·chown·rev·look·strings·type·rename·zip·unzip·mount·umount·install·fdisk·mkfs·rm·rmdir·rsync·df·gpg·vi·nano·mkdir·du·ln·patch·convert·rclone·shred·srm·scp·gzip·chattr·cut·find·umask·wc·tr

Processes

alias·screen·top·nice·renice·progress·strace·systemd·tmux·chsh·history·at·batch·free·which·dmesg·chfn·usermod·ps·chroot·xargs·tty·pinky·lsof·vmstat·timeout·wall·yes·kill·sleep·sudo·su·time·groupadd·usermod·groups·lshw·shutdown·reboot·halt·poweroff·passwd·lscpu·crontab·date·bg·fg·pidof·nohup·pmap

Networking

netstat·ping·traceroute·ip·ss·whois·fail2ban·bmon·dig·finger·nmap·ftp·curl·wget·who·whoami·w·iptables·ssh-keygen·ufw·arping·firewalld

Follow
Followed
Share
FacebookXWhatsAppThreadsBlueskyLinkedInRedditFlipboardCopy linkEmail
Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission.Read More.
A relaxed man lounging on an orange beanbag watches as a friendly yellow robot works on a laptop for him, while multiple red exclamation-mark warning icons float around them.
3 reasons why vibe coding is a terrible idea
A MacBook surrounded by a gear symbol, a shield, an iCloud icon, and a password dots bar.
I made my Mac more secure by changing these 5 settings
A Chromebook keyboard with the search button as the center focus.
These 5 Chromebook tips save me tons of time in Google Docs
See More
The back of the OnePlus 15 sitting in grass and leaves.
The OnePlus 15 can finally be sold in the U.S.
A replacement battery for a Kindle third generation eReader.
It’s time to admit you can swap out internal rechargeable batteries yourself
Several smartphones arranged diagonally on a blue geometric background, each displaying a simple home screen with a solid black wallpaper
Black is the new best wallpaper for your phone
See More

[8]ページ先頭

©2009-2025 Movatter.jp