Support Online
Skip to main content

Using the Grep Command: A Guide to Searching and Filtering Text in Linux

When it comes to text searching and filtering in the Linux terminal, the first tool that comes to mind is the grep command**.
In this guide, you will learn all the details from the basic use of grep to advanced searches with regex support.

What Will You Learn in This Guide?

  • working logic of grep command
  • Search for text in file and command outputs
  • Case insensitive search
  • Use recursive grep within folders
  • Advanced pattern searching with Regex

What is Grep?

grep is an abbreviation for Global Regular Expression Print.
Searches for specific text patterns in files or standard output and lists matching lines.

It is available by default in all Linux distributions.


Basic Grep Usage

grep "aranan_metin" dosya_adi
  • This command shows matching lines within the specified file.

Example


grep "Linux" welcome.txt
  • This command lists all lines passing Linux.

Case Insensitive Search (-i)


grep -i "linux" welcome.txt
  • This command disables case sensitivity.

Deep Search Within Folders (-r)


grep -r "hata" /var/log
  • This command searches all subdirectories.

Display by Line Number (-n)


grep -n "Ubuntu" welcome.txt
  • This command also shows the number of matching lines.

Do Not Show Mismatched Lines (-v)


grep -v "Linux" welcome.txt
  • This command lists the lines where the searched word does not appear.

Exact Word Match (-w)


grep -w "open" welcome.txt
  • This command only finds exact word matches.

Using Pipe (|) with grep

  • grep is frequently used to filter the output of other commands.

dpkg -l | grep -i "openssh"
  • This command checks the OpenSSH package on the system.

Advanced Grep Usage with Regex

1. Line return match (^)


grep ^D welcome.txt

2. End of line match ($)


grep x$ welcome.txt

3. Multiple word search (-E)


grep -E "hata|uyari|error" log.txt

Frequently Asked Questions (FAQ)

1. Why do grep results not appear in color? You can use the --color parameter.


grep --color "Linux" welcome.txt

2. How do I find how many times a word occurs?


grep -c "Linux" welcome.txt

3. Can I exclude certain folders from search?


grep -r --exclude-dir=node_modules "metin" .

4. What is the difference between grep and awk?

Featuregrepawk
Main PurposeText searchText processing
Speed ​​Very fastMedium
Data ManipulationNoneYes
Usage AreaFilteringReporting

Result

The grep command is indispensable for text search and log analysis on Linux systems. When used with the right parameters, it filters large data files in seconds.

You can immediately try grep and other terminal tools on high-performance Linux servers on the GenixNode infrastructure.