Contents

如何grep隐藏的文件和目录

1. 概述

隐藏文件 通常是为了防止意外修改而隐藏的系统或应用程序文件。默认情况下,Linux 隐藏了几个敏感的系统文件。Linux 中的隐藏文件和目录通常以“.”开头。象征。

在本教程中,我们将学习如何使用grep 命令在包含隐藏文件和目录的目录树中搜索特定的文本模式。

2. 设置

让我们创建一个名为working_directory的目录:

$ mkdir working_directory

然后,我们可以导航到它:

$ cd working_directory

接下来,让我们创建两个隐藏目录:

$ mkdir .directory_1 .directory_2

在*.directory_1中,让我们使用以下内容创建一个名为.sample_1.txt*的隐藏文件:

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

最后,在*.directory_2中,让我们创建一个名为.sample_2.txt*的隐藏文件,其中包含以下内容:

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

总的来说,我们的目录结构应该与此匹配:

$ ls -a
.  ..  .directory_1  .directory_2

每个目录都包含一个隐藏的文本文件。我们将使用grep在每个文件中搜索匹配的模式。

3. 搜索隐藏文件和目录

** grep命令是用于执行文件内容搜索的有用 Linux 命令。它还使我们能够递归地搜索特定目录,以查找与给定模式匹配的所有文件。**

让我们看一下我们可以用来grep包含在两者中的单词“Blogdemo”的最简单方法*.directory_1.txt*和 .directory_2.txt

$ grep -r "Blogdemo" .

我们应该得到这个输出:

/uploads/grep_hidden_files_directories/1.png

这 ”.” 匹配当前路径,包括隐藏文件和非隐藏文件。

这个简单的命令在我们当前目录中的每个目录中搜索文本模式“Blogdemo”,包括隐藏文件和目录。此外,它以不同的颜色突出显示文件路径和搜索词,便于浏览结果。

我们还可以修改grep命令以搜索启用了*–ignore-case*选项的文本模式:

$ grep -ir "Blogdemo" .

/uploads/grep_hidden_files_directories/2.png

这有助于扩大返回结果的范围,因为它将匹配搜索词的小写和大写变体。

**这种搜索方法可能很简单,但在我们只需要显式搜索隐藏文件或目录的情况下效率低下。这是因为它在每个文件和目录中搜索特定的文本模式,无论是隐藏的还是非隐藏的。**在具有数千个文件和目录的目录层次结构中,这可能很耗时。

让我们探索一下我们可以用来将搜索条件限制为仅隐藏文件或仅目录的其他方法。

3.1. 仅搜索隐藏文件

当我们在当前目录中有多个隐藏文件时,我们可以将搜索范围限制为仅隐藏文件。这可能很有效,因为它会忽略任何不是隐藏文件的内容。

我们可以运行这个命令在我们的工作目录中搜索文本模式“Blogdemo”:

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

在这里,我们使用find 命令搜索名称以“.”开头的所有文件。象征。然后我们执行grep命令,该命令执行模式匹配搜索。

这个命令很有用。但是,它可能会造成混淆,因为它不包括包含文本模式的隐藏文件的名称。它只是从找到的所有隐藏文件中列出包含指定文本模式出现的所有行。

或者,我们可以使用管道来获得稍微不同的输出:

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

在这里,我们使用 eXtended ARGumentS ( /xargs ) 命令来执行启用了*–ignore-case选项的grep*命令。此方法更有用,因为它还包括包含指定文本模式的文件的路径。

3.2. 仅搜索隐藏目录

当我们只想通过工作目录中的隐藏目录搜索文本模式“Blogdemo”时,让我们看看一个有用的方法:

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

在这里,我们使用find命令搜索所有以“.”开头的目录名称。象征。然后我们执行grep命令来搜索启用了 –ignore-case(-i) 选项的文本模式。我们还使用 –recursive(-r) 选项来搜索每个目录中的每个文件。

此方法很有用,因为它还列出了找到的模式的路径。