How to search for a pattern?

QuestionsQuestions8 SkillsProUser Account ManagementNov, 06 2025
066

To search for a pattern in a file on a Linux system, you can use the grep command. Here’s how to do it:

Basic Syntax:

grep 'pattern' filename

Example:

  1. Search for a specific word in a file:

    grep 'error' log.txt

    This command searches for the word "error" in the log.txt file and displays all lines containing that word.

Common Options:

  • -i: Ignore case (e.g., grep -i 'error' log.txt).
  • -w: Match whole words only (e.g., grep -w 'error' log.txt).
  • -r: Recursively search through directories (e.g., grep -r 'error' /var/log/).
  • -n: Show line numbers with output (e.g., grep -n 'error' log.txt).

Using less for Searching:

If you are viewing a file with less, you can search by:

  1. Pressing / followed by the pattern and hitting Enter.
  2. Pressing n to go to the next occurrence or N to go to the previous occurrence.

Feel free to ask if you need more details or examples!

0 Comments

no data
Be the first to share your comment!