如何以字节为单位打印文件系统的大小?

如何以字节为单位打印文件系统的大小?

问题:

我想打印文件系统的大小(以字节为单位)。我读过这个答案,但是在运行建议的命令时,它会以科学计数法打印输出:

manuel@manuel-ThinkPad-T460:~$ sudo dumpe2fs -h /dev/sdb2 |& awk -F: '/Block count/{count=$2} /Block size/{size=$2} END{print count*size}'
9.96147e+11

我尝试解决

我尝试手动调整命令并得出此命令,但它提供了错误的结果:

manuel@manuel-ThinkPad-T460:~$ sudo dumpe2fs -h /dev/sdb2 |& awk -F: '/Block count/{count=$2} /Block size/{size=$2} END{printf "%d\n", count*size}'
2147483647

附加信息

字段的值为Block size4096Block count值为243200000。乘积为996147200000

答案1

awk在 Ubuntu 的最新版本中,的默认实现是mawk,其默认数字输出格式为%.6g。您可以通过将内置变量设置OFMT为更长的格式来更改它

前任。

$ printf 'Block count: 243200000\nBlock size: 4096\n' | 
    mawk -F: '/Block count/{count=$2} /Block size/{size=$2} END{print count*size}'
9.96147e+11

$ printf 'Block count: 243200000\nBlock size: 4096\n' | 
    mawk -F: '/Block count/{count=$2} /Block size/{size=$2} END{print count*size}' OFMT='%.12g'
996147200000

或(使用你的printf方法)

$ printf 'Block count: 243200000\nBlock size: 4096\n' | 
    mawk -F: '/Block count/{count=$2} /Block size/{size=$2} END{printf "%.12g\n", count*size}'
996147200000

printf "%d\n"我认为,不起作用是因为值在内部被转换为浮点数)。


man mawk

OFMT      format for printing numbers; initially = "%.6g"

答案2

虽然不一定等于文件系统,大小文件系统所在的分区可以使用 进行打印lsblk。在大多数(但不是全部)系统中,分区和文件系统完全重叠,从而提供相同的结果。但是,这不能保证。

这是我的 /dev/sda1 分区的信息:

sudo dumpe2fs -h /dev/sda1
dumpe2fs 1.42.9 (4-Feb-2014)
Filesystem volume name:   <none>
Last mounted on:          /media/user/be816c0e-e757-4b20-8bef-5ce79854eb77
Filesystem UUID:          be816c0e-e757-4b20-8bef-5ce79854eb77
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              819200
Block count:              3251200
Reserved block count:     162559
Free blocks:              1040435
Free inodes:              328525
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      1023
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8192
Inode blocks per group:   512
RAID stride:              32717
Flex block group size:    16
Filesystem created:       Mon Mar 31 11:36:45 2014
Last mount time:          Mon Jan 30 15:26:07 2017
Last write time:          Mon Jan 30 15:26:07 2017
Mount count:              175
Maximum mount count:      -1
Last checked:             Wed Sep  9 17:58:29 2015
Check interval:           0 (<none>)
Lifetime writes:          34 GB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:           256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      8a419c50-75fe-41af-8492-96a2cc20cdb5
Journal backup:           inode blocks
Journal features:         journal_incompat_revoke
Journal size:             128M
Journal length:           32768
Journal sequence:         0x00015415
Journal start:            0

比较:

echo -e 'Block count:3251200\nBlock size: 4096\n' |      mawk -F: '/Block count/{count=$2} /Block size/{size=$2} END{print count*size}' OFMT='%.12g'

13316915200

$ lsblk --noheadings -b /dev/sda1 -o size

13316915200

正如你所见,更少的按键提供了完全相同的结果无管道。

来源man lsblk

相关内容