grep command w/ examples in linux

it Grape The Commands Guide is a follow-up to my previous 90 Linux Commands Frequently Used by Linux Sysadmins article. Every week, or as time permits, I will publish articles on about 90 commands geared towards Linux sysadmins and Linux power users. let’s continue this series grep Permission.

Grape (Yeslowl RRegular Iis expression Printer) searches through the file for a specific pattern of characters. When it finds a match in a line, grep then copies the line to standard output or whatever output you chose using the options. grep Originally developed for the Unix operating system, but eventually made available for all Unix-like systems, such as Linux.

general syntax of grep The order is:

grep [option...] [patterns] [file...]

Linux grep command example


… grep man page.

To find a pattern in a file, use:

grep "pattern" /path/to/file

To find all files in the current directory, use:

grep pattern *

To find the exact pattern, use:

grep -F "pattern" /path/to/file

To search directories recursively, use:

grep -r 'hello' /path/to/dir

To search for a whole word, not part of a word, use:

grep -w 'word' /path/to/file

To perform a case insensitive (ignore case) search, use:

grep -i 'pattern' filename

grep Originally developed for the Unix operating system, but eventually made available for all Unix-like systems, such as Linux. Here are some more example commands.

To display filenames that contain the pattern, use:

grep -l 'pattern' /var/log/*

To display x lines after a matching pattern, use:

grep -A x 'pattern' filename

To display x lines before a matching pattern, use:

grep -B x 'pattern' filename

To display x lines around a matching pattern, use:

grep -C x 'pattern' filename

To display the matching part of a pattern, use:

grep -o 'pattern' filename

To display the line number of a matching pattern, use:

grep -n 'pattern' filename

To return all rows that do not match the pattern, use:

grep -v 'warning' /var/log/nginx/error.log

To display lines starting with ‘er’, use:

grep -e '^er' filename

To display 3 w’s of lines in a row (www), use:

grep -E 'w{3}' filename

Related orders:

  • find – Recursively search for files or directories under the given directory tree.
  • PGREP – Searches for running processes and lists the process IDs that match the selection criteria to stdout.
  • NGREP — grep is applied at the network layer.
  • To find out – Search files in Linux.

Useful links/references:

conclusion

grep Helps find patterns within files or file system hierarchy, so it’s worth spending time getting familiar with its options and syntax.

Leave a Comment