How to search text in files?

You can search for text in files using several command-line tools. Here are some common methods:

  1. Using grep:

    grep "search_term" filename.txt
    

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

  2. Using grep with options:

    • Case-insensitive search:
      grep -i "search_term" filename.txt
      
    • Show line numbers:
      grep -n "search_term" filename.txt
      
    • Search recursively in directories:
      grep -r "search_term" /path/to/directory
      
  3. Using find with grep: To search for a term in multiple files:

    find /path/to/directory -type f -exec grep "search_term" {} +
    
  4. Using nano (text editor):

    • Open the file:
      nano filename.txt
      
    • Press Ctrl + W, type the search term, and press Enter.
  5. Using less:

    • Open the file:
      less filename.txt
      
    • Press /, type the search term, and press Enter.

Choose the method that best fits your needs!

0 Comments

no data
Be the first to share your comment!