20 Useful GREP Commands

grep -V Show the version of Grep command like “grep (GNU grep) 2.14”
grep "search string" file_name Search for the given string.
grep "search string" file_name_* Search given string in multiple files.
grep -i "search string" file_name Ignore case search.
grep -w "This is a test" file_name Full world search.
grep -r "search string" folder_name Search in all files recursively including sub-folders.
grep -v "search string" file_name Print the lines that do not match the search string.
grep -c "search string" file_name Count the number of matches.
grep -vc "search string" file_name Count the number of matches that doesn’t matches.
grep -A 2 "search string" file_name Print 2 lines after the match of search string.
grep -B 2 "search string" file_name Print 2 lines before the match of search string.
grep -C 2 "search string" file_name Print 2 lines after and before the match of search string.
grep -l "search string" file_name Print only name of files that contain the matched search string.
grep -o "search string" file_name Show only the part of a line matching.
grep -b "search string" file_name Show the string position/offset.
grep -n "search string" file_name Show line number.
grep -H "search string" file_name Print file name for each matched search string.
grep -lhr "search string" folder_name List all the files that matched the search string.
grep -f file1 file2 Obtain string pattern from file1 that need to be searched in file2
grep -L "search string" folder_name List files that doesn’t include the search string.

Leave a Reply

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