Problem Statement
Explain the df and du commands. How do you check disk space usage and find large files or directories?
Explanation
The df (disk free) command displays filesystem disk space usage showing total, used, and available space for mounted filesystems. Use df -h for human-readable output (GB, MB instead of bytes), df -T to show filesystem types, or df /path to check specific filesystem. Df helps monitor disk capacity and identify full filesystems before they cause issues.
The du (disk usage) command estimates file and directory space usage. Use du -h for human-readable sizes, du -s for summary (total only), or du -a to include files (not just directories). du --max-depth=1 shows first-level subdirectories only. Example: du -sh /home/* shows size of each directory in /home, useful for finding what's consuming space.
Find large files with find / -type f -size +1G to list files over 1GB, or combine with du: find /var -type f -exec du -h {} \; | sort -rh | head -20 shows 20 largest files. Use ncdu (NCurses Disk Usage) for interactive browsing of directory sizes with a nice interface, though it's not always installed by default.
For disk usage monitoring, regularly check df output to catch filesystems approaching capacity. Use du to identify large directories, then investigate further. Setting up monitoring alerts when filesystems reach 80-90% capacity prevents production issues. Understanding these tools is crucial for system administration and capacity planning.
