forked from flashreads/blogs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added short article about grep command, as requested on flashreads#157
- Loading branch information
1 parent
7fc97c9
commit 8aeb55e
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
id: 012-grep-howto.md | ||
title: Grep command usage cheatsheet | ||
tags: | ||
- linux | ||
- grep | ||
author: waltherman | ||
meta-description: Grep is an ultimate command to search text patterns in files | ||
date: 2021-10-13 03:30:00 +0300 | ||
keywords: linux, grep, searching in files | ||
template: post | ||
categories: | ||
- linux | ||
cover: ../../images/categories/linux.png | ||
--- | ||
|
||
# What is Grep? | ||
|
||
Grep stands for "global regular expression print" and it is an ultimate command to search text or any file for lines that contain a match to the specified text pattern, word or regex. | ||
By default, grep outputs only the matched lines. | ||
|
||
## Basic grep usage | ||
|
||
``` | ||
$ grep pattern filename | ||
``` | ||
|
||
As a result grep would give all patterns found in file named "filename". For example: | ||
|
||
``` | ||
$ grep pattern filename | ||
pattern | ||
pattern1 | ||
zzzpattern44 | ||
``` | ||
|
||
Also you could use pipe to filter output of any command. For example, let's find installed PostgreSQL client version on Ubuntu server via next command: | ||
``` | ||
$ dpkg --list | grep postgresql-client | ||
``` | ||
|
||
-i parameter used for case-insensetive grep | ||
``` | ||
$ grep -i pattern filename | ||
``` | ||
-v - inverted match of grep | ||
|
||
``` | ||
$ grep -v pattern filename | ||
``` | ||
|
||
-w used for search only for words match. For example file named "filename" from very first example had several matches, but with this argument there would be only one: | ||
``` | ||
$ grep -w pattern filename | ||
pattern | ||
``` | ||
|
||
Recursive search | ||
Let's find all files matching pattern in this directory and all subdirectories by -r/R argument: | ||
|
||
``` | ||
$ grep -R pattern . | ||
``` | ||
Also we could use grep to find file by its content: | ||
|
||
``` | ||
$ grep -rnw '/path/to/somewhere/' -e 'pattern' | ||
``` |