ls -l之后的第一行中的“total”是什么?

ls -l输出中的“total”是什么?

$ ls -l /etc total 3344 -rw-r--r-- 1 root root 15276 Oct 5 2004 a2ps.cfg -rw-r--r-- 1 root root 2562 Oct 5 2004 a2ps-site.cfg drwxr-xr-x 4 root root 4096 Feb 2 2007 acpi -rw-r--r-- 1 root root 48 Feb 8 2008 adjtime drwxr-xr-x 4 root root 4096 Feb 2 2007 alchemist 

您可以在您的平台的ls文档中find该行的定义。 对于coreutils ls (在许多Linux系统上find的),可以通过info coreutils lsfind这些info coreutils ls

对于列出的每个目录,以“total BLOCKS”行作为序言,其中BLOCKS是该目录中所有文件的总磁盘分配。

这是列出的文件使用的文件系统块(包括间接块)的总数。 如果你在相同的文件上运行ls -s并且汇总报告的数字,你会得到相同的数字。

公式: 这个数字是多less?

total int = (physical_blocks_in_use)* physical_block_size / ls_block_size)的总和。

哪里:

  • ls_block_size是一个任意的环境variables (一般为512或1024字节),可以用ls上的--block-size=<int>标志自由修改, POSIXLY_CORRECT=1 GNU环境variables(获得512字节单位),或者-k标志强制1kB单位。
  • physical_block_size是内部块接口的操作系统相关值,可能连接或不连接到底层硬件。 这个值通常是512b或1k,但完全依赖于操作系统。 它可以通过statfstat上的%Bfstat请注意,此值(几乎总是)与现代存储设备上物理块的数量无关。

为什么这么混乱?

这个数字与任何物理或有意义的度量都相当分离。 许多初级程序员还没有文件洞或硬/ sym链接的经验 。 另外,关于这个特定主题的文档实际上是不存在的。

“块大小”这个术语的不连贯性和含糊性是许多不同的措施容易混淆的结果,以及围绕磁盘访问的相对较深层次的抽象。

冲突信息的例子: du (或ls -s )vs stat

在项目文件夹中运行du *产生以下结果:( 注意: ls -s返回相同的结果。)

 dactyl:~/p% du * 2 check.cc 2 check.h 1 DONE 3 Makefile 3 memory.cc 5 memory.h 26 p2 4 p2.cc 2 stack.cc 14 stack.h 

总计 :2 + 2 + 1 + 3 + 3 + 5 + 26 + 4 + 2 + 14 = 62块

然而,当我们运行stat我们会看到一组不同的值。 在同一目录下运行stat产生:

 dactyl:~/p% stat * --printf="%b\t(%B)\t%n: %s bytes\n" 3 (512) check.cc: 221 bytes 3 (512) check.h: 221 bytes 1 (512) DONE: 0 bytes 5 (512) Makefile: 980 bytes 6 (512) memory.cc: 2069 bytes 10 (512) memory.h: 4219 bytes 51 (512) p2: 24884 bytes 8 (512) p2.cc: 2586 bytes 3 (512) stack.cc: 334 bytes 28 (512) stack.h: 13028 bytes 

总计: 3 + 3 + 1 + 5 + 6 + 10 + 51 + 8 + 3 + 28 = 118块

注意:您可以使用命令stat * --printf="%b\t(%B)\t%n: %s bytes\n" >输出(按顺序)块的数量,(在parens中)这些块的大小,文件的名称,以字节为单位的大小,如上所示。

有两件重要的事情要做:

  • stat报告上面公式中使用的physical_blocks_in_usephysical_block_size 。 请注意,这些是基于操作系统界面的值。
  • du提供了通常被接受的相当准确的物理磁盘利用率估计

作为参考,这里是上面的目录的ls -l

 dactyl:~/p% ls -l **total 59** -rw-r--r--. 1 dhs217 grad 221 Oct 16 2013 check.cc -rw-r--r--. 1 dhs217 grad 221 Oct 16 2013 check.h -rw-r--r--. 1 dhs217 grad 0 Oct 16 2013 DONE -rw-r--r--. 1 dhs217 grad 980 Oct 16 2013 Makefile -rw-r--r--. 1 dhs217 grad 2069 Oct 16 2013 memory.cc -rw-r--r--. 1 dhs217 grad 4219 Oct 16 2013 memory.h -rwxr-xr-x. 1 dhs217 grad 24884 Oct 18 2013 p2 -rw-r--r--. 1 dhs217 grad 2586 Oct 16 2013 p2.cc -rw-r--r--. 1 dhs217 grad 334 Oct 16 2013 stack.cc -rw-r--r--. 1 dhs217 grad 13028 Oct 16 2013 stack.h 

只要提及 – 您可以使用-h(ls -lh)将其转换为可读的格式。