hackpro
V2EX  ›  Apple

OS X 下如何正确获取目录文件的大小

  •  
  •   hackpro · Jun 3, 2016 · 2506 views
    This topic created in 3703 days ago, the information mentioned may be changed or developed.

    想通过命令行方式获取 OSX 下目录的大小,注意是只获取目录文件的大小 发现 OSX 下命令行获取的目录大小和 Finder 查看的结果不一致

    CLI 命令为

    find . -maxdepth 1 -type d -mindepth 1 -exec du -hs {} \;
    

    结果为:

    请问各位是如何解决的

    10 replies    2016-06-09 23:43:46 +08:00
    MartinWu
        1
    MartinWu  
       Jun 3, 2016 via iPhone
    在 du 里,单位是 1024 进位的,在 finder 里,是 1000 的。实际上它们统计出来的字节数是一样的。
    hackpro
        2
    hackpro  
    OP
       Jun 3, 2016
    @MartinWu 但是只看 byte 的话应该是一样的才对啊 用下面的命令还是不一样😰
    find . -type f -print0 | barges -0 stat -f%z | awk '{b+=$1} END {print b}'
    cxbig
        3
    cxbig  
       Jun 3, 2016
    把 du 命令里的-h 去掉不就行了?
    hackpro
        4
    hackpro  
    OP
       Jun 3, 2016
    @cxbig 还是不对, 30236088
    cxbig
        5
    cxbig  
       Jun 3, 2016
    @hackpro 计算方式是不一样的,实际占用大小以 du 的为准
    hackpro
        6
    hackpro  
    OP
       Jun 3, 2016
    @cxbig 感谢 尽管目前还是未能找到理想的解决方案
    cxbig
        7
    cxbig  
       Jun 3, 2016
    @hackpro 你的需求是什么?为什么想让这 2 个地方的数据一致?还是说只是好奇背后的根源?
    hackpro
        8
    hackpro  
    OP
       Jun 3, 2016
    @cxbig 需要本来希望通过命令行能获取任意文件夹的大小 但是获取目录大小和系统有出入
    所以用户肯定会怀疑获取的方式是否有问题啊……
    cxbig
        9
    cxbig  
       Jun 3, 2016
    @hackpro 了解,我一般这么算
    du -s 某目录,结果是 26376 ,我的文件系统 Block Size=4096
    26376 * 4096 / 8 = 13.504512 MB (不是 MiB )
    用 Finder 查看该目录是 (13.5 MB on disk)
    hackpro
        10
    hackpro  
    OP
       Jun 9, 2016
    @cxbig 太厉害了 感谢大神
    完整解决方案如下
    ```
    diskutil info / | grep "Allocation Block Size"
    Allocation Block Size: 4096 Bytes

    du -s ~/Documents
    30251656 Documents
    ```
    30251656*4096/8=15488847872=15.49MB ,和 Finder 给出结果一致
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5591 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 03:40 · PVG 11:40 · LAX 20:40 · JFK 23:40
    ♥ Do have faith in what you're doing.