如何在 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
廣告