Dan
Dan

Reputation: 23

Create a command to display extensions of regular files in current directory with counts to each extension. Files without extension should be ignored

Suppose you have these files in your current directory:

ifsmy12@cloudshell:~/3300/tests/t2/testers (cs3300-301722)$ ls
bar  emp.lst  foo  q5.sh  q6.sh  q7.sh  q8.sh

My desired output is:

      1 lst
      4 sh

I am able to print out the file extensions with the counts as shown above however I can't quite figure out how to exclude the files without extensions. Could someone tell me where I'm going wrong or what I'm missing? I will provide my command and output below:

ifsmy12@cloudshell:~/3300/tests/t2/testers (cs3300-301722)$ find . -type f | sed 's/.*\.//' | sort | uniq -c
      1 /bar
      1 /foo
      1 lst
      4 sh

Thank you in advance.

Upvotes: 1

Views: 59

Answers (2)

potong
potong

Reputation: 58483

This might work for you (GNU sed):

find . -type f | sed -n 's/.*\.//p' | sort | uniq -c

For the sed command: turn on the -n option which requires explicit printing and then use the substitution flag p to print on a successful substitution.

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163467

You might use a pattern to match at least a . followed by matching 1+ times any char except . or / until the end of the string $

.*\.[^/.]+$

For example

find . -type f -regex '.*\.[^/.]+$' | sed 's/.*\.//' | sort | uniq -c

Output

1 lst
4 sh

Upvotes: 2

Related Questions