bash 如何从目录中递归查找所有文件扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4998290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find all file extensions recursively from a directory?
提问by Matthew
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.
现在,我使用的不同组合ls
和grep
,但我无法找到任何可扩展解决方案。
回答by thkala
How about this:
这个怎么样:
find . -type f -name '*.*' | sed 's|.*\.||' | sort -u
回答by marcosdsanchez
find . -type f | sed 's|.*\.||' | sort -u
Also works on mac.
也适用于 mac。
回答by mindon
list all extensions and their counts of current and all sub-directories
列出所有扩展名及其当前和所有子目录的计数
ls -1R | sed 's/[^\.]*//' | sed 's/.*\.//' | sort | uniq -c
回答by kurumi
if you are using Bash 4+
如果您使用的是 Bash 4+
shopt -s globstar
for file in **/*.*
do
echo "${file##*.}
done
Ruby(1.9+)
红宝石(1.9+)
ruby -e 'Dir["**/*.*"].each{|x|puts x.split(".")[-1]}' | sort -u
回答by tooly
Yet another solution using find (that should even sort file extensions with embedded newlines correctly):
另一个使用 find 的解决方案(甚至应该正确地对带有嵌入换行符的文件扩展名进行排序):
# [^.]: exclude dotfiles
find . -type f -name "[^.]*.*" -exec bash -c '
printf "%sfind * | awk -F . {'print '} | sort -u
0" "${@##*.}"
' argv0 '{}' + |
sort -uz |
tr 'ls -1 | sed 's/.*\.//' | sort -u
' '\n'
回答by ackuser
Boooom another:
嘘另一个:
##代码##回答by TimeDelta
Update: You are correct Matthew. Based on your comment, here is an updated version:
更新:你是对的,马修。根据您的评论,这是一个更新版本:
ls -R1 | egrep -C 0 "[^\.]+\.[^\./:]+$" | sed 's/.*\.//' | sort -u
ls -R1 | egrep -C 0 "[^\.]+\.[^\./:]+$" | sed 's/.*\.//' | sort -u
回答by Mehcs85
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:
我只是在快速尝试这个,因为我在谷歌搜索一个好的答案。我比 Bash 更倾向于 Regex,但这也适用于子目录。我也不认为包含没有扩展名的文件:
ls -R | egrep '(\.\w+)$' -o | sort | uniq -c | sort -r
ls -R | egrep '(\.\w+)$' -o | sort | uniq -c | sort -r