5. How do you check the disk space usage on a Unix system, and what commands would you use for this task?

Advanced

5. How do you check the disk space usage on a Unix system, and what commands would you use for this task?

Overview

Checking disk space usage on a Unix system is a fundamental skill for system administrators and developers, crucial for monitoring system health, performing maintenance, and ensuring that applications have enough space to operate. The Unix operating system provides various commands to inspect disk usage, enabling users to manage system resources effectively.

Key Concepts

  • df (disk free): Displays the amount of disk space available on the file system.
  • du (disk usage): Shows the disk space used by files and directories.
  • Filesystem Hierarchy: Understanding how disk space is distributed across different directories and mounts in Unix.

Common Interview Questions

Basic Level

  1. How do you display the total disk space and free space on all mounted filesystems in Unix?
  2. What is the command to find the size of a specific directory, including its subdirectories?

Intermediate Level

  1. How can you display disk usage of all files and directories in a human-readable format?

Advanced Level

  1. Describe how to find and sort the top 5 directories consuming the most disk space within a given directory.

Detailed Answers

1. How do you display the total disk space and free space on all mounted filesystems in Unix?

Answer: The df command is used to display the amount of disk space available on all mounted filesystems. Using df -h (human-readable) option makes the output easier to understand by displaying values in KB, MB, or GB.

Key Points:
- df displays disk space usage for all mounted filesystems.
- The -h option makes the output human-readable.
- It's a quick way to monitor the overall disk space usage.

Example:

// This is a Unix command, not C#, so demonstration through comments
// To display total disk space and free space on all mounted filesystems
df -h
// Output example:
// Filesystem      Size  Used Avail Use% Mounted on
// /dev/sda1       100G   35G   65G  35% /

2. What is the command to find the size of a specific directory, including its subdirectories?

Answer: The du command is used to estimate file space usage, and du -sh <directory> provides the aggregate size of a directory and its subdirectories in a human-readable format.

Key Points:
- du estimates the space a directory uses.
- The -s option provides a summary for the specified directory.
- The -h option makes the size human-readable.

Example:

// Again, this is a Unix command, shown through comments
// To find the size of a specific directory including its subdirectories:
du -sh /path/to/directory
// Output example:
// 1.5G    /path/to/directory

3. How can you display disk usage of all files and directories in a human-readable format?

Answer: Use the du -ah command in a directory to list the disk usage of all files and directories within it in a human-readable format. The -a flag includes all files, and -h makes the output human-readable.

Key Points:
- du -ah lists disk usage for all files and directories.
- The -a option includes all files in the output.
- The -h option ensures sizes are easy to understand.

Example:

// Unix command example through comments
// Display disk usage of all files and directories:
du -ah /path/to/check
// Output might include both files and directories with sizes in a readable format.

4. Describe how to find and sort the top 5 directories consuming the most disk space within a given directory.

Answer: You can combine the use of du, sort, and head commands to achieve this. First, use du to get the size of each directory within the specified directory. Then, use sort to order them by size, and head to pick the top 5.

Key Points:
- du -h calculates sizes in a human-readable format, but for sorting, avoid -h.
- sort -nr sorts the output numerically and in reverse (largest first).
- head -n 5 lists the top 5 lines of the sorted output.

Example:

// Explained via comments as it involves Unix commands
// To find and sort the top 5 directories consuming the most disk space:
du -s /path/to/directory/* | sort -nr | head -n 5
// Note: Replace '/path/to/directory/' with the actual directory path you want to inspect.
// This command will list the top 5 directories by size.

This guide covers the essentials of checking disk space usage on Unix systems, from basic to advanced levels, using commands like df, du, combined with sorting and filtering techniques.