Search code examples
linuxshellgrepline

Get line number while using grep


I am using grep recursive to search files for a string, and all the matched files and the lines containing that string are printed on the terminal. But is it possible to get the line numbers of those lines too?

Example: presently, I get /var/www/file.php: $options = "this.target", but I am trying to get /var/www/file.php: 1142 $options = "this.target";, well where 1142 would be the line number containing that string.

The syntax I am using to grep recursively is sudo grep -r 'pattern' '/var/www/file.php'

How do we get results for not equal to a pattern? Like all the files, but not the ones having a certain string.


Solution

  • grep -n SEARCHTERM file1 file2 ...