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
-s
: Print the kernel name-r
: Print the kernel release-m
: Print the machine hardware name-p
: Print the processor type-a
: Print all the above information
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
-h
: Print sizes in human-readable format-i
: Show inode information instead of block usage-T
: Show file system type
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
-h
: Print sizes in human-readable format-s
: Display only a total for each argument-d N
: Limit the depth of the directory tree to N
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
-
System Information
# Get system architecture $ uname -m # Check kernel version $ uname -r
-
Disk Space Monitoring
# Check disk usage $ df -h # Find large directories $ du -h --max-depth=1 /
-
Troubleshooting
# Identify full file systems $ df -h # Find large files/directories $ du -h --max-depth=1 | sort -hr | head -n 5
Tips and Tricks
-
Scripting with uname
# Detect operating system if [ "$(uname -s)" == "Linux" ]; then echo "Running on Linux" fi
-
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
-
Recursive du Scans
# Find the largest directories du -h --max-depth=1 / | sort -hr | head -n 5
Best Practices
-
Monitor Disk Space Regularly
# Set up alerts for low disk space df -h | awk '$5 >= 90 {print "Disk usage high on " $6}'
-
Automate System Information Gathering
# Collect system details for reporting uname -a > system_info.txt
-
Use du Carefully on Large Filesystems
# Limit depth for large directories du -h --max-depth=2 /var/log
Common Errors and Solutions
-
Permission Denied
# Use sudo for restricted directories sudo du -h /var/log
-
Filesystem Not Found
# Check if the filesystem is mounted df /mnt/external_drive
-
Slow du Command
# Limit depth or use -s option du -h --max-depth=1 / du -sh /var/log
Related Commands
lsblk
: List block devicesfdisk
: Partition table manipulatorparted
: Partition table manipulatormount
: Mount a filesystem
Advanced Usage
1. Monitoring Disk Space Trends
#!/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.