Home » Questions » Computers [ Ask a new question ]

How do I find a file in Linux based on text I know it contains?

How do I find a file in Linux based on text I know it contains?

How can I locate all files in directory /home/user/example that contain "some text" in Linux. I am using Ubuntu 9.10.

Asked by: Guest | Views: 269
Total answers/comments: 2
bert [Entry]

"I usually use a combination of find and grep so it grabs all hidden files as well:

find /home/user/example -type f -exec grep -i ""some text"" {} +

The -i switch to grep will use a case-insensitive search method. It will be slower, but it is useful if you don't know the exact case of the words. You can remove the switch if you know what the proper case is to speed things up."
bert [Entry]

"If you're not worried about recursing into subdirectories,

grep -l ""some text"" *

Will only output the filenames, and not the context that the match appears in."