- How to perform grep operation on all files in a directory?
- 5 Answers 5
- Find text in files using the Linux grep command
- Training & certification
- Find text in a file
- Extend grep with regular expressions
- How to Find Text in Files in Linux
- Finding text in files
- Find text in files using grep
- Basic search
- Regular expression
- Extended regular expression
- Finding text in files
- Grep GUI
- Find text in files using nano
- Find text in files using Vim
- Find text in files using GNOME Text Editor
- Find text in files using VS Code
- Final thoughts
- About the author
- Sidratul Muntaha
- How to find all files containing specific text in Linux
- Conclusion
How to perform grep operation on all files in a directory?
Working with xenserver, and I want to perform a command on each file that is in a directory, grepping some stuff out of the output of the command and appending it in a file. I’m clear on the command I want to use and how to grep out string(s) as needed. But what I’m not clear on is how do I have it perform this command on each file, going to the next, until no more files are found.
5 Answers 5
In Linux, I normally use this command to recursively grep for a particular text within a directory:
- r = recursive i.e, search subdirectories within the current directory
- n = to print the line numbers to stdout
- i = case insensitive search
grep $PATTERN * would be sufficient. By default, grep would skip all subdirectories. However, if you want to grep through them, grep -r $PATTERN * is the case.
@Tomáš Zato, just supply all your file patterns instead of *: grep $PATTERN *.cpp *.h . If you need more specific rules for what files should be grepped, use find command (check Rob’s answer).
@Chris it’s possible you don’t have *.scss files in current directory but somewhere deeper in subdirs so grep does not look in all the files you wanted. You should use —include option to tell grep to look recursively for files that matches specific patterns: grep -r x —include ‘*.scss’ . (note the quotes, they prevent the pattern from being expanded by the shell). Or just use find (see Rob’s answer).
You want grep -s so you don’t get a warning for each subdirectory that grep skips. You should probably double-quote «$PATTERN» here.
Find text in files using the Linux grep command
Using grep, you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin.
Searching for patterns of text in files or text streams is one of the most common tasks you’ll perform in your sysadmin career. This is a valuable skill that allows you to check a variety of system configurations, analyze data, troubleshoot logs, and perform many other activities.
Training & certification
The most common way to find text in a Linux system is using the command-line utility grep . This utility was originally developed for the Unix operating system in the early 1970s. Grep evolved over the years, and the most common version available today for Linux, GNU grep, has additional features such as colored output. However, its main functionality is still the same.
Using grep , you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin using the shell pipe operator.
This article covers how to use the grep command to find text.
Find text in a file
The most basic way to use grep is searching for text in a single file. To do this, type grep followed by the text pattern to search for and the file name to search in. For example, to find which port the Secure Shell (SSH) daemon uses, search for Port in file /etc/ssh/sshd_config :
$ grep Port /etc/ssh/sshd_config Port 22 #GatewayPorts no
Notice that grep finds all lines that match the text pattern regardless of where the pattern is located.
Extend grep with regular expressions
In the previous example, when you searched for Port in the SSH configuration file, grep returned two lines. The line you were looking for, Port 22, and an additional line containing the search pattern. In some cases, that’s exactly what you want. In other cases, grep could find too many entries that you’re not interested in, requiring you to sort through them to find the desired information.
To avoid that, you can use regular expressions to be more specific about what you’re looking for. For example, to find only lines that start with the word Port , you can use the regular expression operator ^ , like this:
$ grep ^Port /etc/ssh/sshd_config Port 22
How to Find Text in Files in Linux
For a system administrator, working with text files is a common phenomenon. Maybe need to find a specific section from piles of log files for troubleshooting something? Or, need to find the document that contains essential information quickly?
In the case of Linux, there are numerous methods to find texts in files. It’s possible using both built-in tools and 3rd-party apps. Check out how to find texts in files in Linux.
Finding text in files
Depending on the number of files you have to perform a search on, there are two ways to perform the text search: automated or manual. If you have to work with a couple of text files, the manual search is more suitable. However, if there are hundreds of text files, then the automated search is the most efficient.
For automated search, we’ll be using grep. Grep comes pre-installed on any Linux distro. As for manual search, any modern text editor will do the job.
Find text in files using grep
In Linux, grep is the default tool for searching texts. Its name is derived from the ed command g/re/p that stands for “globally search for a regular expression and print matching lines.” It’s available on any modern Linux distro.
Grep is a command-line tool. Its command structure is as follows.
As the name of grep suggests, the pattern to search for is described using a regular expression. The regular expression is a special type of string that describes a pattern to match, locate, and manage. To learn more about grep and regular expression, check out using grep and egrep with regular expression.
For demonstration purposes, grab a sample text file. In this example, download the GNU General Public License v3.0 text file.
Basic search
The fundamental way of using grep is to search for a basic string.
Take a look at the following grep command. It’ll search for the word “GNU” in the text file.
To show the line number, use the “-n” flag.
To perform a case-insensitive search using grep, use the “-i” flag.
You may not want to see the search matches but only the file name where the match happened in some situations. To print only the filename, use the “-l” flag. Here, the asterisk denotes to use all the text files in the current directory.
We can also pipe the output of other commands to grep.
Regular expression
Regex offers a smart way of fine-tuning the search. It has its own rules. However, different applications and programming languages implement regular expression differently. Here are a couple of examples that you can use with grep.
To define that the string is to be found at starting a line, use the caret (^) symbol.
To define that the string is to be found at the end of a line, use the dollar sign ($).
To describe that there can be any character at a certain location of the pattern, use the period character (.). For example, the expression “G.U” is valid if there’s any character between “G” and “U”.
To describe that there can be a subset of characters at a particular location of the pattern, use the brackets ([]). For example, the expression “t[wo]o” tells that the match is valid for “two” and “too” only.
Extended regular expression
As the name suggests, an extended regular expression can do more complex things than basic regular expressions. To use extended regular expression with grep, you have to use the “-E” flag.
To search for two different strings, use the OR operators (|).
Finding text in files
Now comes the main part. Instead of manually telling grep the file to perform the search on, grep can do it automatically. In the following command, grep will use all the available text files in the current directory for searching the pattern.
If you want to grep to perform the search on a different directory, then you have to specify the location.
If there are folders, grep doesn’t explore them by default. To tell grep to search recursively, use the “-R” flag.
Grep GUI
If you prefer to work with GUI but still want to enjoy grep’s features, then check out searchmonkey. It’s a front-end solution for grep. The package is available on almost all the major Linux distros.
Find text in files using nano
GNU Nano is a simple and powerful text editor that comes with any Linux distro. It has built-in features to search for text in a text file.
Note that in this method, you have to open the text file, and search manually. It’s doable if there’s only a handful of text files to work with. If there’s more, then using grep is the most optimal choice.
Open the text file in nano.
To search for a string match, press “Ctrl + W”. After typing the string to search for, press “Enter”.
Find text in files using Vim
Vim is a well-known and reputed text editor. It’s the command-line equivalent of a modern text editor. Vim comes with numerous advanced features like plugins, macros, auto-completion, filters, etc.
Similar to nano, Vim works with a single file at a time. If you have multiple text files, then using grep is the most optimal way to go.
To search in a text file, first, open it in Vim.
Enter the following Vim command and hit “Enter”.
Find text in files using GNOME Text Editor
The GNOME Text Editor is the text editor that comes with the GNOME desktop. It’s a simplistic text editor with all the basic features you’d expect. It’s a nice alternative to the command-line text editors.
Similar to nano and vim, the same caution applies to this method. If the number of text files is large, then you better stick with grep.
Open the text file in Text Editor. Press “Ctrl + F” to bring up the search bar.
Find text in files using VS Code
Visual Studio Code is a powerful text editor with tons of features. It’s optimized for programmers to be used as if it’s a full-fledged IDE. It’s available on almost all the major Linux distros.
Install the Visual Studio Code snap package.
Open the text file in VS Code. Press “Ctrl + F” to start searching.
Final thoughts
There are numerous ways to search text in files. It’s an easy task to master. It’s strongly recommended to master the grep command because it offers the most value in terms of efficiency and ease-of-use.
If you prefer GUI, then there are numerous text editors to choose from. Any modern text editor will provide the text search option.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
How to find all files containing specific text in Linux
This tutorial will teach you how to recursively search for files containing a specific string on Linux using the command line. This tutorial uses the ‘grep’ command to search for strings in files. Alternatively, you may use the find command to look for files with specific content.
A basic syntax for searching text with grep command:
The grep command offers other useful options for finding specific text in file systems.
- -r, —recursive : Search files recursively
- -R, —dereference-recursive : Search files recursively and follow symlinks
- —include=FILE_PATTERN : search only files that match FILE_PATTERN
- —exclude=FILE_PATTERN : skip files and directories matching FILE_PATTERN
- —exclude-from=FILE : skip files matching any file pattern from FILE
- —exclude-dir=PATTERN : directories that match PATTERN will be skipped.
- -L, —files-without-match : Print file names containing no match
- -l, —files-with-matches : Print string containing file names only
- -i, —ignore-case : ignore case of search string
- -e, —regexp=PATTERN : Use a pattern to search or specify multiple search strings
- -w, —word-regexp : force to match whole words
There are several ways to use the grep command to search text. Let’s discuss a few examples of searching a text/string in the file system.
The below example command will search the string “Error” in all files in /var/log directory and its sub-directories.
The -e switch can also be utilized to find multiple strings. This is comparable to the egrep program. The example below will look for “Error” and “Warning” in all the files in the /var/log directory and its subdirectories.
grep -rlw -e "Error" -e "Warning" /var/log
You can search strings in files that match the file name criteria. The following command searches for “Error” in files with the .log extension in the /var/log directory and its sub-directories.
grep -rlw --include="*.log" -e "Error" /var/log
You can use the —exclude option in find to exclude some files that match certain file name criteria. For example, you can exclude files with the .txt extension.
grep -rlw --exclude="*.txt" -e "tecadmin" /var/log
You can also skip searching certain directories. For instance, don’t search for string files in any folder with apache2 in its name.
grep -rlw --exclude-dir="*apache2*" -e "tecadmin" /var/log
Conclusion
You have learned how to search for specific text in files on the Linux file system in this tutorial.