How to search within a file?

QuestionsQuestions8 SkillsProBasic Files OperationsOct, 07 2025
0104

You can search within a file using the grep command. Here’s how to do it:

  1. Basic Search:

    grep "search_term" filename.txt

    This command searches for the specified search_term in filename.txt and displays the matching lines.

  2. Case-Insensitive Search:

    grep -i "search_term" filename.txt

    This command performs a case-insensitive search.

  3. Search for Whole Words:

    grep -w "search_term" filename.txt

    This command searches for the whole word only.

  4. Display Line Numbers:

    grep -n "search_term" filename.txt

    This command shows the line numbers of the matching lines.

  5. Search Recursively in Directories:

    grep -r "search_term" /path/to/directory

    This command searches for the term in all files within the specified directory and its subdirectories.

You can combine these options as needed. For example:

grep -i -n "search_term" filename.txt

This command performs a case-insensitive search and displays line numbers.

0 Comments

no data
Be the first to share your comment!