我想测量在 Linux 中的文件中存储某些信息时所需的字节数。我开始知道字符集用于决定在文件中存储信息时使用多少字节。我尝试了以下步骤来查看使用的字节数,但我不明白为什么我的文件大小每次都会增加一倍。
1. touch 1 - create a file with name 1
[root@pgsql1 iotest]# touch 1
character set and file encoding:
[root@pgsql1 iotest]# file -bi 1
text/plain; charset=us-ascii
[root@pgsql1 iotest]# enca -L none 1
7bit ASCII characters
[root@pgsql1 iotest]# echo $LANG
en_US.UTF-8
-----------
2. stat 1 - output below
[root@pgsql1 iotest]# stat 1
File: ‘1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 831h/2097d Inode: 12 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-09-29 19:18:55.257153854 +0530
Modify: 2024-09-29 19:18:55.257153854 +0530
Change: 2024-09-29 19:18:55.257153854 +0530
Birth: -
[root@pgsql1 iotest]#
3. echo "1" > 1
[root@pgsql1 iotest]# echo "1" > 1
4. stat 1 - output below
[root@pgsql1 iotest]# stat 1
File: ‘1’
Size: 2 Blocks: 8 IO Block: 4096 regular file
Device: 831h/2097d Inode: 12 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-09-29 19:18:55.257153854 +0530
Modify: 2024-09-29 19:20:02.806425277 +0530
Change: 2024-09-29 19:20:02.806425277 +0530
Birth: -
5. echo "1" >> 1
[root@pgsql1 iotest]# echo "1" >> 1
6. stat 1
[root@pgsql1 iotest]# stat 1
File: ‘1’
Size: 4 Blocks: 8 IO Block: 4096 regular file
Device: 831h/2097d Inode: 12 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-09-29 19:18:55.257153854 +0530
Modify: 2024-09-29 19:21:03.907575276 +0530
Change: 2024-09-29 19:21:03.907575276 +0530
Birth: -
但根据https://mothereff.in/byte-counter#:~:text=An%20on-the-fly%20UTF-8%20byte%20counter,我可以看到“1”显示1个字节在尺寸上。但在统计中,文件大小每次都会增加一倍,请有人解释一下存储分配是如何发生的。
测量在文件中存储某些信息时所需的字节数
使用
wc
获取文件中的字节数。
为什么我的文件大小每次都会增加一倍。
echo "1"
写入 1
和换行符。那是两个字符,那是两个字节。