[英]How to find all file extensions recursively from a directory?
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
不同组合,但我找不到任何可扩展的解决方案。
这个怎么样:
find . -type f -name '*.*' | sed 's|.*\.||' | sort -u
find . -type f | sed 's|.*\.||' | sort -u
也适用于 mac。
列出所有扩展名及其当前和所有子目录的计数
ls -1R | sed 's/[^\.]*//' | sed 's/.*\.//' | sort | uniq -c
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
嘘另一个:
find * | awk -F . {'print $2'} | sort -u
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
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.
我比 Bash 更倾向于 Regex,但这也适用于子目录。 I don't think includes files without extensions either:
我也不认为包含没有扩展名的文件:
ls -R | egrep '(\\.\\w+)$' -o | sort | uniq -c | sort -r
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 "%s\000" "${@##*.}"
' argv0 '{}' + |
sort -uz |
tr '\0' '\n'
Another one, similar to others but only uses two programs (find and awk)另一个,与其他类似,但只使用两个程序(find 和 awk)
find ./ -type f -name "*\\.*" -printf "%f\\n" | awk -F . '!seen[$NF]++ {print $NF}'
-type f
restricts it to just files, not directories -type f
将其限制为仅文件,而不是目录
-name "*\\.*"
ensures the filename has a .
-name "*\\.*"
确保文件名有一个.
in it.在其中。
-printf "%f\\n"
prints just the filename, not the path to the filename. -printf "%f\\n"
只打印文件名,而不是文件名的路径。
-F .
makes awk utilize a period as the field separator.使 awk 使用句点作为字段分隔符。
$NF
is the last field, separated by periods. $NF
是最后一个字段,由句点分隔。
!seen[$NF]++
evaluates to true the first time an extension is encountered, and false every other time it is encountered. !seen[$NF]++
在第一次遇到扩展时评估为真,每隔一次遇到一次就为假。
print $NF
prints the extension. print $NF
打印扩展名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.