1 min read

How to search for a string in file under Linux cli

How to search for a string in file under Linux cli

This is a very useful technic. For example, you have 100 files in multiple folders, and need to find the one with the right string of text in it. How do we do this?

grep is the solution :)
Look at this, if you want to find a specific string in one file, use this command:

grep your_string filename


If you need to search in more then one file you can type this in:

grep your_string filename1 filename2


Now I know that sometime you will need to search in hundreds of file, to do so use a wildcard:

grep your_string filename*

OR, when file are in one folder:

grep your_string /home/*


If you want to find the specific file with the specific string, but the file are in many locations, use -r:

grep -r your_string path_to_location


Some useful parameters for grep:

  • -c : Count the number of times a string occurs
  • -i : Ignore case.
  • -n : Show line number where string was found.
  • -o : Only show matching text (don't return the whole line).

    For more info about grep, type in your console:
masn grep


That's it

Enjoy!