Search code examples
linuxbashunixfile-type

Finding the number of specific files via bash


Fill in the dots on the next Unix command, like so, the standard out gives an overview per file type with the number of files in the /dev directory. In this overview, all filetypes must be listed in descending order of the number of found files of the certain type. If there are filetypes with an equal number of files, they must be listed in alphabetical order.

$ find /dev -ls | …
  7 c
  6 l
  3 d

Tips:

The part already given with the find-command, also finds hidden files in the directory.

With help of the cut-command, you can select a certain part of a line, the two most important options are -f and -d. The first one splits the lines in columns. By default, the tab-character is used. With the option -d you can specify a custom delimiter.

tr, sort and uniq might be useful.

What I have so far:

find /dev -ls | tr \\t " " | tr -s " " | cut -f3 -d ' ' | cut -c-1 | sort | uniq -c | sort -r

But this doesn't seem to work...

Thanks in advance.


Solution

  • I like use awk for this cases instead of tr

     find /dev -ls | gawk '{ c=substr($3,1,1) ; x[c]++ } END { for(y in x) print x[y] " " y }' | sort -n