如何在文件夹层次结构中查找所有不同的文件扩展名(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.