Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to search for a string in a file, and the output should give me the line number and line containing that string.

How can I do it?

share|improve this question
up vote 6 down vote accepted

Use grep with the -n flag. Note that line numbers are 1-indexed.

share|improve this answer

You can use grep for this.

grep -n yourString yourFile
share|improve this answer
While some questions benefit from multiple perspectives or different proposed solutions, this answer seems to add very little not already covered by soandos' answer from several months prior. Is there some way you can edit your answer to differentiate it from the earlier answer? – Michael Kjörling Sep 1 '14 at 14:18

The general tool for searching in text files is grep. To run it as you need, just try

grep -n -F "my string" files

The -F tells grep to search for exact matches of the string. Otherwise, grep searches for regular expressions.

-n tells grep to return line numbers as well.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.