Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux du Command: Count The Disk Space Occupied By A Directory Or File

The du (short for "disk usage") command is a Linux utility that estimates the space used by files and directories. It is useful for tracking down large files or directories that are consuming excessive disk space. Here's a basic tutorial on how to use the du command:

  1. Display the disk usage of a directory: To display the disk usage of a specific directory, run the du command followed by the directory path:

    du /path/to/directory
    

    By default, the output will show the space used by each subdirectory in kilobytes.

  2. Display the disk usage of multiple directories: To display the disk usage for multiple directories, list the directory paths after the du command:

    du /path/to/dir1 /path/to/dir2
    
  3. Display the disk usage in human-readable format: Use the -h option to display the output in a more human-readable format, with sizes expressed in bytes (B), kilobytes (K), megabytes (M), gigabytes (G), or terabytes (T):

    du -h /path/to/directory
    
  4. Display the total disk usage of a directory: To display only the total disk usage for a directory, use the -s (summarize) option:

    du -sh /path/to/directory
    
  5. Display disk usage for specific file types: You can use the --include option followed by a file extension or pattern to display disk usage for specific file types:

    du -ah --include='*.txt' /path/to/directory
    
  6. Exclude specific directories or file types: Use the --exclude option followed by a directory or file pattern to exclude certain items from the disk usage output:

    du -h --exclude='*.log' /path/to/directory
    
  7. Find directories larger than a specific size: You can use the find command in combination with the du command to find directories larger than a specific size. For example, to find directories larger than 100MB:

    find /path/to/directory -type d -exec du -sh {} \; | awk '$1 ~ /[0-9]*M/ && $1 > 100'
    

These are some basic examples of using the du command. By understanding how to estimate disk usage, display results in a human-readable format, and filter the output based on specific criteria, you can effectively manage disk space on your Linux system.

  1. How to use du command in Linux: The du command in Linux is used to estimate file and directory space usage. A basic usage example is:

    du /path/to/directory
    

    This command displays the disk space used by the specified directory.

  2. Measuring disk space usage with du: To measure disk space usage:

    du /path/to/directory
    

    This command provides the total disk space used by the specified directory.

  3. Displaying human-readable disk space in du: To display disk space in human-readable format (e.g., KB, MB, GB):

    du -h /path/to/directory
    

    The -h option makes the output more readable.

  4. Recursive disk space counting with du: To count disk space recursively for subdirectories:

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

    The --max-depth option limits the depth of recursion.

  5. Filtering specific directories or files in du: To filter specific directories or files:

    du -h --exclude=/path/to/exclude /path/to/directory
    

    Use the --exclude option to exclude specific paths.

  6. Sorting du output by size in Linux: To sort du output by size:

    du -h --max-depth=1 /path/to/directory | sort -h
    

    The sort -h option sorts the output in a human-readable way.

  7. Excluding certain directories from du analysis: To exclude certain directories from du analysis:

    du -h --exclude=/path/to/exclude /path/to/directory
    

    Use the --exclude option to exclude specific paths.

  8. Checking disk space usage for multiple directories with du: To check disk space usage for multiple directories:

    du -h /path/to/dir1 /path/to/dir2
    

    This command provides disk space usage for each specified directory.

  9. Using du for disk space analysis and cleanup in Linux: For disk space analysis and cleanup:

    du -h --max-depth=1 /path/to/directory | sort -h
    

    Identify large directories and files and consider cleaning or moving them to free up space.