On a Linux system, the need to search one or multiple files for a specific text string can arise quite often. On the command line, the grep command has this function covered very well, but you’ll need to know the basics of how to use it. On GUI, most text editors also have the ability to search for a particular string.
In this article, we’ll show you how to quickly and easily search through files for a specified text string. You’ll learn how to use the
grep command to perform this task, as well as how to access the search function in command line and GUI text editors.
In this tutorial you will learn:
- How to search files for a text string via command line
- How to search files for a text string via command line text editors
- How to search a file for a text string via GUI text editors
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Search files for a text string via command line
To search a file (or files) for a particular text string, the only command you should need to know is
grep. And it’s easy enough to master. Let’s get started by working with the most simple forms of the command and getting gradually more complex.
To search a file for a text string, use the following command syntax:
$ grep string filename
For example, let’s search our
document.txt text document for the string “example.”
$ grep example document.txt
As you can see from the screenshot,
grep returns the entire line that contains the word “example.” If the string occurs on multiple lines, then all of those lines will be returned as well.
If you want to search multiple files, just append as many to the command as you’d like.
$ grep example document1.txt document2.txt
You can also use wildcards in the commmand.
$ grep example document*
The output from
grep shows us which files the string was found in.
To search recursively, use the
-r option with
$ grep -r example
As you can see,
grep searched multiple directories and indicates where it found the string. You could also specify a directory in your command, but omitting it (as we did in this example) will instruct
grep to search every directory in the current path.
Here are some other handy
grep command options you may need when searching for a text string:
-c– Count the number of times a string occurs
-i– Ignore case.
-n– Show line number where string was found.
-o– Only show matching text (don’t return the whole line).
There’s still a lot more you can do with
grep. Check out our guide on grep for more in-depth information, or check the man page for more command line options.
$ man grep
Search for a text string in nano
If you have a file opened in nano and need to find a particular string, there’s no need to exit the file and use
grep on it. Just press
Ctrl + W on your keyboard, type the search string, and hit
Search for a text string in vim
The vim text editor also has a built-in method for searching for a string. All you need to do is type
:/ followed by your search string, then press
Search a file for a text string via GUI
There are tons of GUI text editors available for Linux. Different distros and desktop environments have their own software, so it’s impossible to write a guide that will cover them all. However, they all generally work quite similarly, so we’ll still be able to help you out.
On our Ubuntu test machine, we’re running GNOME and have access to the text editor simply known as “text editor.” We can access its search function from the menu, as seen in the screenshot below.
Searching files on Linux for a text string is a common task and it’s one that’s easy to master. The
grep command proves very wieldy and can fetch results lightning quick. If you’ve already opened a file in a command line or GUI editor, there are search functions available there as well. In this guide, you learned everything you’ll need for finding a text string in one or more files on Linux.