如何在 Linux find 命令中使用管道?
Linux find 命令是最常用的命令之一,它允许我们遍历文件层次结构。它主要用于查找特定的文件或目录,我们还可以附加其他 Linux 命令或标志来增强或执行复杂操作。
让我们来看一个 find 命令的示例,以便更好地理解它。
在下面所示的 Linux 代码中,我尝试在我的 Downloads 文件夹中搜索一个文件,为此我使用了 find 命令。
find sample.sh
输出
sample.sh
请注意,如果 find 命令能够找到该文件,它将只打印文件名;如果找不到,则不会返回任何内容,终端进程将终止。
现在我们知道了 find 命令的工作原理,让我们探讨一下如何将 find 命令与管道一起使用。
Linux 中的管道只是键盘上的竖线。它用于将它左侧的命令作为右侧命令的输入。
现在我们了解了 Linux 中的 find 命令和管道,让我们来看一个同时使用这两个命令的例子。
命令
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
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP