How to use grep in a terminal to find a string in files

“grep” is a command you can use in a terminal to find sequences of characters. It can search through entire folders and find matches. If you use the right parameters, it will display all the matches it finds.

Before simply typing the command, however, you should know it might need a long time to comb through entire folders! So don’t simply type the command in a large folder right away.

Example: don’t just type:

$ grep "philosphy"

in any folder! It will run for ages because you’ve typed a command that doesn’t mean anything. You’ll have the feeling the computer is hanging because nothing happens. If your grep is hanging, follow these steps.

So to search for “string” in all the files of a directory, the easiest is to:

  • move into that directory
  • type the following (with the quotation marks, spacing and period):
  • grep -r "string" .