Skip to content

Latest commit

 

History

History
724 lines (501 loc) · 22.9 KB

du_commands.md

File metadata and controls

724 lines (501 loc) · 22.9 KB

The du command in Unix-like operating systems is used to estimate and report the disk space usage of files and directories. Unlike the df command, which shows the disk usage of entire file systems, du provides a more granular view, typically at the directory level, helping users identify which files and directories are consuming the most disk space. It’s an essential tool for managing disk space, especially on systems with large amounts of data or many files.

Here’s an in-depth guide to the du command and its usage:


1. Basic Usage of du

The basic syntax of the du command is:

du [options] [directory or file]
  • directory or file: The path of the directory or file you want to check. If no directory is provided, du will check the current directory.

Example:

  • Check disk usage of the current directory:

    du

    This command shows the disk usage of the current directory and all its subdirectories.

  • Check disk usage of a specific directory:

    du /path/to/directory

    This shows the disk usage for the specified directory and its subdirectories.


2. Understanding du Output

The du command typically displays output in the following format:

4   /path/to/directory/file1
12  /path/to/directory
  • Size (in blocks): The first column indicates the disk space used by the file or directory, in 1K-blocks by default.
  • Path: The second column shows the path to the file or directory.

For example, the above output indicates that file1 uses 4 blocks, and the directory itself uses 12 blocks, including subdirectories.


3. Commonly Used du Options

The du command includes several options to change its behavior or provide more specific information. Here are some of the most commonly used options:

3.1 Display Disk Usage in Human-Readable Format

  • -h: Displays disk space in a human-readable format (KB, MB, GB, etc.).
    du -h
    Example output:
    4.0K    /path/to/file1
    12K     /path/to/directory

3.2 Display Disk Usage of a Specific Directory

  • du [directory]: Shows the disk usage of a specific directory and its subdirectories.
    du -h /home/user/Documents

3.3 Summarize Disk Usage of a Directory

  • -s: Summarizes the total disk usage for the specified directory, without showing details for each subdirectory.
    du -sh /home/user
    Example output:
    1.5G    /home/user

3.4 Limit Output to a Maximum Depth of Subdirectories

  • --max-depth=[N]: Limits the depth of subdirectories to display. For example, --max-depth=1 shows only the disk usage for the top-level directories.
    du -h --max-depth=1 /home/user

3.5 Show Disk Usage for All Files and Subdirectories

  • -a: Includes all files in the output, not just directories.
    du -ah /home/user

3.6 Display Disk Usage in Bytes

  • -b: Shows the disk usage in bytes.
    du -b /path/to/file_or_directory

3.7 Show Only the Total Disk Usage

  • -c: Displays a grand total at the end of the output.
    du -ch /home/user
    Example output:
    4.0K    /home/user/file1
    12K     /home/user
    12K     total

3.8 Show Disk Usage of All File Systems

  • -x: Limits the display to the current file system. This excludes directories mounted on different file systems.
    du -h -x /home

3.9 Display Disk Usage in Kilobytes

  • -k: Forces the display of disk space in kilobytes (default for many systems).
    du -k /path/to/directory

4. Practical Examples of du

4.1 Check the Disk Usage of the Current Directory

To display disk usage for the current directory in a human-readable format:

du -h

4.2 Check the Disk Usage for a Specific Directory

To check the disk usage of a directory (e.g., /var/log):

du -sh /var/log

4.3 Display the Disk Usage of All Files and Subdirectories

To display the disk usage of all files and subdirectories in a specific directory:

du -ah /home/user

4.4 Summarize the Disk Usage of the Entire System

To get a summary of disk usage for the entire system (or a specific directory):

du -sh /

4.5 Check Disk Usage for Directories at the First Level

To view the disk usage of the top-level directories in the /home/user directory:

du -h --max-depth=1 /home/user

4.6 Display Disk Usage in Bytes

To show the disk usage of a directory in bytes:

du -b /path/to/directory

4.7 Display Total Disk Usage Including a Grand Total

To display the disk usage and include a grand total:

du -ch /home/user

5. Advanced du Usage Scenarios

5.1 Find the Largest Directories in a File System

You can use du with sorting (sort) to identify the largest directories. For example:

du -ah /path/to/directory | sort -rh | head -n 10

