grep How To Find A Text String In Files On Linux And Mac

Something you want to search a lot of files for a specific text. To quickly find any text string within any text file open up a Terminal window and use the grep command.

Wiki: “grep is a command-line utility for searching plain-text data sets for lines matching a regular expression. Grep was originally developed for the Unix operating system, but is available today for all Unix-likesystems. Its name comes from the ed command g/re/p (global / regular expression / print).”

In most cases you’ll use grep with attribute l and r. That is L and R in lowercase.

grep -lr

 [files to look in]

“l” option: only print File names containing matches

“r” option: recursive on the directories

Change Directory in the terminal to the folder were you’re going to do the search and type:

grep -lr apple *.txt

For example, this will list the name of any file in the current directory that ends with .txt and contains the string apple. To get more options for grep type

grep --help

You’ll get the grep manual with output and context control options for GNU grep 2.11 information or take a look below:

Posted in Linux, Mac