爲什麼du命令與xwindows或者ls顯示的大小不一致

在man du命令給出的命令詳解是這樣的,有兩個選項,一個是

--apparent-size

              print apparent sizes,  rather  than  disk  usage;  although  the
              apparent  size is usually smaller, it may be larger due to holes
              in (`sparse') files, internal  fragmentation,  indirect  blocks,
              and the like

       -S, --separate-dirs
              do not include size of subdirectories

       -s, --summarize

              display only a total for each argument

-a, --all
              write counts for all files, not just directories


即-s命令是顯示整個磁盤使用情況,或者爲一個目錄中的所有文件 顯示總的磁盤使用情況。將該標誌與-a標誌進行對比。

-a顯示文件,遞歸的顯示目錄下所有的文件。-s只顯示本目錄下的文件大小和文件夾的總大小。

而默認du顯示的disk usage,即佔用的blocksize,blocksize可以是1k也可以是4k,不能整除的上取整,就是就算佔用半個block也會算作一個大小。

apparent size是實際大小,有的文件中有空洞,空洞不佔用磁盤block,但是有大小,即holes,內部碎片fragmentation和indirect blocks不知道是什麼情況,找了兩張圖片放在下面

External and Internal Fragmentation

External fragmentation is the phenomenon in which free storage becomes divided into many small pieces over time. It occurs when an application allocates and deallocates regions of storage of varying sizes, and the allocation algorithm responds by leaving the allocated and deallocated regions interspersed. The result is that although free storage is available, it is effectively unusable because it is divided into pieces that are too small to satisfy the demands of the application.

Internal fragmentation is the space wasted inside of allocated memory blocks because of restriction on the allowed sizes of allocated blocks.




'--apparent-size'
Print apparent sizes, rather than disk usage. The apparent size of a file is the number of bytes reported by wc -c on regular files, or more generally, ls -l --block-size=1 or stat --format=%s. For example, a file containing the word 'zoo' with no newline would, of course, have an apparent size of 3. Such a small file may require anywhere from 0 to 16 KiB or more of disk space, depending on the type and configuration of the file system on which the file resides. However, a sparse file created with this command:

      dd bs=1 seek=2GiB if=/dev/null of=big

has an apparent size of 2 GiB, yet on most modern systems, it actually uses almost no disk space.

這是國外一個論壇的帖子給出的一個解釋
發佈了23 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章