grep
The command that prints the lines of its input that match a pattern.
In more detail
The pattern is a regular expression, so quote it and remember that a dot matches any character; -F searches for fixed text instead. -i ignores case, -v prints non-matching lines, -c counts, -n adds line numbers, -o prints only the match, -r searches a directory tree and -E switches to extended syntax. grep exits 0 when something matched, 1 when nothing did and 2 on an error, which makes it a natural test in if.
Examples
grep -rn -e found notesgrep -E ' [45][0-9]{2} ' access.loggrep -Eq "^(feat|fix): .+"git log --grep searches commit messages the same way.curl -s http://localhost:8002/metrics | grep -v duration_secondsgrep -rlF searches a whole directory tree for a fixed string and lists the files that contain it, which is a quick sweep for a dropped file path.Related terms
- regular expression — A pattern language for matching text, where characters such as ., *, ^ and $ have special meanings.
- extended regular expression — The two regular expression dialects of Unix tools: basic (BRE), used by grep and sed by default, and extended (ERE), selected with -E.
- exit code — A number a process returns when it finishes: 0 means success, anything else means something went wrong.
- cut command — The cut command, which extracts columns from each line, either by character position or by fields split on a delimiter.
- Semgrep — An open-source static analysis tool whose rules look like the code they match, with metavariables and ellipses for the parts that vary.
- awk — A small programming language for text that splits each line into fields and runs actions on the lines that match a condition.
- sed — The stream editor, which applies editing commands such as find-and-replace to each line of text passing through it.
Taught in
- What A Shell Is, And What Bash Is — Bash Scripting & Command Line Automation
- Help, History, Completion And Aliases — Bash Scripting & Command Line Automation
- The Unquoted Variable: Splitting And Globbing — Bash Scripting & Command Line Automation
- Standard Input, Output And Error — Bash Scripting & Command Line Automation
- Redirection: Truncate, Append, Both Streams — Bash Scripting & Command Line Automation
- Pipelines, tee And Process Substitution — Bash Scripting & Command Line Automation
- Finding Lines: grep And Regular Expressions — Bash Scripting & Command Line Automation
- Command Substitution And Subshells — Bash Scripting & Command Line Automation
- Exit Codes, And What Success Means — Bash Scripting & Command Line Automation
- Conditionals: test, Brackets, Double Brackets, case — Bash Scripting & Command Line Automation
- set -e, set -u And pipefail: Promises And Traps — Bash Scripting & Command Line Automation
- trap, Cleanup And Temporary Files — Bash Scripting & Command Line Automation
- Debugging: set -x, bash -n And Reading The Error — Bash Scripting & Command Line Automation
- eval, And What To Use Instead — Bash Scripting & Command Line Automation
- find And xargs — Bash Scripting & Command Line Automation
- Scheduling, Monitoring And A Script That Reports — Bash Scripting & Command Line Automation
- Workflows, Jobs, Steps, and Actions — Continuous Integration & Continuous Delivery Pipelines
- When A Control Plane Pod Breaks — Certified Kubernetes Administrator (CKA) Exam Prep
- What A Container Said Before It Died — Certified Kubernetes Administrator (CKA) Exam Prep
- A Service With Nobody Behind It — Certified Kubernetes Administrator (CKA) Exam Prep
- Preparing A Node Before kubeadm Will Touch It — Certified Kubernetes Administrator (CKA) Exam Prep
- kubeadm init And join: What Lands Where — Certified Kubernetes Administrator (CKA) Exam Prep
- etcd: Snapshot, Restore, And The Cache That Lies — Certified Kubernetes Administrator (CKA) Exam Prep
- Helm And Kustomize For Cluster Components — Certified Kubernetes Administrator (CKA) Exam Prep
- Which IP Answers: Pods, Services And NodePorts — Certified Kubernetes Administrator (CKA) Exam Prep
- Running, And Still Down — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Choose The Right Workload Resource — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Kustomize Bases, Overlays And Patches — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Deployment: Timed Drill And Checkpoint — Certified Kubernetes Application Developer (CKAD) Exam Prep
- API Deprecations And Migrations — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Probes And Health Checks — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Container Logs — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Debugging Applications In Kubernetes — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Observability: Timed Drill And Checkpoint — Certified Kubernetes Application Developer (CKAD) Exam Prep
- ConfigMaps And Secrets — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Requests, Limits And Quotas — Certified Kubernetes Application Developer (CKAD) Exam Prep
- ServiceAccounts And Tokens — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Services And Empty Endpoints — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Services And Networking: Timed Drill And Checkpoint — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Mock Exam: Sixteen Tasks Against The Clock — Certified Kubernetes Application Developer (CKAD) Exam Prep
- Final Review: Where Your Marks Are At Risk — Certified Kubernetes Application Developer (CKAD) Exam Prep
- kube-bench: Ten Failures On A Fresh Cluster — Certified Kubernetes Security Specialist (CKS) Exam Prep
- Network Exposure: NodePorts, Host Firewalls And Egress — Certified Kubernetes Security Specialist (CKS) Exam Prep
- Encrypting Pod-to-Pod Traffic: mTLS With Istio And Cilium — Certified Kubernetes Security Specialist (CKS) Exam Prep
- Pre-Commit Secret Scanning with Gitleaks & Entropy — Cloud Security & DevSecOps Engineering
- Static Application Security Testing with Semgrep — Cloud Security & DevSecOps Engineering
- Securing the Kubernetes Control Plane & Node Components — Cloud Security & DevSecOps Engineering
- Works On A Laptop, Fails In Production — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- The Alert That Came An Hour Late — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- The Certificate That Expired On A Sunday — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Forty Minutes To Start — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Security Baselines, Policies & Hardening Standards — Cybersecurity Fundamentals, GRC & Cryptography
- Conventional Commits — Advanced Git Internals & Distributed Workflows
- Reading History: log, show And diff — Advanced Git Internals & Distributed Workflows
- Observability Versus Monitoring — Full-Stack Observability: Metrics, Tracing & Logging
- The Three Signals: Logs, Metrics And Traces — Full-Stack Observability: Metrics, Tracing & Logging
- The Prometheus Exposition Format — Full-Stack Observability: Metrics, Tracing & Logging
- A Database You Cannot Recompile: Exporters — Prometheus Certified Associate (PCA) Exam Prep
- Exam Strategy: Order, Verify, And The Docs You May Use — Red Hat Certified System Administrator (EX200) Exam Prep
- Exam-Style Scripts: Arguments, Output And Exit Codes — Red Hat Certified System Administrator (EX200) Exam Prep
- Targets, Boot And Shutdown — Red Hat Certified System Administrator (EX200) Exam Prep
- Recovering Root And Changing The Boot Loader — Red Hat Certified System Administrator (EX200) Exam Prep
- Tuning Profiles With tuned — Red Hat Certified System Administrator (EX200) Exam Prep
- Logs, The Journal, And Keeping It Across Reboots — Red Hat Certified System Administrator (EX200) Exam Prep
- Scheduling: cron, at And systemd Timers — Red Hat Certified System Administrator (EX200) Exam Prep
- Time Clients With chrony — Red Hat Certified System Administrator (EX200) Exam Prep
- Detecting Persistence: Tasks, Registry & Services — Security Operations & Threat Hunting (SOC)
- Containment & Eradication: Host, Net & Identity — Security Operations & Threat Hunting (SOC)
- The Plan That Wanted To Destroy Production — HashiCorp Terraform Associate (004) Exam Prep
- Infrastructure As Code, And One Workflow For Every Platform — HashiCorp Terraform Associate (004) Exam Prep
- Providers: How Terraform Reaches Every API — HashiCorp Terraform Associate (004) Exam Prep
- Versions, The Lock File And Provider Aliases — HashiCorp Terraform Associate (004) Exam Prep
- State: Terraform's Memory Of What It Built — HashiCorp Terraform Associate (004) Exam Prep
- Write, Plan, Apply, And What Init Prepares — HashiCorp Terraform Associate (004) Exam Prep
- Reading A Plan, Saving It, Applying It — HashiCorp Terraform Associate (004) Exam Prep
- Validate, Format, Destroy, And The Flags That Bend A Plan — HashiCorp Terraform Associate (004) Exam Prep
- The Pager Goes Off At 2 A.M.: A Three-Question Triage — CompTIA Linux+ (XK0-006) Exam Prep
- The App In /bin That Vanished: The FHS, Distributions And Licences — CompTIA Linux+ (XK0-006) Exam Prep
- Friday's Backup, Monday's Restore: Incrementals, Images And Compressed Logs — CompTIA Linux+ (XK0-006) Exam Prep
- Who Wrote That Sudoers File? The Journal, rsyslog, logrotate And auditd — CompTIA Linux+ (XK0-006) Exam Prep
- Root Over SSH From Anywhere: Keys, sshd, setuid Files And Attributes — CompTIA Linux+ (XK0-006) Exam Prep
- The Assistant Was Confident: Reviewing AI-Generated Code, Prompts And Data — CompTIA Linux+ (XK0-006) Exam Prep
- You Are In Emergency Mode: Boot Failures From GRUB To fstab, On Both Families — CompTIA Linux+ (XK0-006) Exam Prep
- Kernel Parameters: Now, At Boot, And Which File Wins — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Services That Will Not Come Back: Masks, Targets And The Journal — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Packages And Repositories: Find, Install, Hold, Purge — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Virtual Machines With libvirt: XML, virsh And Autostart — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- SELinux: Modes, Labels, Ports And Booleans That Last — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Addresses, Gateways And Names On Both Stacks — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Time Sync With chrony: Servers, Zones And Proof — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Network Troubleshooting: The Ladder From Link To Port — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- OpenSSH: Keys, Client Aliases And A Hardened Server — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Static Routes: Prefixes, Metrics And Persistence — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Bridges And Bonds: Two Links, One Interface — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Reverse Proxies And Load Balancers — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- The Virtual File System: /proc, /sys, Binds And tmpfs — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Remote File Systems: NFS, SMB And Network Block Devices — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Swap: Partitions, Volumes, Files And Priorities — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Git For Admins: Who Changed That, And How To Undo It — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Constraints: Limits, Control Groups And Listeners — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- SSL Certificates: Create, Inspect And Verify A Chain — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- User Limits: Who Gets Which Number At Login — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- LDAP Users Through SSSD — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Drill: From Emergency Mode To A Clean Boot — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Drill: A Gateway That Survives The Reboot — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Drill: Volumes, Swap, A Share And A Team Directory — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Mock Exam, Part 1: Seventeen Tasks, One Reboot — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Mock Exam, Part 2: Real Machines And The Costly Mistakes — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Runlevels, Targets And A Polite Shutdown — LPIC-1 Linux Administrator (101-500 and 102-500) Exam Prep
- Exam Strategy: The First Fifteen Minutes And ansible-doc — RHCE in Ansible (EX294): Automate Red Hat Systems
- ansible.cfg: Which File Wins, And The Settings That Matter — RHCE in Ansible (EX294): Automate Red Hat Systems
- Finding Modules In Collections: ansible-doc And navigator — RHCE in Ansible (EX294): Automate Red Hat Systems
- SELinux: Labels, Ports And Booleans That Persist — RHCE in Ansible (EX294): Automate Red Hat Systems
- Drill: Storage Only Where The Disk Exists — RHCE in Ansible (EX294): Automate Red Hat Systems
- Mock Exam, Part 1: Seven Tasks Across Three Sections — RHCE in Ansible (EX294): Automate Red Hat Systems
References
Certification courses
- CKA course and practice exam: Certified Kubernetes Administrator
- CKAD course and practice exam: Certified Kubernetes Application Developer
- CKS course and practice exam: Certified Kubernetes Security Specialist
- CCDAK and CCAAK course and practice exams: Confluent Apache Kafka certifications
- PCA course and practice exam: Prometheus Certified Associate
- RHCSA course and practice exam: Red Hat Certified System Administrator (EX200)
- Terraform Associate course and practice exam: HashiCorp Terraform Associate (004)
- CompTIA Linux+ course and practice exam: XK0-006
- LFCS course and practice exam: Linux Foundation Certified System Administrator
- LPIC-1 course and practice exam: Linux Administrator (101-500 and 102-500)
- RHCE course and practice exam: Red Hat Certified Engineer in Ansible (EX294)