This command lists the top 10 largest files and directories in the specified directory.

5.2 Exclude Specific Files or Directories

If you want to exclude certain files or directories from the du output, you can use grep or other shell utilities in combination with du:

du -ah /home/user | grep -v '/tmp'

This excludes any results related to /tmp.

5.3 Monitor Disk Usage in Real-Time

You can combine du with the watch command to monitor disk usage over time:

watch du -h --max-depth=1 /home/user

This will update the disk usage statistics every 2 seconds.


6. Summary of Common du Options

Option Description
-h Display disk usage in human-readable format (KB, MB, GB, etc.)
-s Show the total disk usage of the specified directory
--max-depth=[N] Limit the output to the top N levels of subdirectories
-a Display disk usage for all files (not just directories)
-b Display disk usage in bytes
-c Display a total at the end of the output
-k Display disk usage in kilobytes
-x Exclude directories on different file systems from the output
-i Show inode usage (for file systems with many small files)

7. Conclusion

The du command is a powerful and flexible tool for monitoring disk space usage on Unix-like systems. It allows users to track disk usage at a fine-grained level, helping system administrators and regular users identify large files or directories that may need to be cleaned up or managed. By using various options, du can be customized to provide both detailed and summary views, making it an indispensable part of any system administrator's toolkit.

The du (disk usage) command in Unix-like operating systems (Linux, macOS, etc.) is used to estimate and report the disk space used by files and directories. It provides detailed information about the disk usage of files and directories, which is essential for managing storage and ensuring that systems do not run out of space.

1. Basic Usage of du

The basic syntax of the du command is:

du [options] [file or directory]
  • file or directory: This is the file or directory you want to check the disk usage for. If no file or directory is specified, du will display the disk usage for the current directory.

Examples:

  • Check disk usage for the current directory:

    du

    This will display the disk usage of the current directory and its subdirectories.

  • Check disk usage for a specific directory:

    du /path/to/directory

    This will display the disk usage of the specified directory and its subdirectories.


2. Understanding du Output

The default output of du is a list of directories and their respective disk usage in kilobytes (KB).

Column Description
Size The disk space used by the file or directory (in kilobytes by default).
Directory The directory or file for which the disk usage is reported.

Example Output:

8       ./folder1
4       ./folder1/subfolder1
12      ./folder2
24      .

In this example:

  • The total disk usage of folder1 (including its subfolder subfolder1) is 8 KB.
  • The total disk usage of folder2 is 12 KB.
  • The total disk usage of the current directory (represented by .) is 24 KB.

3. Commonly Used du Options

The du command comes with several options to modify its behavior or provide more detailed information.

3.1 Display Disk Usage in Human-Readable Format

  • -h: This option displays the disk usage in a human-readable format, using KB, MB, GB, etc.
    du -h
    Example output:
    8.0K    ./folder1
    4.0K    ./folder1/subfolder1
    12K     ./folder2
    24K     .

3.2 Show Only Total Disk Usage for a Directory

  • -s: This option shows only the total disk usage of the specified directory (and not its subdirectories).
    du -sh /path/to/directory
    Example output:
    24K     /path/to/directory

3.3 Show Disk Usage for All Files and Subdirectories

  • -a: This option shows the disk usage for all files and subdirectories in addition to directories.
    du -ah
    Example output:
    4.0K    ./file1
    8.0K    ./folder1
    4.0K    ./folder1/subfolder1
    12K     ./folder2
    24K     .

3.4 Display Disk Usage for Each Directory Level

  • --max-depth=N: This option limits the depth of the directory structure to display. For example, --max-depth=1 will show disk usage for the current directory and its immediate subdirectories, but not deeper levels.
    du -h --max-depth=1
    Example output:
    8.0K    ./folder1
    12K     ./folder2
    24K     .

3.5 Display Disk Usage for All Directories Except Some

  • --exclude=PATTERN: This option excludes files or directories matching the given pattern.
    du -ah --exclude="*.log"
    This will show disk usage for all files and directories, excluding those that end with .log.

3.6 Show Disk Usage for Directories Only

  • -d N: This option limits the depth of directory listing, similar to --max-depth=N.
    du -hd 2

