How to find all file extensions recursively from a directory?

笑着哭i 提交于 2019-12-18 10:16:07

问题


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.


回答1:


How about this:

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



回答2:


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

Also works on mac.




回答3:


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

ls -1R | sed 's/[^\.]*//' | sed 's/.*\.//' | sort | uniq -c



回答4:


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:


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'



回答6:


Boooom another:

find * | awk -F . {'print $2'} | sort -u



回答7:


ls -1 | sed 's/.*\.//' | sort -u

Update: You are correct Matthew. Based on your comment, here is an updated version:

ls -R1 | egrep -C 0 "[^\.]+\.[^\./:]+$" | sed 's/.*\.//' | sort -u




回答8:


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



来源:https://stackoverflow.com/questions/4998290/how-to-find-all-file-extensions-recursively-from-a-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!