在Linux机器上,我希望遍历一个文件夹层次结构,并获得其中所有不同文件扩展名的列表。
从外壳中实现这一点的最佳方法是什么?
在Linux机器上,我希望遍历一个文件夹层次结构,并获得其中所有不同文件扩展名的列表。
从外壳中实现这一点的最佳方法是什么?
当前回答
Powershell:
dir -recurse | select-object extension -unique
感谢http://kevin-berridge.blogspot.com/2007/11/windows-powershell.html
其他回答
我发现它简单快捷……
# find . -type f -exec basename {} \; | awk -F"." '{print $NF}' > /tmp/outfile.txt
# cat /tmp/outfile.txt | sort | uniq -c| sort -n > tmp/outfile_sorted.txt
在Python中,为非常大的目录使用生成器,包括空白扩展名,并获取每个扩展名出现的次数:
import json
import collections
import itertools
import os
root = '/home/andres'
files = itertools.chain.from_iterable((
files for _,_,files in os.walk(root)
))
counter = collections.Counter(
(os.path.splitext(file_)[1] for file_ in files)
)
print json.dumps(counter, indent=2)
你也可以这样做
find . -type f -name "*.php" -exec PATHTOAPP {} +
加入我自己的变化。我认为这是最简单的,当效率不是一个大问题的时候,它是有用的。
find . -type f | grep -oE '\.(\w+)$' | sort -u
Powershell:
dir -recurse | select-object extension -unique
感谢http://kevin-berridge.blogspot.com/2007/11/windows-powershell.html