3.7 Sort the Output

  • | sort -h: To sort the output by disk usage in ascending or descending order, you can pipe the output of du into the sort command.

    du -ah | sort -h
  • -r: Sort in reverse order (largest files/directories first).

    du -ah | sort -hr

3.8 Display Disk Usage in Blocks

  • -b: This option shows disk usage in bytes (not kilobytes, which is the default).
    du -b

4. Practical Examples of du

4.1 Check the Disk Usage for a Specific Directory

To check the disk usage for a directory (e.g., /home/user/documents) in a human-readable format:

du -sh /home/user/documents

4.2 Display Disk Usage for All Files in a Directory

To show the disk usage of all files and directories within /home/user/documents:

du -ah /home/user/documents

4.3 Show Disk Usage for Directory Levels

To view the disk usage for the current directory and its immediate subdirectories only:

du -h --max-depth=1

4.4 Find the Largest Files in a Directory

To list the largest files in the current directory and subdirectories, sorted by disk usage:

du -ah | sort -rh | head -n 10

This will display the top 10 largest files and directories in the current directory.

4.5 Exclude Specific Files from Disk Usage Calculation

To exclude all .log files from the disk usage calculation:

du -ah --exclude="*.log"

4.6 Display Disk Usage of a Directory and Its Subdirectories Up to Depth 2

To show disk usage for a directory and its subdirectories up to 2 levels deep:

du -h --max-depth=2 /path/to/directory

5. Use Cases for du

  • Monitoring Disk Usage: Track the disk usage of specific directories or files to ensure you're not running out of space.

  • Disk Cleanup: Identify large files or directories that can be deleted or archived to free up disk space.

  • Backup Verification: Check how much space the backup files are consuming by running du on the backup directory.

  • Optimizing Disk Space: Use du to identify large directories or files that could be optimized or compressed to save space.


6. Summary of Common du Options

| Option | Description | | ------------------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------ | | -h | Display sizes in human-readable format (KB, MB, GB, etc.). | | -s | Show only total disk usage for the specified directory. | | -a | Show disk usage for all files and directories, not just directories. | | --max-depth=N | Limit the depth of the directory structure displayed. | | --exclude=PATTERN | Exclude files or directories matching the pattern from the output. | | -b | Display disk usage in bytes instead of kilobytes. | | -d N | Limit the depth of directory listing. | | | sort -h | Sort the output by disk usage in ascending order. | | | sort -hr | Sort the output by disk usage in descending order (largest files first). |


7. Conclusion

The du command is an invaluable tool for disk space management and analysis. It helps users and system administrators understand how disk space is being used across files and directories, making it easier to identify large files and directories that might be taking up unnecessary space. With its variety of options, du provides a flexible and powerful method for managing disk space effectively on Unix-like systems.

The du (Disk Usage) command in Unix-like operating systems (Linux, macOS, etc.) is used to estimate and report the disk space usage of files and directories. It is a helpful tool for system administrators and users who want to analyze and manage disk space, especially for directories with large amounts of data.

Here's an in-depth explanation of the du command, its options, and examples of how to use it effectively:


1. Basic Usage of du

The basic syntax of the du command is:

du [options] [file/directory]
  • file/directory: The file or directory you want to check for disk usage. If no file or directory is specified, du will report the disk usage of the current directory.

Example:

  • Check disk usage of the current directory:

    du

    This shows the disk usage of the current directory and its subdirectories in terms of disk blocks (usually 1K-blocks).

  • Check disk usage for a specific directory:

    du /path/to/directory

    This shows the disk usage for the specified directory.


2. Understanding du Output

By default, du displays the disk usage for each directory and its subdirectories, in 1K-blocks. For example:

8   ./dir1
4   ./dir1/subdir1
4   ./dir1/subdir2
16  .

In this output:

  • 8 represents the total disk usage of dir1.
  • 4 is the disk usage of subdir1 and subdir2.
  • 16 at the bottom represents the total disk usage of the current directory (.), which includes the disk usage of all its subdirectories.

3. Commonly Used du Options

There are several useful options you can use with du to modify its behavior. Here are some of the most common options:

3.1 Display Disk Usage in Human-Readable Format

  • -h: Displays the disk usage in a human-readable format, using KB, MB, GB, etc., instead of the default number of 1K-blocks.
    du -h
    Example output:
    8.0K    ./dir1
    4.0K    ./dir1/subdir1
    4.0K    ./dir1/subdir2
    16K     .

