Return to Snippet

Revision: 22667
at January 18, 2010 21:26 by tm


Initial Code
# Not sure, what this is good for - however this is asked sometimes.

# I specifically exclude "dotfiles" and only consider files of the form "foo.bar" (i.e. we 
# don't consider filenames without an "inner dot" at all)
find . -type f -name '[^.]*.*' \
       -exec bash -c 'printf "%s\n" ${@##*.}' _ {} + | sort -u
# (or ... | sort | uniq, in case the sort lacks -u, which is a GNUism)

# Of course it's trivial to do it non-recursively:
for f in *.*; do printf "%s\n" "${f##*.}"; done | sort -u

# or recursively again, with a bash4 associative array, including
# a count for each extension (no sorting here):
unset a; declare -A a
while IFS= read -r ext; do 
    ((a[$ext]++))
done < <(find . -type f -name '[^.]*.*' \
         -exec bash -c 'printf "%s\n" ${@##*.}' _ {} +)
for ext in "${!a[@]}"; do
    printf "'%s' (%s)\n" "$ext" "${a[$ext]}"
done

# NOTE: all methods above fail, if an extension contains embedded newlines.

Initial URL


Initial Description


Initial Title
Find all file extensions used under some directory (recursively)

Initial Tags
Bash, find

Initial Language
Bash