> For the complete documentation index, see [llms.txt](https://valineliu.gitbook.io/deuterium-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://valineliu.gitbook.io/deuterium-wiki/linux/cmd/du-xian-shi-wen-jian-da-xiao.md).

# du: 显式文件大小

Linux `du`命令也是查看使用空间的，但是与`df`命令不同的是Linux`du`命令是对文件和目录磁盘使用空间的查看。

## 1. 命令格式

`du [OPTION]... [FILE]...`

## 2. 命令功能

显式每个文件和目录的磁盘使用空间，如果是目录，就递归显式。

## 3. 命令参数

| 参数                        | 含义                                                            |
| ------------------------- | ------------------------------------------------------------- |
| -a, --all                 | 显式所有文件的大小，而不仅仅是目录                                             |
| --apparent-size           | 仅仅显式apparent大小，而不是磁盘占用空间。这个数据会比磁盘占用空间小一些                      |
| -B, --block-size=*SIZE*   | 使用*SIZE*指定的块大小输出                                              |
| -b, --bytes               | 以byte为单位输出，等价于'--apparent-size --block-size=1'                |
| -c, --tot                 | 除了显式文件和目录的大小外，显式总和                                            |
| -D, --dereference-args    | dereference only symlinks that are listed on the command line |
| -H                        | 等价于'--dereference-args (-D)'                                  |
| -h, --human-readable      | 显式可读格式的大小（比如1K，234M，2G）                                       |
| --si                      | 类似于-h，但是以1000为转换单位                                            |
| -k                        | 等价于'--block-size=1K'                                          |
| -l, --count-links         | 如果是硬链接就重复计算                                                   |
| -m                        | 等价于'--block-size=1M'                                          |
| -L, --dereference         | dereference all symbolic links                                |
| -P, --no-dereference      | don't follow any symbolic links (this is the default)         |
| -S, --separate-dirs       | 显式大小时，不包含子目录的大小                                               |
| -s, --summarize           | 仅显示总大小                                                        |
| -x, --one-file-system     | 仅以第一个文件系统为准，其他的略过                                             |
| -X, --exclude-from=*FILE* | 排除符合条件的文件                                                     |
| --exclude=*PATTERN*       | 排除符合条件的文件                                                     |
| --max-depth=*N*           | 递归计算目录大小时，最多递归到N层                                             |
| --time                    | 显式每个文件和目录的最后修改时间                                              |

## 4. 使用实例

### **实例1：显式目录或文件所占空间**

`du`

结果：

```
0    ./search
0    ./article_process
0    ./subcenter
0    ./homepage
0    ./channel
0    ./jieba
65937776    ./toutiao_article_process
8280132    ./baijia_article_process
0    ./toutiao_channel
0    ./toutiao_jieba
0    ./toutiao_homepage
0    ./toutiao_search
0    ./douyin_channel
0    ./douyin_jieba
0    ./douyin_homepage
0    ./douyin_search
5858936    ./douyin_article_process
685904    ./douyin_topic_process
0    ./task_search
0    ./toutiao_task_search
74236    ./delete_cos_file
0    ./import_media_homepage
0    ./huoshan_channel
0    ./pipixia_channel
1108    ./bilibili_crawl
362720    ./douyin_bgm_process
129592    ./douyin_daren_process
12408    ./youtube_process
81342820    .
```

只显示当前目录下的子目录的大小和当前目录的总大小，最下面的是当前目录的总和。

### **实例2：显式指定文件所占空间**

```
du info.log
```

结果：

```
3433284    info.log
```

### **实例3：查看指定目录的所占空间**

```
du media_detect/
```

结果：

```
0    media_detect/search
0    media_detect/article_process
0    media_detect/subcenter
0    media_detect/homepage
0    media_detect/channel
0    media_detect/jieba
65937776    media_detect/toutiao_article_process
8280132    media_detect/baijia_article_process
0    media_detect/toutiao_channel
0    media_detect/toutiao_jieba
0    media_detect/toutiao_homepage
0    media_detect/toutiao_search
0    media_detect/douyin_channel
0    media_detect/douyin_jieba
0    media_detect/douyin_homepage
0    media_detect/douyin_search
5858936    media_detect/douyin_article_process
685904    media_detect/douyin_topic_process
0    media_detect/task_search
0    media_detect/toutiao_task_search
74236    media_detect/delete_cos_file
0    media_detect/import_media_homepage
0    media_detect/huoshan_channel
0    media_detect/pipixia_channel
1108    media_detect/bilibili_crawl
362720    media_detect/douyin_bgm_process
129592    media_detect/douyin_daren_process
12408    media_detect/youtube_process
81342820    media_detect/
```

### **实例4：显式多个文件所占空间**

```
du info.log info.log.20190414 info.log.20190413
```

结果：

```
3433284    info.log
7011232    info.log.20190414
20199964    info.log.20190413
```

### **实例5：只显示总和**

```
du -s
du -s toutiao_article_process/
```

结果：

```
65937780    .
65937780    toutiao_article_process/
```

### **实例6：方便阅读的格式**

```
du -h info.log
```

结果：

```
3.3G    info.log
```

### **实例7：文件和目录都显示**

```
du -ah
```

结果：

```
6.4G    ./toutiao_article_process/info_new_version.log
154M    ./toutiao_article_process/error.log.20190410
6.0G    ./toutiao_article_process/info.log.20190411
232M    ./toutiao_article_process/error.log.20190411
17G    ./toutiao_article_process/info.log.20190412
20G    ./toutiao_article_process/info.log.20190413
3.3G    ./toutiao_article_process/info.log
252M    ./toutiao_article_process/error.log.20190412
6.7G    ./toutiao_article_process/info.log.20190414
52K    ./toutiao_article_process/error.log.20190407
31M    ./toutiao_article_process/error_new_version.log
61M    ./toutiao_article_process/error.log
35M    ./toutiao_article_process/error.log.20190408
4.0K    ./toutiao_article_process/.info.log.swo
4.0K    ./toutiao_article_process/.info.log.swn
154M    ./toutiao_article_process/error.log.20190409
4.2G    ./toutiao_article_process/info.log.20190410
194M    ./toutiao_article_process/error.log.20190413
161M    ./toutiao_article_process/error.log.20190414
63G    ./toutiao_article_process
```

### **实例8：显式指定几个文件，并统计总和**

```
du -c info.log info.log.20190414 info.log.20190413
```

结果：

```
3304388    info.log
7011232    info.log.20190414
20199964    info.log.20190413
30515584    total
```

### **实例9：按空间大小排序**

```
du | sort -nr | more
```

结果：

```
81182196    .
65710208    ./toutiao_article_process
8369220    ./baijia_article_process
5836792    ./douyin_article_process
685904    ./douyin_topic_process
362720    ./douyin_bgm_process
129592    ./douyin_daren_process
74236    ./delete_cos_file
12408    ./youtube_process
1108    ./bilibili_crawl
0    ./toutiao_task_search
0    ./toutiao_search
0    ./toutiao_jieba
0    ./toutiao_homepage
0    ./toutiao_channel
0    ./task_search
0    ./subcenter
0    ./search
0    ./pipixia_channel
0    ./jieba
0    ./import_media_homepage
0    ./huoshan_channel
0    ./homepage
0    ./douyin_search
0    ./douyin_jieba
0    ./douyin_homepage
0    ./douyin_channel
0    ./channel
0    ./article_process
```

### **实例10：输出当前目录下各个子目录使用空间大小**

```
du -h --max-depth=1
```

结果：

```
0    ./search
0    ./article_process
0    ./subcenter
0    ./homepage
0    ./channel
0    ./jieba
63G    ./toutiao_article_process
8.0G    ./baijia_article_process
0    ./toutiao_channel
0    ./toutiao_jieba
0    ./toutiao_homepage
0    ./toutiao_search
0    ./douyin_channel
0    ./douyin_jieba
0    ./douyin_homepage
0    ./douyin_search
5.6G    ./douyin_article_process
670M    ./douyin_topic_process
0    ./task_search
0    ./toutiao_task_search
73M    ./delete_cos_file
0    ./import_media_homepage
0    ./huoshan_channel
0    ./pipixia_channel
1.1M    ./bilibili_crawl
355M    ./douyin_bgm_process
127M    ./douyin_daren_process
13M    ./youtube_process
78G    .
```
