linux命令du和df的区别(Linux系统目录大小通过du命令获取实例)
类别:服务器 浏览量:338
时间:2021-11-05 14:26:43 linux命令du和df的区别
Linux系统目录大小通过du命令获取实例使用过 Linux 系统的小伙伴都知道应该都知道, Linux 系统下的 ls 命令通常被我们用来查看文件目录的内容,但是不知道大家有没有细心留意,ls 命令所显示出来的每一个目录的大小竟然都只有 4 KB ?
$ ls -lh | grep ^d drwxr-xr-x 3 alvin alvin 4.0K Aug 2 13:57 Bank_Details drwxr-xr-x 2 alvin alvin 4.0K Mar 15 2019 alvin drwxr-xr-x 6 alvin alvin 4.0K Feb 16 2019 drive-alvin drwxr-xr-x 13 alvin alvin 4.0K Jan 6 2019 drive-mageshm drwxr-xr-x 15 alvin alvin 4.0K Sep 29 21:32 Thanu_Photos
其实啊,这是因为 Linux 下一切皆文件,相信大家都听过这句话,所以你看到的 4 KB 只是用于存储目录元信息的文件大小而已,并不是我们通常意义上的目录大小。那么问题来了,如何去获取到文件目录的实际大小呢?别急,Linux 下的 du 命令可以帮到你。
du 是英文 disk usage 的缩写,代表磁盘使用率,它是一个标准的 Unix 程序,用于估算当前工作目录中文件空间的使用情况。
下面将以 /home/alvin/Documents 为例,来介绍 du 命令的使用。
查看指定目录的总大小
$ du -hs /home/alvin/Documents or $ du -h --max-depth=0 /home/alvin/Documents/ 20G /home/alvin/Documents
上面两种方式都可以算出目标目录的总大小,其中:
- -h 表示显示成 K、M、G 等大小格式
- -s 表示显示总和
- --max-depth=N 表示目录级别为 N
查看指定目录下每个目录(包括子目录)的大小
将 -s 选项去掉,不进行统计总和,就能显示每一个目录的大小了,以下仅显示前 20 条:
$ du -h /home/alvin/Documents/ | sort -rh | head -20 20G /home/alvin/Documents/ 9.6G /home/alvin/Documents/drive-alvin 6.3G /home/alvin/Documents/Thanu_Photos 5.3G /home/alvin/Documents/Thanu_Photos/Camera 5.3G /home/alvin/Documents/drive-alvin/Thanu-videos 3.2G /home/alvin/Documents/drive-mageshm 2.3G /home/alvin/Documents/drive-alvin/Thanu-Photos 2.2G /home/alvin/Documents/drive-alvin/Thanu-photos-by-month 916M /home/alvin/Documents/drive-mageshm/Tanisha 454M /home/alvin/Documents/drive-mageshm/2g-backup 415M /home/alvin/Documents/Thanu_Photos/WhatsApp Video 300M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017 288M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017 226M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017 219M /home/alvin/Documents/Thanu_Photos/WhatsApp Documents 213M /home/alvin/Documents/drive-mageshm/photos 163M /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent 161M /home/alvin/Documents/Thanu_Photos/WhatsApp Images 154M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017 150M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016
查看指定目录下每个文件和目录的大小
结合通配符 * 的使用,可以实现显示出指定目录下的文件和目录的大小信息的效果:
$ du -hs /home/alvin/Documents/* | sort -rh | head -10 9.6G /home/alvin/Documents/drive-alvin 6.3G /home/alvin/Documents/Thanu_Photos 3.2G /home/alvin/Documents/drive-mageshm 756K /home/alvin/Documents/Bank_Details 272K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 172K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png 164K /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 132K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png 112K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png 96K /home/alvin/Documents/distro-info.xlsx
查看指定目录下的每个目录的大小(排除子目录)
使用 -S 选项,以排除子目录的形式来统计大小:
$ du -hS /home/alvin/Documents/ | sort -rh | head -20 5.3G /home/alvin/Documents/Thanu_Photos/Camera 5.3G /home/alvin/Documents/drive-alvin/Thanu-videos 2.3G /home/alvin/Documents/drive-alvin/Thanu-Photos 1.5G /home/alvin/Documents/drive-mageshm 831M /home/alvin/Documents/drive-mageshm/Tanisha 454M /home/alvin/Documents/drive-mageshm/2g-backup 300M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017 288M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017 253M /home/alvin/Documents/Thanu_Photos/WhatsApp Video 226M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017 219M /home/alvin/Documents/Thanu_Photos/WhatsApp Documents 213M /home/alvin/Documents/drive-mageshm/photos 163M /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent 154M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017 150M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016 127M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2016 100M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2016 94M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2017 92M /home/alvin/Documents/Thanu_Photos/WhatsApp Images 90M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2017
查看指定目录中的一级子目录的大小
这个就很简单了,将 max-depth 设定为 1 就可以了:
$ du -h --max-depth=1 /home/alvin/Documents/ 3.2G /home/alvin/Documents/drive-mageshm 4.0K /home/alvin/Documents/alvin 756K /home/alvin/Documents/Bank_Details 9.6G /home/alvin/Documents/drive-alvin 6.3G /home/alvin/Documents/Thanu_Photos 20G /home/alvin/Documents/
查看指定目录的大小(包含统计功能)
使用 -c 选项能够对结果进行统计,可以看到,指定目录下的文件和目录的大小的总和是 20 G,这和上面第一条命令中所得出的 /home/alvin/Documents/ 的大小 20 G 是一致的:
$ du -hsc /home/alvin/Documents/* | sort -rh | head -10 20G total 9.6G /home/alvin/Documents/drive-alvin 6.3G /home/alvin/Documents/Thanu_Photos 3.2G /home/alvin/Documents/drive-mageshm 756K /home/alvin/Documents/Bank_Details 272K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 172K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png 164K /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 132K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png 112K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png
到此这篇关于Linux系统目录大小通过du命令获取实例的文章就介绍到这了,更多相关用du命令获取Linux系统目录大小内容请搜索开心学习网以前的文章或继续浏览下面的相关文章希望大家以后多多支持开心学习网!
您可能感兴趣
- gitlablinux安装配置(Ubuntu20.04安装配置GitLab的方法步骤)
- 护卫神主机大师教程(护卫神主机大师Linux版安装及卸载图文教程)
- linux内核从原理到代码详解(探究一个LED如何入门Linux内核)
- linux下如何启动phpstudy面板(详解phpstudy linux web面板小皮面板V0.2版本正式发布)
- linux机器重启了docker容器还在不(Linux下docker 容器退出bash的两种实现方法)
- linux rsync 迁移数据(如何使用Linux的rsync)
- 宝塔面板程序目录(将宝塔面板linux版装在/www以外的目录的方法)
- linux虚拟主机怎么设置(linux系统虚拟主机开启支持Swoole Loader扩展的方法)
- linux idea怎么激活(在IDEA中使用Linux命令的操作方法)
- linux怎么设置php代码(linux Cron定时执行带参数的PHP代码)
- linux系统内核参数优化(Linux 系统优化的一些建议内核优化)
- linux部署flask项目(用uWSGI和Nginx部署Flask项目的方法示例)
- centos linux开机不出现登录页面(在CentOS启动时自动加载内核模块overlayfs操作)
- linux系统的组成架构(Linux输入子系统框架原理解析)
- centos7宝塔面板离线安装(linux centos 宝塔面板和安全狗安装过程记录)
- linux如何使用gdb调试(Linux下如何使用gdb调试core文件)
- 大女主 汤唯垂青电视圈,搭档朱亚文出演《大明皇妃孙若微传》(汤唯垂青电视圈)
- 红色代表什么(红色代表什么情感和含义)
- 高中数学题(高中数学题型总结及解题方法)
- 冰岛旅游攻略(冰岛旅游攻略及花费)
- 为什么现在年轻人越来越喜欢买衣服(为什么现在年轻人越来越喜欢买衣服穿)
- 怎么做好SEO(怎么做好seo内容优化)
热门推荐
- zabbix5.4监控磁盘可用内存大小(解析Zabbix 5.0磁盘自动发现和读写监控的问题)
- dedecms二级菜单(dedecms5.7使tag调用的标签正序排列的方法)
- wampserver安装报错(安装wampserver提示丢失MSVCR100.dll的解决方法)
- SQL中patindex的用法
- python 绕过密码(一小段Python代码,破解加密zip文件的密码)
- docker镜像配置的挂载路径(解决docker挂载的目录无法读写问题)
- cssbackground(用 CSS background 实现刻度线的呈现)
- vuex中action的值怎么接(vuex中Getter的用法详解)
- pythonrequests怎么导入模块(Python3使用requests模块实现显示下载进度的方法详解)
- python语言案例教程单元测试(Python单元和文档测试实例详解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9