如何在 Linux 系统中查找文件夹层次结构中的所有不同文件扩展名?
linuxoperating systemopen source更新于 2026/2/15 4:22:17
虽然在 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
相关文章
有用资源
linux 参考教程 - 该教程包含有关 linux 的更多信息:https://www.cainiaomax.com/linux/

