ls 与 du查看到的磁盘空间不一样,差别很大

 

ls   查看到的是文件逻辑上占用的空间,

du 查看到的是文件物理上占用的块大小。

 

ls与du查看到文件大小不一致,这涉及到了一个概念:稀疏文件(sparse file)

稀疏文件可能没有实际分配到用来存储用户数据的磁盘空间。

Sparse File就是在文件中留有很多空余空间,留备将来插入数据使用。如果这些空余空间被ASCII码的NULL字符占据,并且这些空间相当大,那么,这个文件就被称为稀疏文件,而且,并不分配相应的磁盘块。

 

 

[root@localhost test]# echo 123 > test.txt  #创建一个可读文件
[root@localhost test]# cat test.txt  #验证
123
[root@localhost test]# ll test.txt   #查看文件信息
-rw-r--r-- 1 root root 4 Jan 12 19:59 test.txt
[root@localhost test]# ll test.txt|awk '{print $5}'  #文件大小为4 byte
4
[root@localhost test]# du -sh test.txt  #文件占用块大小为4kb(最小存储单元)
4.0K    test.txt

[root@localhost test]# dd if=/dev/zero of=test.txt bs=1M seek=5 count=10  #文件从第5M的位置开始,写入大小为10M块数据
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.0159494 s, 657 MB/s
[root@localhost test]# cat test.txt   #文件头部信息完好,此时文件依旧可读
123
[root@localhost test]# ll test.txt|awk '{print $5}' #ll查看文件大小,为15M
15728640
[root@localhost test]# ll test.txt|awk '{print $5/1024/1024 "M"}'
15M
[root@localhost test]# du -sh test.txt #du查看文件大小为11M  ?
11M     test.txt
[root@localhost test]# du -k  test.txt  #10244k=10M+4k,没问题了,du的human-readable会进一取整
10244   test.txt

 

posted @ 2024-01-15 11:14  咿呀哒喏  阅读(180)  评论(0编辑  收藏  举报