Skip to content

Commit e201a2e

Browse files
committed
Ch03: Make du match with other intros
1 parent bb7e5a4 commit e201a2e

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

docs/Ch03/index.md

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -874,26 +874,59 @@ $ find [OPTION] PATH [EXPRESSION]
874874

875875
### 统计文件或文件夹大小 {#du}
876876

877-
`du` 命令可以统计文件和目录的大小。目录的大小是无法直接获取的,需要统计里面所有的文件和子目录的大小之后加和才能得到。`du` 命令的输出类似这样:
877+
`du` 命令可以统计文件和目录的大小,因为目录的大小是无法直接获取的,需要统计里面所有的文件和子目录的大小之后加和才能得到。
878878

879879
```console
880-
$ du -h /etc/
881-
4.0K /etc/initramfs-tools/hooks
882-
8.0K /etc/initramfs-tools/conf.d
883-
4.0K /etc/initramfs-tools/scripts/local-premount
884-
4.0K /etc/initramfs-tools/scripts/nfs-premount
885-
(中间内容省略)
886-
4.0K /etc/initramfs-tools/scripts/panic
887-
4.0K /etc/initramfs-tools/scripts/local-top
888-
44K /etc/initramfs-tools/scripts
889-
72K /etc/initramfs-tools
890-
12K /etc/udisks2
891-
16K /etc/fonts/conf.d
892-
60K /etc/fonts/conf.avail
893-
84K /etc/fonts
880+
$ du [OPTION] [FILE or DIRECTORY]
894881
```
895882

896-
由于前面说到的原因,`du` 需要先递归进入子目录,处理完其中所有的项目之后,才能回到上层目录并显示上层目录的总大小。
883+
常用的选项:
884+
885+
| 选项 | 含义 |
886+
| ------------------------ | ---------------------------------- |
887+
| `-h`, `--human-readable` | 以人类可读的方式显示大小 |
888+
| `-s`, `--summarize` | 仅显示总大小 |
889+
| `-a`, `--all` | 显示所有文件和目录的大小 |
890+
| `--max-depth N`, `-d N` | 仅显示到指定的目录深度(N 为数字) |
891+
892+
`du` 需要先递归进入子目录,处理完其中所有的项目之后,才能回到上层目录并显示上层目录的总大小。
893+
894+
!!! example "du 示例"
895+
896+
```console title="显示当前目录下所有文件和目录的大小"
897+
$ du -h
898+
3.8M ./share/iana-etc
899+
4.0K ./share/licenses/iana-etc
900+
4.0K ./share/licenses/tzdata
901+
4.0K ./share/licenses/gcc-libs
902+
4.0K ./share/licenses/ncurses
903+
4.0K ./share/licenses/zlib
904+
4.0K ./share/licenses/sqlite
905+
20K ./share/licenses/util-linux-libs
906+
4.0K ./share/licenses/e2fsprogs
907+
12K ./share/licenses/openssl
908+
(中间内容省略)
909+
9.5G .
910+
```
911+
912+
```console title="显示当前目录下所有文件和目录的大小,并且仅显示一层目录"
913+
$ du -h -d 1
914+
3.0G ./share
915+
1.1G ./bin
916+
337M ./include
917+
4.7G ./lib
918+
4.0K ./local
919+
5.0M ./src
920+
473M ./lib32
921+
340K ./libexec
922+
4.0K ./man
923+
9.5G .
924+
```
925+
926+
```console title="显示当前目录下所有文件和目录的总大小"
927+
$ du -sh
928+
9.5G .
929+
```
897930

898931
此外,`ncdu` 命令可以以图形化和交互式的方式显示目录的内容和大小,并可以用左右方向键浏览目录,类似 Windows 的文件资源管理器。这非常便于观察哪个目录占用了较大的磁盘空间。按 `d` 可以删除当前选中的文件或目录,按 `q` 退出。
899932

0 commit comments

Comments
 (0)