【Linux】du命令

本文详细介绍了Linux下du命令的使用方法及各种参数的功能,包括查看目录使用情况、转换显示单位、统计特定目录空间占用等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

du即Disk Usage,用来获取磁盘的使用情况的,下面分别介绍几个常用的命令参数

1. 查看给定目录树各个目录的使用情况

[root@localhost redis-5.0.4]# pwd
/root/redis-5.0.4
[root@localhost redis-5.0.4]# du 
44	./deps/hiredis/adapters
40	./deps/hiredis/examples
1296	./deps/hiredis
368	./deps/jemalloc/bin
88	./deps/jemalloc/build-aux
324	./deps/jemalloc/doc
684	./deps/jemalloc/include/jemalloc/internal

2. 增加-h选项,磁盘的使用转化为容易理解的大小表示方式

[root@localhost redis-5.0.4]# du -h
44K	./deps/hiredis/adapters
40K	./deps/hiredis/examples
1.3M	./deps/hiredis
368K	./deps/jemalloc/bin
88K	./deps/jemalloc/build-aux
324K	./deps/jemalloc/doc
684K	./deps/jemalloc/include/jemalloc/internal
768K	./deps/jemalloc/include/jemalloc

3. -s用来获取特定目录使用的总空间

[root@localhost redis-5.0.4]# pwd
/root/redis-5.0.4
[root@localhost redis-5.0.4]# du -sh .
117M	.

4. -a显示所有的文件和目录的磁盘使用情况

默认情况下只显示目录的,不包含文件

[root@localhost redis-5.0.4]# du -ah
4.0K    ./.gitignore
100K    ./00-RELEASENOTES
4.0K    ./BUGS
4.0K    ./CONTRIBUTING
4.0K    ./COPYING
4.0K    ./INSTALL
8.0K    ./MANIFESTO
4.0K    ./Makefile
24K     ./README.md
4.0K    ./deps/Makefile
8.0K    ./deps/README.md
4.0K    ./deps/hiredis/.gitignore
4.0K    ./deps/hiredis/.travis.yml
8.0K    ./deps/hiredis/CHANGELOG.md

5. -k控制显示制输出的磁盘使用k为单位

[root@localhost redis-5.0.4]# du -sk .
119504	.
[root@localhost redis-5.0.4]# 

6. -m控制显示输出的磁盘使用M为单位

[root@localhost /]# du -m -d 1 /
126	/boot
0	/dev
du: cannot access ‘/proc/40904/task/40904/fd/3’: No such file or directory
du: cannot access ‘/proc/40904/task/40904/fdinfo/3’: No such file or directory
du: cannot access ‘/proc/40904/fd/4’: No such file or directory
du: cannot access ‘/proc/40904/fdinfo/4’: No such file or directory
0	/proc
9	/run
0	/sys
41	/etc
119	/root
1608	/var
9	/tmp
4114	/usr
268	/home
0	/media
0	/mnt
0	/opt
0	/srv
6290	/

7. -c用来输出给定目录的总的使用情况,结果在最后两行显示

[root@localhost redis-5.0.4]# du -ch .
...
8.0K	./utils/hashtable
12K	./utils/hyperloglog
20K	./utils/lru
20K	./utils/releasetools
136K	./utils
117M	.
117M	total
[root@localhost redis-5.0.4]# 

8. --exclude用来剔除符合给定pattern的文件

[root@localhost integration]# ll
total 100
-rw-rw-r--. 1 root root  1302 Mar 18 12:21 aof-race.tcl
-rw-rw-r--. 1 root root  8857 Mar 18 12:21 aof.tcl
-rw-rw-r--. 1 root root  2079 Mar 18 12:21 block-repl.tcl
-rw-rw-r--. 1 root root  1305 Mar 18 12:21 convert-zipmap-hash-on-load.tcl
-rw-rw-r--. 1 root root   824 Mar 18 12:21 logging.tcl
-rw-rw-r--. 1 root root  2626 Mar 18 12:21 psync2-reg.tcl
-rw-rw-r--. 1 root root  9394 Mar 18 12:21 psync2.tcl
-rw-rw-r--. 1 root root  4335 Mar 18 12:21 rdb.tcl
-rw-rw-r--. 1 root root  6018 Mar 18 12:21 redis-cli.tcl
-rw-rw-r--. 1 root root  3170 Mar 18 12:21 replication-2.tcl
-rw-rw-r--. 1 root root  5036 Mar 18 12:21 replication-3.tcl
-rw-rw-r--. 1 root root  5194 Mar 18 12:21 replication-4.tcl
-rw-rw-r--. 1 root root  5032 Mar 18 12:21 replication-psync.tcl
-rw-rw-r--. 1 root root 11539 Mar 18 12:21 replication.tcl
[root@localhost integration]# du -ah --exclude="redis*" .
4.0K	./aof-race.tcl
12K	./aof.tcl
4.0K	./block-repl.tcl
4.0K	./convert-zipmap-hash-on-load.tcl
4.0K	./logging.tcl
4.0K	./psync2-reg.tcl
12K	./psync2.tcl
8.0K	./rdb.tcl
4.0K	./replication-2.tcl
8.0K	./replication-3.tcl
8.0K	./replication-4.tcl
8.0K	./replication-psync.tcl
12K	./replication.tcl
96K	.
[root@localhost integration]# 

