home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 16.23 Comparing Filenames in Two Directory Trees Chapter 16
Where Did I Put That?
Next: 16.25 Listing Files by Age and Size
 

16.24 Counting Files by Types

I use awk ( 33.11 ) a lot. One of my favorite features of awk is its associative arrays. This means awk can use anything as an index into an array. In the next example, I use the output of the file ( 25.8 ) command as the index into an array to count how many files there are of each type:








${*-.}
 
xargs
 








#!/bin/sh
# usage: count_types [directory ...]
# Counts how many files there are of each type
# Original by Bruce Barnett
# Updated version by yu@math.duke.edu (Yunliang Yu)
find ${*-.} -type f -print | xargs file |
awk '{
        $1=NULL;
        t[$0]++;
} 
END {
        for (i in t) printf("%d\t%s\n", t[i], i);
}' | sort -nr   # Sort the result numerically, in reverse

The output of this might look like:

38  ascii text
32  English text
20  c program text
17  sparc executable not stripped
12  compressed data block compressed 16 bits
8   executable shell script
1   sparc demand paged dynamically linked executable
1   executable /bin/make script

- BB


Previous: 16.23 Comparing Filenames in Two Directory Trees UNIX Power Tools Next: 16.25 Listing Files by Age and Size
16.23 Comparing Filenames in Two Directory Trees Book Index 16.25 Listing Files by Age and Size

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System