How to Search for Text in Files Using Linux

Introduction
There may be times when the name of a required file is unknown but you may remember some of the content of a file. A search can be performed in a Linux terminal that will display a list of files that contains the text you remember.

The Command
The command that will be used is “find” with “xargs” and “grep”. A few options are supplied to the command to only print out file names and also not search in hidden directories. This will result in a neat readable output.

find <directory to search> -type f | xargs grep -rl '<text to search for>'

 
Example
If I wanted to search a hypothetical backup directory (/ftp/backup) for the text “mpshouse”:

find /ftp/backup/ -type f | xargs grep -rl 'mpshouse'

There is more than one way of searching for text in files, this is just one.

Tags: , , , , , , , ,

5 Responses to "How to Search for Text in Files Using Linux"

  • dj says:
  • Geoff says:
  • Arun Maurya says:
  • bill says:
Leave a Comment