9 --time输出文件或目录的修改时间

[root@localhost integration]# du -ha --time .
4.0K	2019-03-18 12:21	./aof-race.tcl
12K	2019-03-18 12:21	./aof.tcl
4.0K	2019-03-18 12:21	./block-repl.tcl
4.0K	2019-03-18 12:21	./convert-zipmap-hash-on-load.tcl
4.0K	2019-03-18 12:21	./logging.tcl
4.0K	2019-03-18 12:21	./psync2-reg.tcl
12K	2019-03-18 12:21	./psync2.tcl
8.0K	2019-03-18 12:21	./rdb.tcl
8.0K	2019-03-18 12:21	./redis-cli.tcl
4.0K	2019-03-18 12:21	./replication-2.tcl
8.0K	2019-03-18 12:21	./replication-3.tcl
8.0K	2019-03-18 12:21	./replication-4.tcl
8.0K	2019-03-18 12:21	./replication-psync.tcl
12K	2019-03-18 12:21	./replication.tcl
104K	2019-03-18 12:21	.

10 -d 控制遍历子目录的深度

[root@localhost redis-5.0.4]# du -h -d 1 .
65M	./deps
52M	./src
800K	./tests
136K	./utils
117M	.
[root@localhost redis-5.0.4]# 

更深入的了解可以直接通过du的man文件了解

<think>嗯,用户想了解Linuxdu命令的用法和示例。首先,我需要回忆一下du的基本功能。du是disk usage的缩写,主要用于查看目录或文件的磁盘使用情况。用户可能需要知道如何快速检查某个目录的大小,或者找出占用空间最多的文件。 接下来,我应该考虑用户可能的场景。比如,他们可能遇到磁盘空间不足的问题,需要找出哪个目录占用了太多空间。这时候,du的常用选项如-h(人类可读格式)、-s(总计)会很有用。比如du -sh /home可以查看/home目录的总大小。 然后,用户可能需要更详细的示例,比如如何按大小排序子目录。这时候需要结合sort命令,例如du -h --max-depth=1 | sort -hr。这里要注意不同系统下sort的参数可能不同,比如MacOS可能需要调整。 另外,用户可能对排除某些文件类型感兴趣,比如用--exclude参数来过滤掉不需要的文件。或者需要检查文件的修改时间,使用--time选项。 还要考虑用户是否需要监控磁盘使用情况的变化,这时候--time和定期执行命令可能会有帮助。比如结合watch命令实时监控。 需要确保回答覆盖基本用法、常用选项、实际案例,以及可能遇到的问题,比如权限不足导致部分目录无法访问,这时候需要sudo来提升权限。 还要检查引用的资料是否有帮助。引用1提到--time选项,引用3提到了基本语法。可以引用这些来增强答案的可信度。同时,用户提供的引用中有df和du的区别,可能需要在相关问题中提及,帮助用户区分两者的不同用途。 最后,生成相关问题时要考虑用户可能的后续疑问,比如如何排除特定文件、如何排序结果、与df的区别,以及权限问题如何处理。这些问题能帮助用户更深入地理解du的使用场景和技巧。</think>### Linux du 命令详解及示例 du(disk usage)是Linux系统中用于分析**目录/文件磁盘占用**的核心命令,特别适合排查存储空间异常问题。以下从基础到进阶分步骤说明其用法: --- #### 一、基础语法 ```bash du [选项] [目录/文件路径] ``` 若未指定路径,默认分析**当前目录**。 --- #### 二、核心功能与常用选项 1. **查看目录总大小** ```bash du -sh /path/to/directory # -s: 显示总计,-h: 人类可读单位 ``` 示例: ```bash $ du -sh /var/log 1.2G /var/log ``` > 适用于快速定位大目录[^3] 2. **显示子目录详细大小** ```bash du -h --max-depth=1 /path # 限制递归深度为1级子目录 ``` 示例: ```bash $ du -h --max-depth=1 /home 24G /home/user1 15G /home/user2 39G /home ``` 3. **按大小排序输出结果** ```bash du -h --max-depth=1 /path | sort -hr # -h: 按人类可读数值排序,-r: 降序 ``` > 注意:MacOS需改用`-k`替代`-h`参数[^2] 4. **追踪文件修改时间** ```bash du --time -sh /path # 显示最后修改时间 ``` 输出示例: ```bash 1.2G 2024-01-15 14:30 /var/log ``` > 适用于分析近期变化的目录[^1] 5. **排除特定文件类型** ```bash du -h --exclude="*.tmp" /path # 跳过所有.tmp文件 ``` --- #### 三、典型应用场景 1. **定位存储异常增长** ```bash sudo du -sh /* # 扫描根目录下所有一级目录 ``` 2. **监控目录容量变化** ```bash watch -n 60 "du -sh /tmp" # 每60秒刷新/tmp目录大小 ``` 3. **统计隐藏文件占用** ```bash du -sch .[!.]* * | sort -h # 包含点文件的全量统计 ``` --- #### 四、注意事项 1. **权限问题**:分析系统目录时需加`sudo` 2. **符号链接**:默认不追踪,需添加`-L`选项 3. **与df的区别**:`df`显示磁盘分区的整体使用情况,`du`分析具体目录的存储占用[^2] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值