如何在資料夾層次結構中查詢所有不同的副檔名(Linux)?


雖然有很多方法可以使用不同的實用程式命令在 Linux 中查詢特定檔案的副檔名,但如果我們需要查詢資料夾層次結構中所有不同的副檔名,則首先需要了解 **find** 和 **sed** 命令的用途,因為這些命令將用於列印資料夾或資料夾層次結構中所有不同的副檔名。

我們必須瞭解的兩個 Linux 實用程式命令是:

  • **find** - 用於查詢特定檔案或目錄

  • **sed** - 流編輯器的縮寫,用於執行搜尋、編輯和替換等功能。

當我們談論單個資料夾時,甚至不需要 find 命令,因為我們可以簡單地迭代所有檔案,然後也使用 sort 命令。

假設我有一個名為 dir1 的目錄,我想知道此資料夾中不同的副檔名。

為此,我將在該目錄內鍵入下面顯示的命令。

命令

for file in *.*; do printf "%s
" "${file##*.}"; done | sort -u

輸出

immukul@192 dir1 % for file in *.*; do printf "%s
" "${file##*.}"; done | sort -u app c dmg doc docx epub go h htm jnlp jpeg jpg json mp4 o odt pdf png srt torrent txt webm xlsx zip

正如您所注意到的,上面示例中列出的所有副檔名都是不同的。現在,如果我們想列出檔案層次結構中所有不同的副檔名,則需要向上述命令新增遞迴。

命令

find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort -u

輸出

immukul@192 dir1 % find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort -u
app
c
dmg
bz2
callgrind
case-hosts
cc
cfg
cgi
comments
conf
config
contention
cov
cpu
crash
crt
css
csv
dat
debug_rnglists
demangle-expected
dep
description

更新於:2021年7月29日

1K+ 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.