Linux test if string exists in file

In this article let’s discuss about Linux test if string exists in file. Let’s go through the following methods without any delay.

Method 1:

if grep -Fxq "string" file.txt; then 
	echo "Match"
else 
	echo "No match"
fi

If you are facing any issues with the above approach then try the alternative that is given below. It may look similar but have a try!

Method 2 :

Just use grep with flags 'F' (fixed string), 'x' (exact match) and 'q'
(quiet output) in order to check if a word string is in a file
if ! grep -Fxq "string" file.txt; then #do some code...#; fi

Hope the above solution works.

Realted Posts : Git stash apply specific index