How to find all file extensions recursively from a

2020-02-16 07:44发布

What command, or collection of commands, can I use to return all file extensions in a directory (including sub-directories)?

Right now, I'm using different combinations of ls and grep, but I can't find any scalable solution.

8条回答
smile是对你的礼貌
2楼-- · 2020-02-16 07:48

list all extensions and their counts of current and all sub-directories

ls -1R | sed 's/[^\.]*//' | sed 's/.*\.//' | sort | uniq -c
查看更多
干净又极端
3楼-- · 2020-02-16 07:48

Yet another solution using find (that should even sort file extensions with embedded newlines correctly):

# [^.]: exclude dotfiles
find . -type f -name "[^.]*.*" -exec bash -c '
  printf "%s\000" "${@##*.}"
' argv0 '{}' + |
sort -uz | 
tr '\0' '\n'
查看更多
干净又极端
4楼-- · 2020-02-16 07:50

if you are using Bash 4+

shopt -s globstar
for file in **/*.*
do
  echo "${file##*.}
done

Ruby(1.9+)

ruby -e 'Dir["**/*.*"].each{|x|puts x.split(".")[-1]}' | sort -u
查看更多
男人必须洒脱
5楼-- · 2020-02-16 07:52

I was just quickly trying this as I was searching Google for a good answer. I am more Regex inclined than Bash, but this also works for subdirectories. I don't think includes files without extensions either:

ls -R | egrep '(\.\w+)$' -o | sort | uniq -c | sort -r

查看更多
够拽才男人
6楼-- · 2020-02-16 07:54

Boooom another:

find * | awk -F . {'print $2'} | sort -u
查看更多
叛逆
7楼-- · 2020-02-16 07:58

How about this:

find . -type f -name '*.*' | sed 's|.*\.||' | sort -u
查看更多
登录 后发表回答