如何在 Linux find 命令中使用管道?

Linux find语句是使用最广泛的语句之一,它允许我们遍历文件层次结构。它主要用于查找特定的文件或目录,我们还可以附加不同的其他 Linux 语句或标志来增强或执行复杂的操作。

让我们探索一个find语句的示例以更好地理解它。

在下面显示的 Linux 代码中,我试图在我的下载文件夹中搜索一个文件,为此我使用了find语句

find sample.sh
输出结果
sample.sh

请注意,如果 find 命令能够定位该文件,那么它将简单地打印文件的名称,否则它将不会返回终端进程将终止的任何内容。

现在我们知道了 find 语句是如何工作的,让我们探索一下我们想要将 find 命令与管道一起使用的情况。

Linux 中的管道只是键盘上的垂直条。它用于将其左侧的命令视为其右侧命令的输入。

现在我们知道了 Linux 中的管道 find 命令,让我们考虑一个示例,我们将在 Linux 命令中同时使用这两个命令。

命令

find . -name '*.txt' | xargs cat

在上面的命令中,就在管道之前,我正在考虑名称以.txt扩展名结尾的所有文件,然后在管道之后,我只是在cat命令的帮助下打印这些文件。

输出结果

immukul@192 directory1 % find . -name '*.txt' | xargs cat
this is a test file and it is used for testing and is not available for anything else so
please stop asking, lionel messi
orange
orange
blabla
blabla
foofoo
here
is the
text
to keep between the 2 patterns
bar
blabla
blabla