AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1252657
Accepted
Jin Yu Chan
Jin Yu Chan
Asked: 2020-06-22 21:02:22 +0800 CST2020-06-22 21:02:22 +0800 CST 2020-06-22 21:02:22 +0800 CST

为什么目录的总 stat 块大小是目录的 ls 块大小的两倍?

  • 772

我对 Ubuntu 还很陌生,想学习更多 Linux 编程。

每次我打印ls -l并再次比较我自己的代码以添加st_blocks每个文件时,结果总是 stat 块是ls块大小的两倍?

有没有特别的解释?


在命令行中:

$ ls -ls
total 28
4 ... test
20 ... test.c
4 ... test.txt

程序总和非'。' 文件:

$ myls
total 56
8 ... test
40 ... test.c
8 ... test.txt

循环中使用的代码摘录:

...
    ...
        if (dirName[0] != '.') {
            blocksize += buf.st_blocks;
    }
    return blocksize;
...
programming command-line directory ls
  • 1 1 个回答
  • 1593 Views

1 个回答

  • Voted
  1. Best Answer
    Eliah Kagan
    2020-06-22T21:50:51+08:002020-06-22T21:50:51+08:00

    统计

    st_blocks在使用 Linux 内核的系统(包括所有 GNU/Linux 系统,例如 Ubuntu)上,用作单位的块大小始终为 512 字节。在大多数其他类 Unix 操作系统上也是如此。

    正如斯蒂芬基特在回答为什么 st_blocks 总是以 512 字节块报告时所解释的那样?:

    块的大小是特定于实现的。由于历史原因,在 Linux 上它总是 512 字节;特别是,它曾经是磁盘扇区的典型大小。

    (强调我的)

    stat(2)列出 的成员struct stat,其中包括:

    blkcnt_t  st_blocks;      /* number of 512B blocks allocated */
    

    该stat命令(参见stat(1))同样使用 512 字节的块大小。例如,正如 所揭示的ls -l,在我的系统/bin/nano上是 208480 字节大,并且stat /bin/nano包括Blocks: 408.


    ls

    ls -l默认情况下以字节为单位打印文件大小,但ls -s以 1024 字节块的形式打印它们。存在ls一些默认为 512 字节块的类 Unix 系统的实现,但 GNU ls(/bin/ls在 Ubuntu 中提供)和busybox ls(在 Ubuntu 中提供的其他ls实现)都默认为 1024 字节块。-k即使在某些其他系统上可能需要此行为,您也不必通过此行为。

    以块为单位打印文件大小时,Ubuntu 的ls命令默认为 1024 字节块,因为它们更方便。--block-size如果你愿意,你可以改变它。在实践中,我认为最流行的方法是:

    • 传递-l而不-s获取以字节为单位的大小(实际上是块大小为 1)
    • 传递-l或-s使用-h单位标签打印更多人类可读的尺寸
    • 传递-s并使用默认的 1024 字节

    某些 GNU 实用程序在POSIX 兼容模式下运行时会打印 512 字节的块。du是一个特别明显的案例;将 的输出与 的输出进行du /bin/nano比较POSIXLY_CORRECT= du /bin/nano。-sGNU ls 在传递时也有这种行为,但是带有-l和 not-s的输出以及带有 的输出-h不受影响。(没有选项的输出当然也不受影响,因为它不会打印大小。)

    除了支持之外--block-size,GNU ls 还尊重其他一些环境变量,尤其是BLOCK_SIZE. 与 不同的POSIXLY_CORRECT是,这不仅会ls -s影响. 如果出于某种原因您已设置但不想使用它,并且您想使用 1024 字节块,则传递with将覆盖它。ls -l-sBLOCK_SIZEls -s-k-s

    还有其他方法可以对此进行调整,当同时使用多种方法时会涉及一些微妙之处。有关快速详细信息,请参阅下面的示例和ls(1) ( man ls)。有关完整的详细信息,请阅读GNU coreutils 手册(也应该在本地提供:run info coreutils),尤其是第2.3 节块大小。


    ls例子

    以下是一些控制单元大小的示例,由 显示ls。

    ls -l以字节为单位打印大小:

    $ ls -l /bin/nano
    -rwxr-xr-x 1 root root 208480 Feb 15  2017 /bin/nano
    

    ls -s以 1024 字节块打印大小:

    $ ls -s /bin/nano
    204 /bin/nano
    

    添加-h到任何一个以人类可读的形式打印它:

    $ ls -lh /bin/nano
    -rwxr-xr-x 1 root root 204K Feb 15  2017 /bin/nano
    $ ls -sh /bin/nano
    204K /bin/nano
    

    传递-l并-s预先添加任何-s会自行显示的内容,而不是影响由以下生成的大小列-l:

    $ ls -ls /bin/nano
    204 -rwxr-xr-x 1 root root 208480 Feb 15  2017 /bin/nano
    $ ls -lsh /bin/nano
    204K -rwxr-xr-x 1 root root 204K Feb 15  2017 /bin/nano
    

    添加-k到上面显示的命令-s或-l不更改输出,因为 GNU ls 已经默认使用 2014 字节块-s,并且-l以字节为单位显示大小并且不受-k. 但是,-k在更复杂的情况下会产生影响(见下文)。

    添加导致大小以指定大小的块打印:--block-size=size-in-bytes

    $ ls --block-size=256 -s /bin/nano
    816 /bin/nano
    $ ls --block-size=512 -s /bin/nano
    408 /bin/nano
    $ ls --block-size=1024 -s /bin/nano
    204 /bin/nano
    

    与其他一些选项不同,该选项甚至对通常以字节显示--block-size的大小列产生这种影响:ls -l

    $ ls --block-size=256 -l /bin/nano
    -rwxr-xr-x 1 root root 815 Feb 15  2017 /bin/nano
    $ ls --block-size=512 -l /bin/nano
    -rwxr-xr-x 1 root root 408 Feb 15  2017 /bin/nano
    $ ls --block-size=1024 -l /bin/nano
    -rwxr-xr-x 1 root root 204 Feb 15  2017 /bin/nano
    $ ls -l /bin/nano
    -rwxr-xr-x 1 root root 208480 Feb 15  2017 /bin/nano
    

    --block-size不会被覆盖-k,即使-k出现在它之后:

    $ ls -s --block-size=256 -k /bin/nano
    816 /bin/nano
    $ ls -s -k --block-size=256 /bin/nano
    816 /bin/nano
    $ ls -l --block-size=256 -k /bin/nano
    -rwxr-xr-x 1 root root 815 Feb 15  2017 /bin/nano
    $ ls -l -k --block-size=256 /bin/nano
    -rwxr-xr-x 1 root root 815 Feb 15  2017 /bin/nano
    

    (我的示例使用 2 的幂,但操作数--block-size不一定是 2 的幂。此外,-s显然-l使用不同的舍入规则。)

    设置BLOCK_SIZE环境变量与传递类似的效果--block-size:

    $ BLOCK_SIZE=256 ls -s /bin/nano
    816 /bin/nano
    $ BLOCK_SIZE=512 ls -s /bin/nano
    408 /bin/nano
    $ BLOCK_SIZE=1024 ls -s /bin/nano
    204 /bin/nano
    
    $ BLOCK_SIZE=256 ls -l /bin/nano
    -rwxr-xr-x 1 root root 815 Feb 15  2017 /bin/nano
    $ BLOCK_SIZE=512 ls -l /bin/nano
    -rwxr-xr-x 1 root root 408 Feb 15  2017 /bin/nano
    $ BLOCK_SIZE=1024 ls -l /bin/nano
    -rwxr-xr-x 1 root root 204 Feb 15  2017 /bin/nano
    

    --block-size选项和 BLOCK_SIZE环境变量在效果上的区别在于BLOCK_SIZE环境变量更经常被选项覆盖。-k覆盖BLOCK_SIZE:

    $ BLOCK_SIZE=256 ls -k -s /bin/nano
    204 /bin/nano
    

    这适用于-s. 但-k不会覆盖BLOCK_SIZE由 显示的尺寸-l,仅仅是因为(如上所述)-k不会影响:

    $ BLOCK_SIZE=256 ls -k -l /bin/nano
    -rwxr-xr-x 1 root root 815 Feb 15  2017 /bin/nano
    $ BLOCK_SIZE=256 ls -kls /bin/nano
    204 -rwxr-xr-x 1 root root 815 Feb 15  2017 /bin/nano
    

    --block-size也覆盖BLOCK_SIZE. 由于--block-size同时影响-s和-l,它覆盖BLOCK_SIZE两者:

    $ BLOCK_SIZE=256 ls --block-size=512 -s /bin/nano
    408 /bin/nano
    $ BLOCK_SIZE=256 ls --block-size=512 -l /bin/nano
    -rwxr-xr-x 1 root root 408 Feb 15  2017 /bin/nano
    $ BLOCK_SIZE=256 ls --block-size=512 -ls /bin/nano
    408 -rwxr-xr-x 1 root root 408 Feb 15  2017 /bin/nano
    

    设置POSIXLY_CORRECT环境变量,即使是空字符串,也会导致ls -s使用 512 字节的块。、和选项会覆盖此效果-h,而是产生它们指定的行为。但与and不同,它仍然以字节为单位打印大小。-k--block-size--block-sizeBLOCK_SIZEls -l

    $ POSIXLY_CORRECT= ls -s /bin/nano
    408 /bin/nano
    $ POSIXLY_CORRECT= ls -sh /bin/nano
    204K /bin/nano
    $ POSIXLY_CORRECT= ls -sk /bin/nano
    204 /bin/nano
    $ POSIXLY_CORRECT= ls --block-size=256 -s /bin/nano
    816 /bin/nano
    $ POSIXLY_CORRECT= ls -l /bin/nano
    -rwxr-xr-x 1 root root 208480 Feb 15  2017 /bin/nano
    

    BLOCK_SIZE优先于POSIXLY_CORRECT:

    $ BLOCK_SIZE=256 POSIXLY_CORRECT= ls -s /bin/nano
    816 /bin/nano
    

    影响块大小的选项当然也优先于POSIXLY_CORRECT,因为POSIXLY_CORRECT只是更改默认块大小。特别是,-k覆盖POSIXLY_CORRECT:

    $ POSIXLY_CORRECT= ls -s /bin/nano
    408 /bin/nano
    $ POSIXLY_CORRECT= ls -sk /bin/nano
    204 /bin/nano
    

    ls- 其他选项

    我没有展示相关选项和环境变量的所有组合。

    此外,还有两个与 GNU ls 如何选择块大小相关的环境变量:

    • BLOCKSIZE(请注意,没有下划线)的行为类似于BLOCK_SIZEforls -s但不是ls -l。它取决于BLOCK_SIZE何时存在。
    • LS_BLOCK_SIZE表现类似BLOCK_SIZE但只影响ls而不影响duand df。如果两者LS_BLOCK_SIZE都BLOCK_SIZE设置了,LS_BLOCK_SIZE则使用。

    与其他环境变量一样,这些环境变量优先于POSIXLY_CORRECT.

    如上所述,请参阅GNU coreutils 手册中的第2.3 节块大小以获取更多信息,包括有关这些的详细信息。这本手册,您可以通过在命令行中键入 来阅读,它比coreutils 提供的手册页和其他命令详细得多。info coreutilsls

    • 2

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve