Linux du
命令详解:查看磁盘使用情况与高级用法
在 Linux 服务器或个人计算机中,了解磁盘使用情况是系统维护的关键任务之一。du
(Disk Usage)命令是 Linux 提供的一个强大工具,可以帮助我们分析目录和文件的磁盘占用情况。本文将详细介绍 du
命令的基础用法、如何查看不同用户的磁盘占用情况,以及一些高级技巧,以帮助你更高效地管理磁盘空间。
1. du
命令简介
du
(disk 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. du
与 df
的对比
du
主要用于分析文件和目录的实际占用空间,而 df
(disk 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
命令与 find
、sort
、awk
等工具结合使用,以实现更强大的分析能力,提高磁盘管理效率!
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
anddf
.
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大模型辅助下完成。