Linux du 命令详解:查看磁盘使用情况与高级用法 (中英双语)查看某个用户磁盘占用

ops/2025/2/6 22:47:38/
github-gist">

Linux du 命令详解:查看磁盘使用情况与高级用法

在 Linux 服务器或个人计算机中,了解磁盘使用情况是系统维护的关键任务之一。duDisk Usage)命令是 Linux 提供的一个强大工具,可以帮助我们分析目录和文件的磁盘占用情况。本文将详细介绍 du 命令的基础用法、如何查看不同用户的磁盘占用情况,以及一些高级技巧,以帮助你更高效地管理磁盘空间。

1. du 命令简介

dudisk usage,磁盘使用情况)用于统计文件或目录所占用的磁盘空间。其基本语法如下:

du [选项] [文件或目录]

默认情况下,du 会递归统计指定目录中的所有文件和子目录,并返回每个子目录的大小,最终在末尾输出整个目录的总大小。

2. 基础用法

2.1 查看当前目录大小

du -sh .
  • -s:仅显示目录或文件的总大小,而不显示子目录的大小。
  • -h:以人类可读格式(如 KB、MB、GB)显示结果。

例如:

$ du -sh .
1.2G    .

表示当前目录占用 1.2G 磁盘空间。

2.2 查看单个文件的大小

du -h example.txt

如果 example.txt 占用 2.5M,那么输出将类似:

2.5M    example.txt

3. 统计每个用户的磁盘占用情况

在多用户 Linux 服务器上,我们通常需要统计每个用户的磁盘使用量。假设用户的家目录都在 /home 目录下,我们可以使用以下命令:

