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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap