How can I find all of the distinct file extensions in a folder hierarchy?

后端 未结 16 1278
梦谈多话
梦谈多话 2020-11-30 16:00

On a Linux machine I would like to traverse a folder hierarchy and get a list of all of the distinct file extensions within it.

What would be the best way to achieve

16条回答
  •  眼角桃花
    2020-11-30 16:49

    None of the replies so far deal with filenames with newlines properly (except for ChristopheD's, which just came in as I was typing this). The following is not a shell one-liner, but works, and is reasonably fast.

    import os, sys
    
    def names(roots):
        for root in roots:
            for a, b, basenames in os.walk(root):
                for basename in basenames:
                    yield basename
    
    sufs = set(os.path.splitext(x)[1] for x in names(sys.argv[1:]))
    for suf in sufs:
        if suf:
            print suf
    

提交回复
热议问题