Search text in all files in linux

How to Perform Grep Search on All Files and in All Directories

The versatile grep command lets you perform search for a text in all the files and all the subdirectories of a directory. Here’s how to do that.

Grep is an excellent tool when you have to search on the content of a file.

Usually, you run grep on a single file like this:

grep search_term filename

Grep is quite versatile. If you want to search all the files in a directory with grep, use it like this:

There is a problem with it. It only searches in all the files in the current directory. It won’t search in the subdirectories.

You can make grep search in all the files and all the subdirectories of the current directory using the -r recursive search option:

You may also specify the directory path if you are not in the directory where you want to perform the search:

grep -r search_term directory_path

That was a quick recap. Let me show you all this in details with proper examples so that it is easier for you to understand.

Grep search in all files of a directory

Here’s the directory structure I am going to use in this example. Except empty.txt, all files contain the term ‘simple’ on which I’ll perform the grep search.

[email protected]:~/scripts$ tree . ├── dir1 │ └── new_script.sh ├── dir2 │ └── your_script.sh ├── dir3 │ ├── empty.txt │ └── linked.txt -> ../../sample.txt ├── my_script.sh └── your_script.sh 3 directories, 6 files 

To search for the word ‘simple’ in all the files of the current directories, just use wild card (*). The wild card actually substitutes with the name of all the files and directories in the current directory.

This will search in all the files in the current directories, but it won’t enter the subdirectories. Since you cannot directly grep search on a directory, it will show «XYZ is a directory» error along with search results.

Grep search in all files of a folder

If you are not in the same directory where you want to perform, you can specify the directory path and end it with /*

grep search_term directory_path/*

Basically, you are using the wild card to expand on all the elements (files and directories) of the given directory.

Now that you know that, let’s see how you can perform a recursive search with grep so that it also looks into the files in the subdirectories.

Grep recursive search in all subdirectories of a directory

Grep provides a -r option for the recursive search. With this option, grep will look into all the files in the current (or specified) directory and it will also look into all the files of all the subdirectories.

Читайте также:  Top packages for linux

Here’s the recursive search I performed in the previous example to do a grep search in the current folder:

Grep recursive search in all files and all subdirectories

There is also a -R option for recursive search and it works almost the same as the -r option.

So, what’s the difference grep -r and grep -R ? Only one, actually. The -R is dereferenced search which means it will follow the symbolic links to go to the original file (which may be located in some other part of the system).

Take a look at the output of the -R search in the same example:

Recursive search with grep including the symbolic links

Did you notice that it gives an additional search result with the linked.txt which is basically a symbolic link and was omitted from the grep search with -r option?

If you are not in the directory where you want to perform the recursive search, just provide the absolute or relative path of the directory to grep command:

grep -r search_term path_to_directory

Bonus tip: Exclude a certain directory from the recursive grep search

Everything seems good but what if you want to exclude a certain directory from the recursive search? There is a provision for that too. I told you, grep is an extremely versatile command.

grep -r --exclude-dir=dir_name serach_term directory_path

That’s not it. You can exclude more than one subdirectory from the recursive search in the following fashion:

grep -r --exclude-dir= serach_term directory_path

Here’s what excluding directories look like in our example here:

Exclude directories from grep recursive search

And yes, as demonstrated by the example above, the exclusion works with both -r and -R recursive options.

To summarize

Here’s a quick summary of using grep search for multiple files and directories:

Grep Command Description
grep string * Searches in all the files in current directory
grep string dir Searches in all the files in dir directory
grep -r string . Recursive search in all the files in all the subdirectories
grep -r string dir Recursive search in all files in all the subdirectories of dir
grep -R string . Same as r but follows the symbolic links

I hope you like this quick grep tip. If you want more, you may read this detailed tutorial on using the grep command:

Let me know if you have any questions or suggestions on this topic.

Источник

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.

Читайте также:  Linux подключить телевизор wifi

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.

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.

Источник

Оцените статью
Adblock
detector