2024-03-16
duAt its core, du summarizes disk usage. Its simplest form is:
du -sh /path/to/directorydu: Invokes the disk usage command.-s: Produces a single total for each argument. Without this, du lists usage for each subdirectory.-h: Prints sizes in human-readable format (e.g., KB, MB, GB)./path/to/directory: Specifies the directory you want to analyze.Let’s say you want to see the total size of your home directory:
du -sh ~This will output a single line showing the total size of your home directory in a human-readable format.
du’s OptionsThe du command offers a range of options to fine-tune your analysis:
1. Detailed Directory Listing:
Omitting the -s option provides a detailed breakdown of disk usage for each subdirectory within the specified path:
du -h /path/to/directoryThis will list each subdirectory and its size, making it easy to pinpoint large directories.
2. Sorting by Size:
To sort the output by size, use the -h (human-readable) and -c (total) options along with sort:
du -sh * | sort -rhThis command lists all files and directories in the current directory, sorts them in reverse order (largest to smallest) and displays the total disk usage at the end. Note the * acts as a wildcard for all files and directories in current location.
3. Specifying File Types:
While du primarily focuses on directories, you can analyze individual files:
du -sh my_large_file.txt4. Finding the Top 10 Largest Directories:
Combining du, sort, and head, you can quickly identify the 10 largest directories:
du -sh /* | sort -rh | head -n 10This command analyzes all directories in the root directory (/), sorts them by size in reverse order, and displays the top 10. Caution: Running this on the root directory can take some time.
5. Excluding Specific Files or Directories:
The --exclude option allows you to ignore specific files or directories during the analysis. For example, to exclude the /tmp directory:
du -sh --exclude=/tmp /path/to/directory6. Maximum Depth:
The -d option lets you specify the maximum depth to traverse when analyzing subdirectories. For instance, to only analyze the immediate subdirectories:
du -d 1 -h /path/to/directoryThese examples demonstrate the versatility of the du command. By combining different options, you can tailor your analysis to your specific needs, effectively managing disk space and identifying potential issues.