ILD

file name length statistics under a directory
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2024-11-7 站点:Inside Linux Development

下面这个结构体用来存储文件名(不包括路径)

struct qstr

{

        union {

                char name_arr[31];

                char *name_ptr;

        } __attribute__((packed));

        unsigned char len;

};


小于31长度的,直接存储,超过的分配一个指针存储。这个31要如何选取呢?太小则需要很多指针分配,太大则浪费内存。因此想着统计一个目录下,各个长度的分布。这个目录是一个代码库和编译产物,有100多万个文件。


1 打印出所有文件名

$ find -printf "%f\n" > a.log

部分结果:

src

qca-romboot

utils

bin2hex

bin2hex.c


2 计算出文件名的长度

$ awk '{ print length, $0 }' < a.log > b.log

部分结果:

3 src

11 qca-romboot

5 utils

7 bin2hex

9 bin2hex.c


3 排序

$ sort -n -s b.log > c.log

开头内容,这样就从小到大排列了

1 .

1 t

1 f

1 t

1 h


4 只取长度

$ cut -f1 -d' ' c.log  > d.log


5 统计重复个数,这里uniq的-c选项是整个系列命令的核心。


$ uniq -c d.log  > e.log


    372 1

   9537 2

  22035 3

  24898 4

  54734 5

  58613 6

  90132 7

 115610 8

  91813 9

  95160 10

  81227 11

  76916 12

  65684 13

  62856 14

  52073 15

  45055 16

  39114 17

  34462 18

  29163 19

  25042 20

  22498 21

  19887 22

  18343 23

  15222 24

  13940 25

  11819 26

   9814 27

   8342 28

   7248 29

   6193 30

   7666 31

   3926 32

   3139 33

   2892 34

   2235 35

   1984 36

   1937 37


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.