-1

I found a related question here:

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

But my case is slightly different. Using bash on Ubuntu 14.04 Linux, if I have a bunch of files like the ones below:

ls -1 | sort -V
fileA.foo.bar.txt.gz
fileA.foo.foo.txt.gz
fileA.xyz.bar.txt.gz
fileB.foo.bar.txt.gz
fileB.foo.foo.txt.gz
fileB.xyz.bar.txt.gz

I would like to know how many files of each extension I get from the first separator found (e.g. \. in the example). So it would be:

2 .foo.bar.txt.gz
2 .foo.foo.txt.gz
2 .xyz.bar.txt.gz

This is instead of the best answers that I found in the question above:

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

find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort | uniq -c
      6 gz
Inian
  • 80,270
  • 14
  • 142
  • 161
719016
  • 9,922
  • 20
  • 85
  • 158
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Mar 02 '18 at 02:49

2 Answers2

2

All bash solution for files in cwd:

declare -A a         # declare an associative array a
for f in *.*         # loop all filenames with a .
do 
  ((a[${f#*.}]++))   # increment array elements value
done

Outputing counts:

for k in "${!a[@]}"  # loop all array keys
do 
  echo ${a[$k]} $k   # output value and key
done
1 zip
2 txt
1 txt~
James Brown
  • 36,089
  • 7
  • 43
  • 59
0

You can get rid of sed or perl and use cut, try using:

find . -type f  | cut -d. -f3- | sort | uniq -c
dubioushencho
  • 466
  • 4
  • 7