Shell One-Liner For Most Common C Includes

Here's a shell one-liner for finding the most commonly included header files in a C project:

find -name '*.c' -o name '*.h' \
 | xargs awk -F '[<>]' '/#include/ { print $2 }' \
 | sort \ 
 | uniq -c \
 | sort -nr

Running it was surprisingly fast, only taking a few seconds to go through the Linux source tree. Here is some sample output:

  12000 linux/module.h
   8683 linux/kernel.h
   8560 linux/slab.h
   6617 linux/init.h
   5552 linux/types.h
   4867 linux/delay.h
   4454 linux/platform_device.h
   4207 linux/interrupt.h
   3869 linux/io.h

It may also work for C++ projects, but angled brackets being used for template parameters might upset it.

#bash #c #shell