1. Overview

Hidden files are usually system or application files concealed to prevent accidental modifications. By default, Linux hides several sensitive system files. Hidden files and directories in Linux typically start with the “.” symbol.

In this tutorial, we’ll learn how to use the grep command to search for a specific text pattern in a directory tree containing hidden files and directories.

2. Setup

Let’s create a directory named working_directory:

$ mkdir working_directory

Then, we can navigate into it:

$ cd working_directory

Next, let’s create two hidden directories:

$ mkdir .directory_1 .directory_2

In .directory_1, let’s create a hidden file named .sample_1.txt with this content:

$ cat >> .sample_1.txt
This is an article on how to grep hidden files and directories on Baeldung
We'll discuss different methods of solving this problem.
baeldung is awesome!

Finally, in .directory_2, let’s create a hidden file named .sample_2.txt with this content:

$ cat >> .sample_2.txt
This is the second hidden file.
It also has the word Baeldung that we'll search for with grep
BAELdung is very informative!

Overall, our directory structure should match this:

$ ls -a
.  ..  .directory_1  .directory_2

Each directory contains a hidden text file. We’ll be using grep to search for matching patterns in each file.

3. Searching Hidden Files and Directories

The grep command is a useful Linux command for performing file content search. It also enables us to recursively search through a specific directory, to find all files matching a given pattern.

Let’s look at the simplest method we can use to grep the word “Baeldung” that’s included in both .sample_1.txt and .sample_2.txt:

$ grep -r "Baeldung" .

We should get this output:

The “.” matches the current path, which includes both hidden files and non-hidden files.

This simple command searches through every directory in our current directory, including hidden files and directories, for the text pattern “Baeldung”. Furthermore, it highlights the file path and the search term in different colors, which makes it easy to scan through the results.

We can also modify the grep command to search for a text pattern with the –ignore-case option enabled:

$ grep -ir "Baeldung" .

This helps widen the scope of results returned because it’ll match both lowercase and uppercase variations of the search term.

This method of searching may be simple, but it’s inefficient in situations where we need to explicitly search only through hidden files or directories. This is because it searches every file and directory, hidden or non-hidden, for a specific text pattern. It can be time-consuming in directory hierarchies with thousands of files and directories.

Let’s explore other methods we can use to restrict the search criteria to either only hidden files or only directories.

3.1. Searching Only Hidden Files

When we have several hidden files in the current directory, we can restrict our search scope to only hidden files. This can be efficient because it ignores anything that’s not a hidden file.

We can run this command to search for the text pattern “Baeldung” in our working directory:

$ find . -name ".*" -type f -exec grep -i "Baeldung" {} \;
This is an article on how to grep hidden files and directories on Baeldung
baeldung is awesome!
It also has the word Baeldung that we'll search for with grep
BAELdung is very informative!

Here, we’re using the find command to search for all files with a name starting with a “.” symbol. We then execute the grep command, which performs the pattern-matching search.

This command is useful. However, it can be confusing because it doesn’t include the name of the hidden file containing the text pattern. It simply lists all lines containing occurrences of the text pattern specified, from all the hidden files found.

Alternatively, we can use pipes to get a slightly different output:

$ find . -name ".*" -type f | xargs grep -i "Baeldung"
./.directory_1/.sample_1.txt:This is an article on how to grep hidden files and directories on Baeldung
./.directory_1/.sample_1.txt:baeldung is awesome!
./.directory_2/.sample_2.txt:It also has the word Baeldung that we'll search for with grep
./.directory_2/.sample_2.txt:BAELdung is very informative!

Here, we’re using the eXtended ARGumentS (xargs) command to execute the grep command with the –ignore-case option enabled. This method is more useful because it also includes the path to the file containing the text pattern specified.

3.2. Searching Only Hidden Directories

Let’s look at a useful method for when we want to search for the text pattern “Baeldung” only through hidden directories in our working directory:

$ find . -name ".*" -type d -exec grep -ir "Baeldung" {} \;
./.directory_1/.sample_1.txt:This is an article on how to grep hidden files and directories on Baeldung
./.directory_1/.sample_1.txt:baeldung is awesome!
./.directory_2/.sample_2.txt:It also has the word Baeldung that we'll search for with grep
./.directory_2/.sample_2.txt:BAELdung is very informative!

Here, we’ve used the find command to search for all directory names starting with a “.” symbol. We then execute the grep command to search for the text pattern with the –ignore-case(-i) option enabled. We’re also using the –recursive(-r) option to search through every file in each directory.

This method is useful because it also lists the path of the patterns found.

4. Conclusion

In this article, we’ve briefly discussed hidden files and directories. We looked at the Linux grep command and how to use it to search for a specific text pattern in hidden files and directories. We’ve explored different methods of using the grep command to search through hidden files. However, the specific method to use is generally dependent on the situation.

The first method, using only grep, is simple and effective for when we don’t have a large number of files in the working directory. It also has color encoding, which makes it easier to scan through the output. Alternatively, we can use either the second or third methods for cases where we want to narrow down the search criteria for more precision and efficiency.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!