Skip to content

Linux uname, df, and du Commands: Gathering System Information

Published: at 03:47 PMSuggest Changes

As a DevOps professional, having a good understanding of your Linux system’s hardware and software configuration is crucial. The uname, df, and du commands provide valuable information about your system, helping you monitor and troubleshoot issues effectively.

uname Command

Basic Syntax

uname [options]

Common Options

Usage Examples

# Print kernel name
$ uname -s
Linux

# Print kernel release
$ uname -r
5.10.0-19-generic

# Print all system information
$ uname -a
Linux myhost 5.10.0-19-generic #20-Ubuntu SMP Fri Mar 12 09:39:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

df Command

Basic Syntax

df [options] [file|directory]

Common Options

Usage Examples

# Show disk usage
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       50G   20G   28G  43% /
tmpfs           16G  1.6M   16G   1% /run
/dev/sda2      460G  450G  5.0G  99% /home

# Show inode usage
$ df -i
Filesystem      Inodes IUsed   IFree IUse% Mounted on
/dev/sda1      3.2M   105K   3.1M    3% /
tmpfs          4.0M     1    4.0M    1% /run
/dev/sda2      29M    1.1M   28M     4% /home

du Command

Basic Syntax

du [options] [file|directory]

Common Options

Usage Examples

# Show directory size
$ du -h /var/log
4.0K    /var/log/alternatives.log
12K     /var/log/apt
4.0K    /var/log/auth.log
...

# Show total directory size
$ du -sh /var/log
120M    /var/log

# Show size of files in current directory
$ du -h
4.0K    ./file1.txt
8.0K    ./file2.txt
12K     .

Common Use Cases

  1. System Information

    # Get system architecture
    $ uname -m
    
    # Check kernel version
    $ uname -r
    
  2. Disk Space Monitoring

    # Check disk usage
    $ df -h
    
    # Find large directories
    $ du -h --max-depth=1 /
    
  3. Troubleshooting

    # Identify full file systems
    $ df -h
    
    # Find large files/directories
    $ du -h --max-depth=1 | sort -hr | head -n 5
    

Tips and Tricks

  1. Scripting with uname

    # Detect operating system
    if [ "$(uname -s)" == "Linux" ]; then
        echo "Running on Linux"
    fi
    
  2. Automating df Checks

    # Monitor disk usage and send alerts
    while true; do
        df -h | awk '$5 >= 90 {print "Disk usage high on " $6}'
        sleep 60
    done
    
  3. Recursive du Scans

    # Find the largest directories
    du -h --max-depth=1 / | sort -hr | head -n 5
    

Best Practices

  1. Monitor Disk Space Regularly

    # Set up alerts for low disk space
    df -h | awk '$5 >= 90 {print "Disk usage high on " $6}'
    
  2. Automate System Information Gathering

    # Collect system details for reporting
    uname -a > system_info.txt
    
  3. Use du Carefully on Large Filesystems

    # Limit depth for large directories
    du -h --max-depth=2 /var/log
    

Common Errors and Solutions

  1. Permission Denied

    # Use sudo for restricted directories
    sudo du -h /var/log
    
  2. Filesystem Not Found

    # Check if the filesystem is mounted
    df /mnt/external_drive
    
  3. Slow du Command

    # Limit depth or use -s option
    du -h --max-depth=1 /
    du -sh /var/log
    

Advanced Usage

#!/bin/bash
# Track disk usage over time
while true; do
    df -h | awk '$5 >= 90 {print "$(date): Disk usage high on " $6 " (" $5 ")"}'
    sleep 3600  # Check every hour
done

2. Identifying Large Files

#!/bin/bash
# Find the largest files in a directory
dir="/var/log"
du -h "$dir" | sort -hr | head -n 10 | awk -F'\t' '{print $2, $1}'

3. Automating System Information Collection

#!/bin/bash
# Gather system information for reporting
echo "System Information:"
uname -a
echo "Disk Usage:"
df -h
echo "Memory Usage:"
free -h

The uname, df, and du commands provide valuable information about your Linux system’s hardware, software, and disk usage. Mastering these commands is essential for DevOps professionals to effectively monitor, troubleshoot, and manage their infrastructure.


Previous Post
Linux ip, ping, and traceroute Commands: Network Troubleshooting Essentials
Next Post
Linux apt, yum, and dnf Commands: Package Management Essentials