sudo du -sh /home/*

示例输出:

4.2G    /home/alice
8.7G    /home/bob
2.1G    /home/charlie

如果服务器上的数据存放在 /data 目录下,我们可以运行:

sudo du -sh /data/*

示例输出:

50G     /data/projectA
120G    /data/projectB

如果需要按照用户进行归类统计,可以结合 awk 进行处理:

sudo du -sh /home/* | awk '{print $2, $1}'

输出结果示例:

/home/alice 4.2G
/home/bob 8.7G
/home/charlie 2.1G

4. du 命令的高级用法

4.1 按层级列出目录大小

有时候,我们希望按照目录层级查看占用情况,例如 /var/log 目录下的空间使用情况,可以使用:

du -h --max-depth=1 /var/log

示例输出:

4.0K    /var/log/apt
1.2M    /var/log/nginx
500M    /var/log/journal
501M    /var/log

--max-depth=1 选项限制 du 仅统计指定层级的目录大小,不会递归到更深的子目录。

4.2 排序磁盘使用情况

我们可以将 du 的输出按大小排序,快速找出占用空间最大的目录:

du -h --max-depth=1 /home | sort -hr

示例输出:

12G     /home/bob
7G      /home/alice
2G      /home/charlie

sort -hr 作用:

  • -h:按照人类可读格式(如 K、M、G)进行排序。
  • -r:按降序排列。

4.3 统计指定类型文件的磁盘占用情况

如果我们想查看某个目录下特定类型的文件(如 .log 文件)占用多少磁盘空间,可以结合 find 命令:

find /var/log -name "*.log" -exec du -ch {} + | grep total$

示例输出:

1.5G    total

表示所有 .log 文件总计占用 1.5G 空间。

4.4 仅统计某个用户的文件占用空间

如果想查看某个用户(如 alice)在整个系统中的文件占用情况,可以使用:

sudo du -sh /home/alice

如果用户的数据可能存放在 /data 目录,也可以添加:

sudo du -sh /home/alice /data/alice

或者查找整个 / 根目录下属于 alice 用户的文件:

sudo find / -user alice -exec du -ch {} + | grep total$

5. dudf 的对比

du 主要用于分析文件和目录的实际占用空间,而 dfdisk free)则用于查看整个磁盘的使用情况。例如:

df -h

示例输出:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1      500G  350G  150G  70% /
tmpfs          7.8G   2M   7.8G   1% /run
  • df 关注整个磁盘的分区使用情况,而 du 适用于深入分析目录和文件的使用情况。

6. 总结

本文介绍了 du 命令的基础用法、如何统计各个用户的磁盘占用情况,并列举了一些高级技巧,如按层级统计、按大小排序、统计特定类型文件的使用情况等。在管理 Linux 服务器或本地系统时,du 是一个非常有用的工具,能够帮助我们高效地监控磁盘空间并找出占用大量存储的文件或目录。

在实际运维中,可以将 du 命令与 findsortawk 等工具结合使用,以实现更强大的分析能力,提高磁盘管理效率!

Understanding the du Command in Linux: An In-Depth Guide

Managing disk space is a critical task in Linux system administration, whether on personal machines or enterprise servers. The du (Disk Usage) command is a powerful tool that helps users analyze and monitor disk space usage at the file and directory level. This article will cover the basics of du, how to check disk usage by individual users, and some advanced techniques to enhance your workflow.


1. Introduction to du

The du (disk usage) command is used to estimate file space usage. Its basic syntax is:

du [OPTIONS] [FILE or DIRECTORY]

By default, du recursively scans directories and returns the size of each subdirectory before displaying the total.


2. Basic Usage

2.1 Checking the Size of the Current Directory

To quickly check how much space the current directory is using:

du -sh .

Explanation:

  • -s: Summarizes the total size without showing individual files and subdirectories.
  • -h: Displays results in a human-readable format (e.g., KB, MB, GB).

Example output:

1.2G    .

This means the current directory consumes 1.2GB of space.

2.2 Checking the Size of a Specific File

To check the size of a single file:

du -h example.txt

Example output:

2.5M    example.txt

This means the file occupies 2.5MB of disk space.


3. Checking Disk Usage by User

On multi-user Linux servers, it is essential to monitor each user’s disk usage. If users store their data under /home, we can check their usage with:

sudo du -sh /home/*

Example output:

4.2G    /home/alice
8.7G    /home/bob
2.1G    /home/charlie

For servers that store user data in /data, use:

sudo du -sh /data/*

Example output:

50G     /data/projectA
120G    /data/projectB

To format the output more neatly, we can use awk:

sudo du -sh /home/* | awk '{print $2, $1}'

Example output:

/home/alice 4.2G
/home/bob 8.7G
/home/charlie 2.1G

4. Advanced du Usage

4.1 Displaying Disk Usage by Directory Depth

To view the size of subdirectories at a specific depth (e.g., /var/log), use:

du -h --max-depth=1 /var/log

Example output:

4.0K    /var/log/apt
1.2M    /var/log/nginx
500M    /var/log/journal
501M    /var/log

The --max-depth=1 option limits the recursion depth to only the first level.

4.2 Sorting Disk Usage

Sorting directories by size helps identify large space consumers:

du -h --max-depth=1 /home | sort -hr

Example output:

12G     /home/bob
7G      /home/alice
2G      /home/charlie
  • sort -hr:
    • -h: Sorts in human-readable format (e.g., K, M, G).
    • -r: Reverses the order (largest first).

4.3 Finding the Space Usage of Specific File Types

To check the total space used by .log files in /var/log:

find /var/log -name "*.log" -exec du -ch {} + | grep total$

Example output:

1.5G    total

This means all .log files combined take up 1.5GB of space.

4.4 Checking Disk Usage by a Specific User

To analyze how much space a particular user (e.g., alice) is using:

sudo du -sh /home/alice

If the user also stores files under /data, check:

sudo du -sh /home/alice /data/alice

To find all files belonging to alice across the system:

sudo find / -user alice -exec du -ch {} + | grep total$

5. du vs. df

While du measures individual file and directory sizes, df (disk free) provides an overview of disk partition usage:

df -h

Example output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1      500G  350G  150G  70% /
tmpfs          7.8G   2M   7.8G   1% /run
  • df shows free and used space for disk partitions.
  • du breaks down space usage at the directory and file level.

6. Summary

This article explored the du command in detail, covering:

  • Basic usage for analyzing file and directory sizes.
  • Checking disk usage per user.
  • Advanced techniques such as sorting, filtering by file type, and limiting directory depth.
  • Differences between du and df.

By mastering du, system administrators and users can efficiently monitor and manage disk space, preventing storage issues before they become critical. Integrating du with tools like find, sort, and awk further enhances its usefulness in daily operations.

后记

2025年2月5日于山东日照。在GPT4o大模型辅助下完成。


http://www.ppmy.cn/ops/156272.html

相关文章

RabbitMQ持久化队列配置修改问题

文章目录 1.问题产生2.问题解决1.询问gpt2.独立思考 1.问题产生 我在给一个普通队列去绑定死信交换机和死信队列的时候,发现总是报错x-dead-letter-exchange的属性为none ERROR [PFTID:] [Module:defaultModule] org.springframework.amqp.rabbit.connection.Cach…

JDK长期支持版本(LTS)

https://blogs.oracle.com/java/post/the-arrival-of-java-23 jdk长期支持版本(LTS):JDK 8、11、17、21:

图论——最小生成树的扩展应用

最小生成树相关原理 acwing1146.新的开始 假设存在一个“超级发电站” 在每一个矿井修发电站相当于从这个“超级发电站”到各个矿井连一条长度为 v [ i ] v[i] v[i]的边。 这样一来这就是一个最短路的模板题。 #include <iostream> #include <cstring> using na…

短链接项目02---依赖的添加和postman测试

文章目录 1.声明2.对于依赖的引入和处理2.1原有的内容说明2.2添加公共信息2.3dependencies和management区别说明2.4添加spring-boot依赖2.5数据库的相关依赖2.6hutool工具类的依赖添加2.7测试test 的依赖添加 3.core文件的代码3.1目录层级结构3.2启动类3.3testcontroller测试类…

HTML基本语法

什么是HTML? HTML是超文本标记语言&#xff08;HyperText Markup Language&#xff09;的缩写&#xff0c;是一种用于创建网页的标准标记语言。HTML允许网页设计师通过使用标签来描述网页的结构和内容。 W3C标准 W3C&#xff08;World Wide Web Consortium&#xff09;是一…

机器学习之数学基础:线性代数、微积分、概率论 | PyTorch 深度学习实战

前一篇文章&#xff0c;使用线性回归模型逼近目标模型 | PyTorch 深度学习实战 本系列文章 GitHub Repo: https://github.com/hailiang-wang/pytorch-get-started 本篇文章内容来自于 强化学习必修课&#xff1a;引领人工智能新时代【梗直哥瞿炜】 线性代数、微积分、概率论 …

Mac M1 ComfyUI 中 AnyText插件安装问题汇总?

Q1&#xff1a;NameError: name ‘PreTrainedTokenizer’ is not defined ? 该项目最近更新日期为2024年12月&#xff0c;该时间段的transformers 版本由PyPI 上的 transformers 页面 可知为4.47.1. A1: transformers 版本不满足要求&#xff0c;必须降级transformors &#…

网络安全--边界安全

现在人们生活依赖互联网程度越来越高&#xff0c;网络安全也逐步进入人们日常视野&#xff0c;信用卡信息泄漏、开房记录被查询、商业机密泄漏等等&#xff1b;无不牵动着一个人、一个公司、甚至一个国家的神经。随着技术的发展&#xff0c;网络边界变得也越来越复杂&#xff0…