我可以使用什么命令或命令集来返回目录(包括子目录)中的所有文件扩展名?
现在,我正在使用 ls和 grep的不同组合,但是我找不到任何可伸缩的解决方案。
ls
grep
How about this:
find . -type f -name '*.*' | sed 's|.*\.||' | sort -u
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
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'
find . -type f | sed 's|.*\.||' | sort -u
Also works on mac.
Boooom another:
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. I don't think includes files without extensions either:
ls -R | egrep '(\.\w+)$' -o | sort | uniq -c | sort -r
list all extensions and their counts of current and all sub-directories
ls -1R | sed 's/[^\.]*//' | sed 's/.*\.//' | sort | uniq -c
Another one, similar to others but only uses two programs (find and 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 . in it.
-name "*\.*"
.
-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.
-F .
$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