Linux Basics

30 / 87
Use grep command
   

If you want to locate a word in files, you can use the grep command. Grep lists all the lines from files in which a particular word exists. Examples of grep

grep myword file1 file2

If you want to search in files recursively - inside every subdirectory of a directory, use the following command

grep -r myword directory

If you want to search case insensitive, use "-i" switch

grep -i myword file1 file2

Practical Use Cases

grep is a very powerful tool. It can somewhat behave like where clause in sql queries.

Few Examples are:

  1. On a webserver you could filter only the errors from a log file
  2. Say you have a directory containing the temperature of various cities and you are looking for temperatures of the city having name as nyc, you could easily do: grep nyx tempdirectory/*