Search for text in a file within a specific directory in Linux

Sometimes, you need to find something quickly and don’t want to open tens or hundreds of file to find it. Luckily, Linux is a powerful operating system and can assist you in finding what you need. If you need to find something, let’s say you are looking for a specific piece of text within a specific folder, you can use this command to list the files where your defined string is found:

grep -H -R "keyword" /directory/ | cut -d: f1

To give you an example, I was looking for a CSS file that was pointing to a page that no longer existed. I was working on optimizing my WordPress installation, so I wanted to remove that file since it has no use any longer as it would only return a 404. So I checked the source code that was served to my browser, check the file and then performed a search in my WordPress wp-content/themes folder:

grep -H -R "" /home/thomas/domains/ | cut -d: f1

This returned a list of files that contained, which is a part of the URL of the CSS file that no longer existed.

Ultimately, the file I was looking for was found here:


I then opened that directory through FTP, edited the file, saved it and the broken link was gone. All in under 5 minutes!


Author Bio

Thank you for your interest in my blog! On this miniblog, I write mostly short (technical) blog posts that might interest other people. Read more about me or feel free to contact me.


Leave a Reply

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