Skip to content

Linux: Search inside files

You ever wanted to find something within loads of files? And you’d like the matching lines to be printed? And with line-numbers? What if you’d like the X lines above/below matching lines to be printed? No problem.

Find, and print, all lines (with filename) that matches ?ERROR?;

find /path/to/folder -type f -exec grep "ERROR" /dev/null '{}' \;
(you can replace ?/dev/null? with -H in most distros)

Find, and print, all lines (with filename) that matches ?ERROR?, but only in files starting with ?2010-04?;

find /path/to/folder -type f -name "2010-04*" -exec grep "ERROR" /dev/null '{}' \;

Find, and print, all lines (with filename) that matches ?ERROR?, but only in files starting with ?2010-04?. In addition, 10 lines above the matches should be printed;

find /path/to/folder -type f -name "2010-04*" -exec grep -B 10 "ERROR" /dev/null '{}' \;

Find, and print, all lines (with filename) that matches ?ERROR?, but only in files starting with ?2010-04?. In addition, 10 lines below the matches should be printed;

find /path/to/folder -type f -name "2010-04*" -exec grep -A 10 "ERROR" /dev/null '{}' \;

Find, and print, all lines (with filename) that matches ?ERROR?, but only in files starting with ?2010-04?. In addition, line-numbers for all matches, should be printed;

find /path/to/folder -type f -name "2010-04*" -exec grep -n "ERROR" /dev/null '{}' \;
Leave a Reply

Your email address will not be published. Required fields are marked *