How to find all files containing specific text in linux?

Recently, while working on a project, I needed to search if an image I intended to delete was in use anywhere else in the code. There were lot’s of text files to go through and I needed a quick way to tell whether or not it was safe to delete said image. I learned how to use grep, which is a commandline utility for searching plain text files. To find files that contain the text you’re looking for, do the following:

grep -rnw '/path/to/directory' -e "pattern"


r – recursive
n – show line number
w – only show whole word matches