Korean

grep — Linux basic command

Linux / Basic Linux commands / grep.html

Basic Linux commandsOriginalRegistered 2026-10-19T09:00:00+09:00

A powerful text search command for searching specific patterns in files or input.

Basic Information

Command Namegrep (global regular expression print)
One-line DescriptionA powerful text search command for searching specific patterns in files or input.
Provided ByGNU grep (included by default in all Linux distributions)
Official Documentationhttps://man7.org/linux/man-pages/man1/grep.1.html
Supported OSLinux, macOS, BSD, WSL
Installation StatusPre-installed (no separate installation required)
CategoryBasic Linux Command (Tags: search, text, regex, linux, basic)

What is this command?

grep is a command that searches for and outputs lines matching a specific string or regular expression pattern in files or input. It is one of the most frequently used tools in Linux and is widely used for log analysis, configuration checking, and code exploration.

It supports regular expressions, allowing it to handle everything from simple string searches to complex pattern matching, and comes with many useful options like -i (ignore case), -v (invert match), and -r (recursive).

What problem does it solve?

  • Search for specific keywords in files.
  • Find error messages in logs.
  • Check specific options in configuration files.
  • Batch search for the same pattern across multiple files.

Key Features

Basic String Search

Outputs lines containing a specific string from a file.

  • Use Case: When you need a simple keyword search.
  • Result: All lines containing that string are displayed.
  • Limitations: It can be case-sensitive (modifiable with an option).

Regular Expression Search

Searches for complex patterns using regular expressions.

  • Use Case: When searching text with complex conditions.
  • Result: Lines matching the regular expression are displayed.
  • Limitations: The regular expression syntax can be complex.

Case Insensitive Search (-i)

Searches without distinguishing between uppercase and lowercase letters.

  • Use Case: When searching text with mixed case.
  • Result: All case combinations are matched.
  • Limitations: It may behave unexpectedly in some locales.

Recursive Search (-r)

Recursively searches all files under a directory.

  • Use Case: When finding keywords across an entire project.
  • Result: All files in subdirectories are searched.
  • Limitations: It may be slow for large directories.

Installation and Execution

Checking Pre-installation

It is included by default in all Linux distributions.

which grep
grep --version

Installation (Lightweight Environment)

It is usually already installed.

sudo apt install grep   # Debian/Ubuntu
sudo dnf install grep    # Fedora

Practical Usage Examples

Basic String Search

Search for a string in a file.

# Search in a file
grep "error" /var/log/syslog

# Case insensitive search
grep -i "ERROR" /var/log/syslog

# Show line numbers
grep -n "error" /var/log/syslog

# Invert match (lines without error)
grep -v "error" /var/log/syslog

Multiple Files and Recursive Search

Batch search across multiple files.

# Search multiple files
grep "TODO" *.py

# Recursive directory search
grep -r "import" ./src/

# Specify file type
grep --include="*.py" -r "def main" .

# Show filenames with no matches
grep -rl "config" /etc/

Regular Expression and Piping

Precise search using regular expressions.

# Regular expression search
grep -E "^#" /etc/nginx/nginx.conf

# IP address pattern
grep -E "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" access.log

# With piping
cat access.log | grep " 404 " | wc -l

# Search processes in ps
ps aux | grep nginx