3.2 Display Only Total Usage for a Directory

  • -s: Summarizes the total disk usage for the given directory or file, rather than showing the disk usage of all its subdirectories.
    du -sh /path/to/directory
    Example output:
    16K     /path/to/directory

3.3 Display Disk Usage for Each Directory Recursively

  • -a: Displays disk usage for all files and directories, rather than just directories.
    du -ah /path/to/directory
    Example output:
    4.0K    /path/to/directory/file1.txt
    8.0K    /path/to/directory/dir1
    4.0K    /path/to/directory/dir1/file2.txt

3.4 Limit the Depth of Directory Recursion

  • --max-depth=N: Limits the display of disk usage to directories that are at most N levels deep.
    du -h --max-depth=1 /path/to/directory
    Example output:
    8.0K    /path/to/directory/dir1
    4.0K    /path/to/directory/dir2
    16K     /path/to/directory
    Here, --max-depth=1 shows only the top-level directories and their sizes.

3.5 Exclude Certain Files or Directories

  • --exclude=PATTERN: Excludes files or directories that match the given pattern.
    du -ah --exclude='*.txt' /path/to/directory

3.6 Display Disk Usage for File Systems in Terms of Inodes

  • -i: Reports the number of inodes (file system objects) used by each file or directory, rather than the disk space usage.
    du -i /path/to/directory
    Example output:
    8       ./dir1
    4       ./dir1/subdir1
    4       ./dir1/subdir2
    16      .

3.7 Show Only the Largest Directories

  • --max-depth=N: Combined with sorting tools like sort, you can use du to display the largest directories. This is helpful for finding large files and directories consuming disk space.
    du -h --max-depth=1 /path/to/directory | sort -rh

3.8 Display Usage in Specific Units

  • -k: Forces the display of disk space in kilobytes (default behavior).
  • -m: Displays the disk usage in megabytes.
  • -g: Displays the disk usage in gigabytes.
du -h -m /path/to/directory

This will display the disk usage in megabytes.


4. Practical Examples of du

Here are some practical examples of how you can use the du command:

  • Check disk usage for the current directory and all its subdirectories:

    du -ah
  • Check the total disk usage of a specific directory in human-readable format:

    du -sh /var/log
  • List the disk usage of each subdirectory in the /home directory, but limit the depth to 2 levels:

    du -h --max-depth=2 /home
  • Find large files and directories within the /var directory:

    du -ah /var | sort -rh | head -n 10
  • Check disk usage of all files and directories in the /etc directory but exclude .bak files:

    du -ah --exclude='*.bak' /etc

5. Advanced du Usage Scenarios

5.1 Tracking Disk Space Over Time

You can use du in a script to track changes in disk usage over time. For example, you could use the following command to capture the disk usage and save it to a file:

du -sh /path/to/directory > disk_usage.txt

5.2 Combine with find to Find Large Files

You can combine du with find to identify large files or directories that may be taking up significant disk space:

find /path/to/directory -type f -exec du -h {} + | sort -rh | head -n 10

This finds the 10 largest files within the directory and displays them.

5.3 Using du with Cron Jobs

To automate disk space checks, you can use du within a cron job. For example, you can schedule a weekly report of the /home directory disk usage:

crontab -e

Add the following line to check the disk usage of /home and email it to yourself every Sunday at midnight:

0 0 * * SUN du -sh /home | mail -s "Disk Usage Report" [email protected]

6. Summary of Common du Options

Option Description
-h Human-readable output (KB, MB, GB, etc.)
-s Show total disk usage for the specified directory or file
-a Show usage for all files, not just directories
--max-depth=N Limit the depth of directory recursion to N levels
-i Show inode usage rather than disk space
--exclude=PATTERN Exclude files and directories matching the pattern
-k Display usage in kilobytes (default)
-m Display usage in megabytes
-g Display usage in gigabytes

Conclusion

The du command is a powerful tool for checking disk usage in Linux and other Unix-like systems. It allows you to get detailed information about disk space usage at the file and directory levels. By using the various options, you can tailor its output to your specific needs—whether you're checking for large files, summarizing disk usage, or tracking usage over time. Regularly using du can help prevent your system from running out of disk space and allow you to manage storage effectively.