Linux ls -l 之后的第一行中的“总计”是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7401704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:10:25  来源:igfitidea点击:

What is that "total" in the very first line after ls -l?

linuxshellunixcommandprompt

提问by Sanket Sahu

What is the totalin the output of ls -l?

什么是total在输出ls -l

    $ 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

采纳答案by Mat

You can find the definition of that line in the lsdocumentation for your platform. For coreutilsls(the one found on a lot of Linux systems), the information can be found via info coreutils ls:

您可以在ls您平台的文档中找到该行的定义。对于coreutilsls(在许多 Linux 系统上找到的那个),可以通过info coreutils ls以下方式找到信息:

For each directory that is listed, preface the files with a line `total BLOCKS', where BLOCKS is the total disk allocation for all files in that directory.

对于列出的每个目录,在文件前面加上一行“total BLOCKS”,其中 BLOCKS 是该目录中所有文件的总磁盘分配。

回答by Dave Lasley

That is the total number of file system blocks, including indirect blocks, used by the listed files. If you run ls -son the same files and sum the reported numbers you'll get that same number.

这是列出的文件使用的文件系统块的总数,包括间接块。如果您运行ls -s相同的文件并对报告的数字求和,您将得到相同的数字。

回答by Tsvetomir Dimitrov

Just to mention - you can use -h (ls -lh) to convert this in human readable format.

只是提一下 - 您可以使用 -h (ls -lh) 将其转换为人类可读的格式。

回答by Don Scott

The Formula: What is thatnumber?

公式:那个数字是多少?

total int= Sum of (physical_blocks_in_use) * physical_block_size/ls_block_size)for each file.

Where:

  • ls_block_sizeis an arbitrary environment variable(normally 512 or 1024 bytes) which is freely modifiable with the --block-size=<int>flag on ls, the POSIXLY_CORRECT=1GNU environment variable (to get 512-byte units), or the -kflag to force 1kB units.
  • physical_block_sizeis the OS dependent value of an internal block interface, which may or may not be connected to the underlying hardware. This value is normally 512b or 1k, but is completely dependent on OS. It can be revealed through the %Bvalue on stator fstat. Note that this value is (almost always) unrelated to the number of physical blocks on a modernstorage device.

total int=每个文件的(physical_blocks_in_use) * physical_block_size/ls_block_size) 的总和。

在哪里:

  • ls_block_size是一个任意的环境变量(通常为 512 或 1024 字节),可以通过--block-size=<int>标记 on lsPOSIXLY_CORRECT=1GNU 环境变量(获得 512 字节单位)或-k强制 1kB 单位的标记自由修改 。
  • physical_block_size是内部块接口的操作系统相关值,它可能连接也可能不连接到底层硬件。该值通常为 512b 或 1k,但完全取决于操作系统。它可以通过或%B上的值显示出来。 请注意,此值(几乎总是)与现代存储设备上的物理块数无关statfstat

Why so confusing?

为什么这么混乱?

This number is fairly detached from any physical or meaningful metric. Many junior programmers haven't had experience with file holesor hard/sym links. In addition, the documentation available on this specific topic is virtually non-existent.

这个数字与任何物理或有意义的指标完全不同。许多初级程序员没有处理文件漏洞硬/符号链接的经验。此外,关于此特定主题的可用文档几乎不存在。

The disjointedness and ambiguity of the term "block size"has been a result of numerous different measures being easily confused, and the relatively deep levels of abstraction revolving around disk access.

术语“块大小”的脱节和歧义是由于许多不同的度量容易混淆,以及围绕磁盘访问的相对深层次的抽象。

Examples of conflicting information: du(or ls -s) vs stat

冲突信息的例子:( duls -s)vsstat

Running du *in a project folder yields the following: (Note: ls -sreturns the same results.)

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

Total: 2+2+1+3+3+5+26+4+2+14 = 62 Blocks

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

Yet when one runs statwe see a different set of values. Running statin the same directory yields:

然而,当我们运行时,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

Total:3+3+1+5+6+10+51+8+3+28 = 118 Blocks

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

Note:You can use the command stat * --printf="%b\t(%B)\t%n: %s bytes\n"> to output (in order) the number of blocks, (in parens) the size of those blocks, the name of the file, and the size in bytes, as shown above.

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

There are two important things takeaways:

有两件重要的事情外卖:

  • statreports both the physical_blocks_in_useand physical_block_sizeas used in the formula above. Note that these are values based on OS interfaces.
  • duis providing what is generally accepted as a fairly accurate estimateof physical disk utilization.
  • stat报告上面公式中使用的physical_blocks_in_usephysical_block_size。请注意,这些是基于操作系统接口的值。
  • du提供了普遍接受的对物理磁盘利用率的相当准确的估计


For reference, here is the ls -lof directory above:

作为参考,这里是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