linux bash 向文件回显 4096 字节,文件在磁盘上使用 8 字节?

liunx bash echo 4096 bytes to a file, file using 8 bytes on disk?

当我在 liunx 终端上使用 bash 编写临时测试文件时。

echo text_content>file1

if set length of text_content 4096 char(random char from [a-Z]) long。 结果 file1 最终使用了两个 4K 块。和一个索引节点。

test@instance-7:~/notes/rust$ du -csh file1
8.0K    file1
8.0K    totaldu

但是为什么用了两个4K块呢?我的意思是,一个 4K 块还不够吗?

如果我设置 text_content 4095 char long 的长度,它只使用一个 4K 块。

为什么它需要使用更多的块?或者我遗漏了什么?

这是我的 liunx 机器的一些磁盘信息。

test@instance-7:~/notes/rust$ sudo fdisk -l /dev/sda

Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: PersistentDisk  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gptDisk identifier: 35BD657D-931E-497E-A86C-D3D7C4F6BD2A

试试这个实验:

  • 写入 cat > file1 并按 Enter,
  • 然后写 this 然后按 Ctrl-D 两次 而不是先按 Enter;
  • 写入 cat > file2 并按 Enter,
  • 然后写 this,然后按 Enter 然后按 Ctrl-D.
  • 最后 运行 diff file1 file2ls -l file[12]