ThomasDr
ThomasDr

Reputation: 366

Scanning, group and count file extensions on Linux

Is there a way to scan a path and group and count the file extensions?

Upvotes: 2

Views: 452

Answers (2)

Hastur
Hastur

Reputation: 2828

How to count files

To count how many files for each extension are present in a path, you can use one of the answers of "Count files in a directory by extension" question of another site[1], e.g.:

ls | awk -F . '{print $NF}' | sort | uniq -c | awk '{print $2,$1}'

How to list grouped by extension

To group the files by extension you can use simply the -X option of ls

ls -X
--sort=WORD
          sort by WORD instead of name: 
          none -U, extension -X, size -S, time -t, version -v

Note:

  • The concept of extension is imported from DOS, under Unix there is only the file name eventually with more than one '.' character inside...

Upvotes: 0

avivb
avivb

Reputation: 187

If I understand your question, you can use this command -

ls -ls | awk '{print $10}' | grep "\." | awk -F. '{print $2}' | sort | uniq -c

which count the extensions in the current path.

Upvotes: 1

Related Questions