How to show disk usage in Linux

LinuxLinuxBeginner
Practice Now

Introduction

Understanding disk usage is crucial for Linux system administrators and developers. This comprehensive tutorial explores various techniques and commands to effectively monitor and analyze disk space in Linux environments. By mastering these skills, you'll gain insights into storage management, system performance, and resource allocation.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/SystemInformationandMonitoringGroup -.-> linux/watch("`Command Repeating`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/watch -.-> lab-420235{{"`How to show disk usage in Linux`"}} linux/cd -.-> lab-420235{{"`How to show disk usage in Linux`"}} linux/pwd -.-> lab-420235{{"`How to show disk usage in Linux`"}} linux/find -.-> lab-420235{{"`How to show disk usage in Linux`"}} linux/ls -.-> lab-420235{{"`How to show disk usage in Linux`"}} linux/df -.-> lab-420235{{"`How to show disk usage in Linux`"}} linux/du -.-> lab-420235{{"`How to show disk usage in Linux`"}} linux/mount -.-> lab-420235{{"`How to show disk usage in Linux`"}} end

Disk Usage Basics

Understanding Disk Space Management

Disk usage management is a critical skill for Linux system administrators and developers. It involves monitoring, analyzing, and optimizing storage resources to ensure efficient system performance.

Key Concepts of Disk Usage

What is Disk Usage?

Disk usage refers to the amount of storage space occupied by files, directories, and system resources on a computer's storage device. Understanding disk usage helps prevent storage overflow and optimize system performance.

Importance of Disk Usage Monitoring

graph TD A[Disk Usage Monitoring] --> B[Prevent Storage Overflow] A --> C[Optimize System Performance] A --> D[Identify Large Files/Directories] A --> E[Plan Storage Expansion]

Disk Space Measurement Units

Unit Size Description
Byte 1 B Smallest storage unit
Kilobyte 1 KB 1,024 Bytes
Megabyte 1 MB 1,024 KB
Gigabyte 1 GB 1,024 MB
Terabyte 1 TB 1,024 GB

Common Disk Usage Scenarios

  1. Server storage management
  2. Development environment optimization
  3. Backup and archiving
  4. Performance troubleshooting

Best Practices

  • Regularly monitor disk space
  • Remove unnecessary files
  • Use compression techniques
  • Implement storage quotas

LabEx Tip

On LabEx platforms, understanding disk usage is crucial for efficient cloud-based development and learning environments.

Linux Disk Commands

Essential Commands for Disk Usage Analysis

df (Disk Free) Command

The df command provides information about disk space usage across file systems.

Basic Usage
df
Advanced Options
## Show disk usage in human-readable format
df -h

## Display inode information
df -i

du (Disk Usage) Command

The du command helps analyze disk space used by directories and files.

Key Options
## Show total size of current directory
du -sh

## List size of subdirectories
du -sh *

## Show detailed file sizes
du -ah

Command Comparison

graph LR A[Disk Commands] --> B[df: File System Overview] A --> C[du: Directory/File Details] A --> D[fdisk: Disk Partitioning]

Comprehensive Command Reference

Command Primary Function Key Options
df File system space -h, -i
du Directory usage -sh, -ah
fdisk Disk partitioning -l
lsblk List block devices -f

Advanced Disk Analysis Techniques

  1. Combine commands for detailed insights
  2. Use pipes for filtering
  3. Schedule regular disk checks

Example: Complex Disk Analysis

## Find top 10 largest directories
du -h / | sort -rh | head -10

LabEx Recommendation

On LabEx cloud environments, mastering these commands is crucial for efficient system management and troubleshooting.

Best Practices

  • Always use -h for human-readable output
  • Regularly monitor disk usage
  • Understand the context of disk space consumption

Practical Disk Analysis

Comprehensive Disk Management Strategies

Identifying Large Files and Directories

Using Find Command
## Find files larger than 100MB
find / -type f -size +100M 2>/dev/null

## Find top 10 largest files
find / -type f -printf '%s %p\n'| sort -nr | head -10

Disk Space Cleanup Techniques

graph TD A[Disk Space Cleanup] --> B[Remove Temporary Files] A --> C[Clear Package Caches] A --> D[Delete Old Logs] A --> E[Uninstall Unused Packages]

Automated Disk Monitoring Scripts

Basic Monitoring Script

#!/bin/bash
## Disk Usage Monitoring Script

THRESHOLD=90

## Check disk usage
DISK_USAGE=$(df -h / | awk '/\// {print $5}' | sed 's/%//')

if [ $DISK_USAGE -ge $THRESHOLD ]; then
    echo "Warning: Disk usage is $DISK_USAGE%"
    ## Send alert or take action
fi

Disk Analysis Tools Comparison

Tool Function Key Features
ncdu Interactive disk usage User-friendly interface
baobab Graphical disk usage Visual representation
stacer System optimization Comprehensive cleanup

Advanced Troubleshooting Techniques

  1. Use lsof to find open files
  2. Analyze disk I/O with iotop
  3. Check file system health with fsck

Disk I/O Monitoring

## Real-time disk I/O monitoring
sudo iotop

LabEx Pro Tip

In LabEx cloud environments, regular disk analysis prevents performance bottlenecks and ensures smooth system operation.

Best Practices

  • Implement regular cleanup scripts
  • Set up disk usage alerts
  • Use compression for large files
  • Regularly review and optimize storage

Automated Cleanup Example

#!/bin/bash
## Automated Cleanup Script

## Clear package manager cache
sudo apt clean

## Remove old log files older than 30 days
find /var/log -type f -mtime +30 -delete

## Clear temporary files
sudo rm -rf /tmp/*

Conclusion

Effective disk analysis requires a combination of tools, scripts, and proactive management strategies.

Summary

Mastering disk usage analysis in Linux empowers users to efficiently manage storage resources, diagnose potential space constraints, and optimize system performance. By leveraging commands like df and du, Linux professionals can make informed decisions about file system management and proactively address storage challenges.

Other Linux Tutorials you may like