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

后端 未结 16 1247
梦谈多话
梦谈多话 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:29

    Try this (not sure if it's the best way, but it works):

    find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
    

    It work as following:

    • Find all files from current folder
    • Prints extension of files if any
    • Make a unique sorted list
    0 讨论(0)
  • 2020-11-30 16:29

    Recursive version:

    find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort -u
    

    If you want totals (how may times the extension was seen):

    find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort | uniq -c | sort -rn
    

    Non-recursive (single folder):

    for f in *.*; do printf "%s\n" "${f##*.}"; done | sort -u
    

    I've based this upon this forum post, credit should go there.

    0 讨论(0)
  • 2020-11-30 16:29

    you could also do this

    find . -type f -name "*.php" -exec PATHTOAPP {} +
    
    0 讨论(0)
  • 2020-11-30 16:30

    Adding my own variation to the mix. I think it's the simplest of the lot and can be useful when efficiency is not a big concern.

    find . -type f | grep -o -E '\.[^\.]+$' | sort -u
    
    0 讨论(0)
  • 2020-11-30 16:34

    I think the most simple & straightforward way is

    for f in *.*; do echo "${f##*.}"; done | sort -u
    

    It's modified on ChristopheD's 3rd way.

    0 讨论(0)
  • 2020-11-30 16:36

    The accepted answer uses REGEX and you cannot create an alias command with REGEX, you have to put it into a shell script, I'm using Amazon Linux 2 and did the following:

    1. I put the accepted answer code into a file using :

      sudo vim find.sh

    add this code:

    find ./ -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
    

    save the file by typing: :wq!

    1. sudo vim ~/.bash_profile

    2. alias getext=". /path/to/your/find.sh"

    3. :wq!

    4. . ~/.bash_profile

    0 讨论(0)
提交回复
热议问题