linux下使用 du查看某个文件或目录占用磁盘空间的大小

du -ah --max-depth=1     这个是我想要的结果  a表示显示目录下所有的文件和文件夹(不含子目录),h表示以人类能看懂的方式,max-depth表示目录的深度。

du命令用来查看目录或文件所占用磁盘空间的大小。常用选项组合为: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。
  很明显,与df不同,它用来查看文件或目录所占用的磁盘空间的大小。
  二、du常用的选项:
  -h:以人类可读的方式显示
  -a:显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘空间的大小
  -s:显示目录占用的磁盘空间大小,不要显示其下子目录和文件占用的磁盘空间大小
  -c:显示几个目录或文件占用的磁盘空间大小,还要统计它们的总和
  --apparent-size:显示目录或文件自身的大小
  -l :统计硬链接占用磁盘空间的大小
  -L:统计符号链接所指向的文件占用的磁盘空间大小
  一、du -h:这个就不多说了。
  二、du -a:使用此选项时,显示目录和目录下子目录和文件占用磁盘空间的大小。
<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>
  可以看出,默认情况下du只显示download目录占用磁盘空间的大小,而使用-a选项后,它不仅显示了目录(最后一行),而且显示了目录下各个文件占用磁盘空间的大小。
  三、du -s:使用此选项时,du只显示目录所占用磁盘空间的大小,而不显示其下子目录和文件占用磁盘空间的信息。
<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>
  默认情况下,du不显示目录下文件占用磁盘空间的信息,但它会显示其下子目录占用磁盘空间的信息;而使用-s选项以后,只显示xx目录占用磁盘空间的大小。
  四、du --apparent-size:显示文件或目录自身大小,而不是它们占用的磁盘空间大小。文件或目录占用磁盘空间的大小与它们自身大小有时候并非完全一致;这种现象非linux所独有,windows里也是如此。我们看这个选项的帮助文档的解释: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.
  这段话给出了文件或目录自身大小与占用磁盘空间大小的区别。他下面举出了一个更加夸张的例子,这里就不把它写出来了。我们知道了:wcls --block-size显示的是其自身大小,而du给出的则是占用的磁盘空间的大小。
<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>
  五、du -c:使用此选项时,不仅显示几个文件或目录各自占用磁盘空间的大小,还统计它们的总和。
<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>
如图所示,加上-c选项后,du不仅显示两个目录各自占用磁盘空间的大小,还在最后一行统计它们的总和。
  六、du -l:这个选项主要是针对硬链接。在统计目录占用磁盘空间大小时,-l选项会把硬链接也统计进来。帮助文档是这样解释的: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>
  我们在download目录下建立一个硬链接,指向本目录下的一个文件。不使用-l选项时,du发现硬链接指向本目录下的文件,于是自动忽略该链接文件,以免造成重复统计;使用-l选项,du才会把硬链接文件也统计进来。

  七、du -L:这个选项主要是针对符号链接。它会把符号链接所指向的文件占用磁盘空间的大小也统计进来。帮助文档是这样解释的: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>
   我们在download目录下创建一个符号链接,指向本目录下的一个文件。我们看到上面图片中显示的操作结果,当使用-L选项时,du会把符号链接所指向的文件的大小也统计到目录里来。
  另外我们看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

猜你喜欢

转载自m635674608.iteye.com/blog/2337145