Linux – Find files containing text

Because we are forgetful by nature, we often forget the files we have created. We can only be lucky if we can still remember the path, folder or directory where we have stored those missing files. If that’s the case, then it would never be a big problem. However, this is really a big problem and even painful if we can’t even remember where we have placed the missing files on our computer. “Simple”, I’m listening… “find it”. Yes we can find it. But wait, do you know the name of the file?

Forgetting files and forgetting file names are the most common in us. I will never believe that someone has a photographic memory and has never lost some of the files that they had created before. No doubt, we have all been through that terrifying experience, especially if the missing file is so precious to us.

On Windows, this problem can be easily resolved by simply using the search tool in the Start menu. Can you remember some texts or phrases in the file name? Use “find files with names” and unleash the power of the wildcard

. For example, if you can only remember the word “statistics” in the file name, search for “*statistics*” and that will search for files with the word “statistics” in the file name. “I can’t even remember a word in the file name”, again I can hear you say that. Well, I don’t think you can even remember a single word in the content of the file. If you can’t remember a single word or phrase in the file name, search for the contents of the file itself. In Windows, you can still search for files that contain some texts that you specify in the “search for files that contain text” input box. That will absolutely solve your problem of forgetting words in file name.

However, if you are on Linux, everything would be more different and complex than on Windows, especially if you are a normal user who depends on the GUI interface. Linux is more about running commands from a shell.

So if you are a normal user facing the “missing files” issue on Linux, don’t worry, I will show you the most common methods to solve this problem:

Find files containing a text string

grep -lir “text to find” *

The -l switch outputs only the names of the files in which the text appears (rather than each line containing the text), the -i switch ignores case, and the -r switch steps down to subdirectories.

Find files containing search terms in Ubuntu

To find files that contain keywords, Linux has a powerful command called grep, which you can use to find the lines within any file or a list of files.

grep -i -n ‘text to find’ *

List files containing text

It is used to recursively search a directory for files that contain a string, generate the file names and the line number. This will search for all the regular files in for.

grep –with-filename –line-number `find -type f`

Leave a Reply

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