- Home
- Computers and Devices
- Linux: Find Files Containing Text
Linux: Find Files Containing Text
- By Selwin Verallo
- Published 09/5/2007
- Computers and Devices
-
Rating:




Where did that file go?
Humans are forgetful in nature, and for that reason oftentimes we lose track of important documents that we store on our computers. Sometimes we get lucky and remember the path or directory in which we have stored that elusive file (though if you're like me, the odds of this happening are slim to none.) So what do you do when you need a file, but just can not remember what you have named it?
If you are a WindowsTM user the task is simple: use the Search tool accessible via the Start menu. If you can remember any part of the file name then you will use the "find files with name" option and make use of the wild-card character "*". For example, if you can only remember that your file had the work "statistic" in the name, then you would search for: "*statistic*", which will return a list of all files on your machine with the text "statistic" in the name. Though most of the time we wont be so lucky as to remember (even remotely) what we have named our file; so what do you do in this case? Simple, if you can remember any word a phrase in your file, you can use the "find files containing text" option which will look at the contents of a file, not just of the filename.
If you are a Linux user, the task may seemingly be a bit more complex or overwhelming in comparison to the WindowsTM method (especially if you are a standard Linux user, who is dependent on a GUI interface such as GNOME or KDE.) No matter your skill level with Linux, by following the upcoming instru ctions you will be able to efficiently track down that slippery file.
For locating files that contain a certain word or phrase, my weapon of choice is grep, a command line utility originally written for the Unix operating system which, when given a list of files to read, searches for lines of text that match one or many regular expressions and then outputs special information about the files that matched your regular expression. Standard grep syntax is: "grep [OPTION]... PATTERN [FILE] ...", or used in an example:
The -l switch tells grep to output the name of the file whos content matches your regular expression, -i tell grep to ignore the case in which the content is in (so if you search for "hello" and the text "HeLLO" is found, -i says, yes this is a match) and -r tells grep to do a recursive search into all subdirectories. another switche that may be helpful to when using grep is:
For a more extensive list of grep switches you can type: "grep --help" into the console.
If you are a WindowsTM user the task is simple: use the Search tool accessible via the Start menu. If you can remember any part of the file name then you will use the "find files with name" option and make use of the wild-card character "*". For example, if you can only remember that your file had the work "statistic" in the name, then you would search for: "*statistic*", which will return a list of all files on your machine with the text "statistic" in the name. Though most of the time we wont be so lucky as to remember (even remotely) what we have named our file; so what do you do in this case? Simple, if you can remember any word a phrase in your file, you can use the "find files containing text" option which will look at the contents of a file, not just of the filename.
If you are a Linux user, the task may seemingly be a bit more complex or overwhelming in comparison to the WindowsTM method (especially if you are a standard Linux user, who is dependent on a GUI interface such as GNOME or KDE.) No matter your skill level with Linux, by following the upcoming instru ctions you will be able to efficiently track down that slippery file.
For locating files that contain a certain word or phrase, my weapon of choice is grep, a command line utility originally written for the Unix operating system which, when given a list of files to read, searches for lines of text that match one or many regular expressions and then outputs special information about the files that matched your regular expression. Standard grep syntax is: "grep [OPTION]... PATTERN [FILE] ...", or used in an example:
grep -lir 'text to find' *
The -l switch tells grep to output the name of the file whos content matches your regular expression, -i tell grep to ignore the case in which the content is in (so if you search for "hello" and the text "HeLLO" is found, -i says, yes this is a match) and -r tells grep to do a recursive search into all subdirectories. another switche that may be helpful to when using grep is:
-c, --count only print a count of matching lines per FILE
For a more extensive list of grep switches you can type: "grep --help" into the console.
