How to use the command 'df' (with examples)

How to use the command 'df' (with examples)

The df command is a powerful utility in Unix-like operating systems used to display the amount of available disk space on file systems. It provides an overview of the filesystem disk space usage, showing information about total space, used space, available space, and the percentage of disk space used on the available drives. It’s a helpful tool for system administrators and users to monitor disk usage and manage efficient resource allocation.

Use case 1: Display an Overview of Filesystem Disk Space Usage

Code:

df

Motivation: In its simplest form, the df command provides an immediate overview of all mounted filesystems and their current disk space usage. This is the most straightforward way to quickly assess disk space on all drives in a system. It’s especially useful for system administrators who need to regularly monitor the health and storage capacity of multiple filesystems to prevent unexpected issues due to limited storage.

Explanation: The command df without any options displays the disk space usage for all mounted filesystems. The output is shown in 512-byte blocks, the default unit of measurement for the command. Each line in the output includes the filesystem name, total space, used space, available space, percentage of space used, and the location where the filesystem is mounted.

Example Output:

Filesystem    512-blocks      Used  Available  Capacity  Mounted on
/dev/disk1s1  195404320    47582144  145406176      25%   /
devfs               391         391         0      100%   /dev

Use case 2: Display Disk Usage in Human-Readable Format with Grand Total

Code:

df -h -c

Motivation: Sometimes, interpreting disk space in terms of bytes can be confusing, especially if you’re dealing with massive storage sizes. Using the human-readable option makes it easier for humans to understand disk space usage by converting bytes into more digestible units like Kilobytes (K), Megabytes (M), Gigabytes (G), etc. Additionally, having a grand total summarizing all filesystems can be useful for getting a quick summary without manual calculations.

Explanation:

  • -h: This option stands for human-readable format, which displays sizes in powers of 1024, typically showing disk usage in the more familiar units like K, M, and G.
  • -c: This option calculates and displays a grand total of all listed filesystems at the end of the output. This gives a comprehensive sense of the total disk usage relative to the whole system.

Example Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/disk1s1    186G   45G  138G  25% /
devfs           191K  191K    0K 100% /dev
total           186G   45G  138G  25%

Use case 3: Display Disk Usage Using Human-Readable Units Based on 1000

Code:

df --si

Motivation: This usage is valuable for those accustomed to seeing file sizes based on powers of 1000 (as opposed to 1024). In many formats and reports, especially in advertising and the commercial market, storage sizes are often quantified using the Decimal SI standard (base 10). This can offer better alignment with vendor specifications and provide a standardized view of disk space.

Explanation:

  • --si: This option uses the SI standard to calculate space, using powers of 1000 rather than 1024, which can often result in slightly smaller numbers for sizes, reflecting a base 10 representation of data sizes.

Example Output:

Filesystem      Size   Used  Avail  Use%  Mounted on
/dev/disk1s1    199G    48G   142G   25%  /
devfs           197k   197k     0k  100%  /dev

Use case 4: Display Filesystem Disk Usage for Specific Path

Code:

df path/to/file_or_directory

Motivation: With file servers or systems with many mounted filesystems, sometimes you need to understand disk usage specific to a particular file or directory. This function simplifies the task of determining which filesystem a particular file or directory resides on and provides the associated disk usage statistics for that filesystem.

Explanation:

  • path/to/file_or_directory: By specifying a specific file path or directory, the df command will show the disk usage for the filesystem containing that path, allowing users to assess how much space a specific part of the filesystem is utilizing.

Example Output:

Filesystem     512-blocks      Used  Available  Capacity  Mounted on
/dev/disk1s1  195404320    47582144  145406176      25%   /

Use case 5: Display Inode Information and Filesystem Types

Code:

df -iT

Motivation: Asides from space usage, inode availability can be a critical aspect of disk management, as inodes represent the index entries for files and directories. A system can run out of inodes even if space appears available. This command provides a comprehensive view of both space and inode usage, as well as the filesystem types, which adds contextual information regarding the structure and capacity of each filesystem.

Explanation:

  • -i: This option includes inode counts in the output, showing used inodes and free inodes for each filesystem.
  • -T: This option adds a column showing filesystem type, which helps clarify what kind of storage or limitations each filesystem might have.

Example Output:

Filesystem     Type    Inodes  IUsed   IFree IUse%  Mounted on
/dev/disk1s1   ext4  244060   82094   161966  35%  /
devfs           dev   191      191       0  100%  /dev

Use case 6: Use 1024-byte Units for Disk Space Figures

Code:

df -k

Motivation: For those who prefer understanding disk usage in Kilobytes (KB), this command allows a consistent display of values in 1024-byte units. Kilobytes can be an ideal compromise between overly precise bytes and the significantly larger Megabytes or Gigabytes, making the output practical in a technical context.

Explanation:

  • -k: Requests the output in 1024-byte units (kilobytes), offering a balance between detail and readability without abstracting into larger units.

Example Output:

Filesystem    1024-blocks      Used Available Capacity Mounted on
/dev/disk1s1   97702160   23791072  72703088     25%  /
devfs               191        191        0    100%  /dev

Use case 7: Display Information in a Portable Output Format

Code:

df -P

Motivation: Portability in output is crucial when creating scripts or automation tasks that interact with disk usage data in a consistent, cross-system manner. This option ensures that the output format is uniform and minimal, making it robust for parsing in various text processing or script operations.

Explanation:

  • -P: Ensures that output is consistent in spacing and formatting, suitable for text parsing via scripts or for systems where the df command may vary between different Unix-like operating systems.

Example Output:

Filesystem    1024-blocks     Used Available Capacity Mounted on
/dev/disk1s1   97702160  23791072  72703088     25%  /
devfs               191       191        0    100%  /dev

Conclusion

The df command offers flexible and powerful insights into disk space usage across Unix-like systems. Using these options, users can tailor the output to meet specific needs, whether for system monitoring, management, automation, or even for educational purposes. With its combination of simplicity and diversity in functionality, df remains an indispensable tool in the toolkit of system administrators and developers alike.

Tags :

Related Posts

How to use the command 'bitwise' (with examples)

How to use the command 'bitwise' (with examples)

The bitwise command is a versatile tool designed for users who need an interactive and multifaceted calculator capable of handling complex base conversions and intricate bit manipulations seamlessly.

Read More
How to use the command 'xml canonic' (with examples)

How to use the command 'xml canonic' (with examples)

The xml canonic command from the XMLStarlet suite is a powerful tool designed for transforming XML documents into their canonical forms.

Read More
How to Use the Command 'git add' (with Examples)

How to Use the Command 'git add' (with Examples)

The git add command is fundamental in Git operations, allowing users to add changes in the working directory to the staging area.

Read More