Use du under linux to view the size of disk space occupied by a file or directory

du -ah --max-depth=1 This is the result I want a means to display all files and folders in the directory (excluding subdirectories), h means in a way that humans can understand, max-depth means the directory depth.

The du command is used to view the amount of disk space occupied by a directory or file. Commonly used option combinations are: du -sh

  一、du的功能:`du` reports the amount of disk space used by the specified files and for each subdirectory (of directory arguments). with no arguments,`du` reports the disk space for the current directory。
  Obviously, unlike df , it is used to see the size of the disk space occupied by a file or directory.
  Second, the common options of du :
  -h : display in human readable form
  -a : Display the size of the disk space occupied by the directory, and also display the size of the disk space occupied by the directories and files under it
  -s : Display the size of the disk space occupied by the directory, do not display the size of the disk space occupied by the subdirectories and files under it
  -c : Displays the size of disk space occupied by several directories or files, and also counts their sum
  --apparent-size : Display the size of the directory or file itself
  -l  : Count the size of disk space occupied by hard links
  -L : Count the amount of disk space occupied by the file pointed to by the symbolic link
  1. du -h : Not much to say about this.
  2. du -a : When using this option, display the size of the disk space occupied by the subdirectories and files under the directory and directory.
<iframe id="iframe_0.7478217754978687" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 413px; height: 195px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://d.hiphotos.baidu.com/album/pic/item/aec379310a55b319c03a678543a98226cefc177a.jpg?_=3825461%22%20style=%22border:none;max-width:1209px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7478217754978687',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
  It can be seen that by default du only displays the size of the disk space occupied by the download directory, but after using the -a option, it not only displays the directory (the last line), but also displays the size of the disk space occupied by each file in the directory.
  3. du -s : When using this option, du only displays the size of the disk space occupied by the directory, and does not display the information about the disk space occupied by the subdirectories and files under it.
<iframe id="iframe_0.42043043696321547" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 266px; height: 135px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://d.hiphotos.baidu.com/album/pic/item/a8ec8a13632762d0ac34404fa0ec08fa513dc63c.jpg?_=3825461%22%20style=%22border:none;max-width:1209px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.42043043696321547',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
  By default, du does not display the information about the disk space occupied by the files in the directory, but it will display the information about the disk space occupied by the subdirectories under it; after using the -s option, only the size of the disk space occupied by the xx directory is displayed.
  Fourth, du --apparent-size : Display the size of the file or directory itself, rather than the size of the disk space they occupy. The size of the disk space that a file or directory occupies is sometimes not exactly the same as its own size; this phenomenon is not unique to linux , but also to windows . Let's look at the explanation of this option's help documentation: The apparent size of a file is the number of bytes reported by `wc –c` 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.
  This paragraph gives the difference between the size of a file or directory itself and the size of the disk space it occupies. He gives a more exaggerated example below, which I will not write here. We know: wc or ls --block-size shows its own size, while du gives the size of the occupied disk space.
<iframe id="iframe_0.475971368374303" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 404px; height: 68px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://b.hiphotos.baidu.com/album/pic/item/b03533fa828ba61e49372ddf4134970a314e5961.jpg?_=3825461%22%20style=%22border:none;max-width:1209px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.475971368374303',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
  5. du -c : When using this option, it not only displays the size of the disk space occupied by several files or directories, but also counts their sum.
<iframe id="iframe_0.8080178210511804" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 350px; height: 115px;" src="data:text/html; charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://c.hiphotos.baidu.com/album/pic/item/4bed2e738bd4b31cc5ff714b87d6277f9f2ff87b.jpg?_=3825461%22%20style=%22border: none;max-width:1209px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent. postMessage(%7BiframeId:'iframe_0.8080178210511804',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder ="0" scrolling="no"></iframe>
As shown in the figure, after adding the -c option, du not only displays the size of the disk space occupied by the two directories, but also counts their sum on the last line.
  Six, du -l : This option is mainly for hard links. When calculating the size of the disk space occupied by the directory, the -l option will also count the hard links. The help documentation explains it this way: Count the size of all files,even if they have appeared already(as a hard link) .
<iframe id="iframe_0.6218455212656409" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 624px; height: 117px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://a.hiphotos.baidu.com/album/pic/item/908fa0ec08fa513d468e882e3d6d55fbb2fbd91e.jpg?_=3825461%22%20style=%22border:none;max-width:1209px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6218455212656409',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
  We create a hard link in the download directory, pointing to a file in this directory. When the -l option is not used, du finds that the hard link points to the file in this directory, so the linked file is automatically ignored to avoid duplicate statistics; when the -l option is used, du will also count the hard link files.

  Seven, du -L : This option is mainly for symbolic links. It will also count the amount of disk space occupied by the file pointed to by the symbolic link. The help documentation explains it this way: Dereference symbolic links(show the disk space used by the file or directory that the link points to instead of the space used by the link) .

<iframe id="iframe_0.023297965060919523" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 654px; height: 147px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://c.hiphotos.baidu.com/album/pic/item/09fa513d269759ee5f74843bb2fb43166d22df1e.jpg?_=3825461%22%20style=%22border:none;max-width:1209px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.023297965060919523',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
   We create a symbolic link in the download directory, pointing to a file in this directory. We see the result of the operation shown in the picture above. When the -L option is used, du will also count the size of the file pointed to by the symbolic link into the directory.
  另外我们看du -lh的结果,与du -h相同,好像没有统计出符号链接的大小;其实并非如此。从实际操作来看,符号链接本身似乎并不占用磁盘空间,所以才出现上述情况。
<iframe id="iframe_0.9980598026886582" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 349px; height: 38px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://h.hiphotos.baidu.com/album/pic/item/63d0f703918fa0ec570481fd269759ee3c6ddb7b.jpg?_=3825461%22%20style=%22border:none;max-width:1209px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9980598026886582',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
  最后,du命令在统计目录占用磁盘空间时,默认不统计链接文件(无论是硬链接或是符号链接),所以要用-l-L选项特地指出来;此外,du命令默认不显示目录下文件占用磁盘空间的信息,但是却默认显示其下子目录所占用磁盘空间的信息。怪哉。

 

du -sh : 查看当前目录总共占的容量。而不单独列出各子项占用的容量 

 

du -lh --max-depth=1 : 查看当前目录下一级子文件和子目录占用的磁盘容量。

 

http://www.cnblogs.com/kobe8/p/3825461.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326412704&siteId=291194637