Bash Command to Find String in a File

If you have a use-case where you want to find out if a string exists in a file or not using a bash shell, then you can make use of the grep command.


Syntax:

grep "the-string-to-find" /file/path/

Example:
% bash-3.2$ grep 'Code2care' /Users/c2ctechtv/Desktop/data.txt 

Code2care - Lines of code for a change!

If there is no match for the search, the command returns back to the bash shell.

Please note that the grep string searched using grep is case-sensitive by default. If you want to make it case insensitive make use of the -i flag along with it.

% bash-3.2$ grep 'welcome' /Users/c2ctechtv/Desktop/data.txt 

WELCOME
Bash find a string in a file using Grep Command

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!