在Linux机器上,我希望遍历一个文件夹层次结构,并获得其中所有不同文件扩展名的列表。

从外壳中实现这一点的最佳方法是什么?


当前回答

递归版本:

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

如果你想要总数(有多少次扩展被看到):

find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort | uniq -c | sort -rn

非递归(单个文件夹):

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

我是根据这个论坛帖子写的,功劳应该在那里。

其他回答

不需要管道排序,awk可以做所有的事情:

find . -type f | awk -F. '!a[$NF]++{print $NF}'

加入我自己的变化。我认为这是最简单的,当效率不是一个大问题的时候,它是有用的。

find . -type f | grep -oE '\.(\w+)$' | sort -u

我发现它简单快捷……

   # find . -type f -exec basename {} \; | awk -F"." '{print $NF}' > /tmp/outfile.txt
   # cat /tmp/outfile.txt | sort | uniq -c| sort -n > tmp/outfile_sorted.txt

找到每一个点,只显示后缀。

find . -type f -name "*.*" | awk -F. '{print $NF}' | sort -u

如果你知道所有后缀有3个字符,那么

find . -type f -name "*.???" | awk -F. '{print $NF}' | sort -u

或使用sed显示所有1到4个字符的后缀。将{1,4}更改为您希望在后缀中使用的字符范围。

find . -type f | sed -n 's/.*\.\(.\{1,4\}\)$/\1/p'| sort -u

递归版本:

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

如果你想要总数(有多少次扩展被看到):

find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort | uniq -c | sort -rn

非递归(单个文件夹):

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

我是根据这个论坛帖子写的,功劳应该在那里。