The du
command in Unix-like operating systems is used to estimate file and directory space usage. It stands for "disk usage". Here’s an overview of du
and its common usage:
Purpose: du
is used to determine the disk usage of files and directories within a filesystem. It recursively traverses directories and reports back the total disk space used by each file and directory.
Availability: du
is a standard command-line utility available on Unix-like systems, including Linux distributions, macOS, and BSD variants.
-
Display Disk Usage of Current Directory:
- To display the disk usage of files and directories in the current directory:
By default,
du
du
recursively lists the disk usage of all files and directories starting from the current directory.
- To display the disk usage of files and directories in the current directory:
-
Display Disk Usage of a Specific Directory:
- To display the disk usage of a specific directory (e.g.,
/home/user/docs
):Replacedu /home/user/docs
/home/user/docs
with the path to the directory you want to analyze.
- To display the disk usage of a specific directory (e.g.,
-
Display Human-Readable Format:
- To display disk usage in a more human-readable format (e.g., in kilobytes, megabytes, gigabytes):
The
du -h
-h
option (or--human-readable
) converts sizes into a human-readable format.
- To display disk usage in a more human-readable format (e.g., in kilobytes, megabytes, gigabytes):
-
Display Total Disk Usage:
- To display the total disk usage of a directory, including all its subdirectories:
The
du -h --summarize /path/to/directory
--summarize
option provides a summary at the end of the output, showing the total disk usage.
- To display the total disk usage of a directory, including all its subdirectories:
-
Sort Output by Size:
- To sort the output of
du
by size, showing the largest items first:Thedu -h | sort -rh
-r
option sorts in reverse order (largest to smallest), and-h
provides human-readable sizes.
- To sort the output of
-
Limit Depth of Recursive Search:
- To limit the depth of recursive directory search (e.g., up to 2 levels):
Adjust
du -h --max-depth=2 /path/to/directory
2
to the desired depth level you want to analyze.
- To limit the depth of recursive directory search (e.g., up to 2 levels):
-
Symbolic Links: By default,
du
does not follow symbolic links unless specified with the-L
option. -
Permissions: Ensure the user running
du
has appropriate permissions to access the directories and files being analyzed. -
Performance Impact: Analyzing large filesystems with
du
can be resource-intensive, especially when used with options like-r
for recursive scanning.
-
ncdu: A text-based disk usage analyzer that provides an interactive interface for exploring disk usage and navigating directories.
-
Disk Usage Analyzers: Graphical tools like
Baobab
(for GNOME) andFilelight
(for KDE) offer visual representations of disk usage.
du
is a versatile command-line tool for analyzing disk usage on Unix-like systems, providing insights into file and directory sizes within filesystems. It’s useful for managing disk space, identifying large files, and optimizing storage resources.