Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to know a handy command to search for some strings in multiple plain text files and show the line number where it matchs in the output. ie:

grep -r 'hello' --include=*.js ¿?

output:

base.js  [4]
base.js  [13]
utils.js [27]
...

thanks!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Use -n as an option to grep. Or combine them in one with recursive mode, e.g.:

grep -rn pattern path/to/folder

As the manpage of grep says:

   -n, --line-number
          Prefix  each  line of output with the 1-based line number
          within its input file.  (-n is specified by POSIX.)

Example output:

temp/openssh/openssh-6.0p1/sshconnect2.c:1468:  sent = send_pubkey_test(authctxt, id);
                                         ^^^^
                                         ||||----- Line number
share|improve this answer
add comment

Your Answer

 
discard

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.