Skip to content

‘df’ Command in Linux with Examples

The df command is an essential tool for any Linux user or system administrator. It provides a comprehensive overview of disk space usage on a system, allowing users to monitor available space, identify potential issues, and troubleshoot common problems.

In this article, we will explore the df command in depth, providing examples of its basic usage, advanced features, and troubleshooting tips.

Key Takeaways

  • The df command is an essential tool for monitoring disk space usage on a Linux system.
  • Basic usage of the df command involves typing “df” into the terminal to display a summary of disk usage.
  • Advanced usage of the df command involves using options and arguments to customize the output and provide more detailed information.

Understanding the df Command

The df command in Linux is used to display the amount of disk space available and used on file systems. It can be used to check the available space on a single file system or on all file systems mounted on the system.

When using the df command, it is important to understand the output. The output shows the file system name, total size, used space, available space, used percentage, and mount point.

To make the output more readable, it is recommended to use the -h flag, which displays the sizes in a human-readable format. For example, instead of displaying the size in bytes, it will display it in gigabytes or megabytes.

Here is an example of the df command with the -h flag:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  5.6G   14G  29% /
/dev/sda2        50G   10G   38G  21% /home
/dev/sdb1       200G  150G   50G  75% /mnt/data

In the above example, the output shows three file systems: /dev/sda1, /dev/sda2, and /dev/sdb1. The Size column shows the total size of the file system, the Used column shows the amount of space used, the Avail column shows the amount of space available, and the Use% column shows the percentage of space used. The Mounted on column shows the mount point of the file system.

Using the df command with the -i flag can display the inode usage information. The output will show the total number of inodes, the number of used inodes, and the number of available inodes.

Overall, the df command is a useful tool for checking disk space usage on a Linux system. By understanding the output and using the appropriate flags, users can easily monitor disk space usage and prevent potential issues caused by running out of disk space.

Basic Usage of df Command

The df command is a popular utility in Linux used to display the disk space usage of file systems. It can be helpful in managing storage space on a system. In this section, we will discuss the basic usage of the df command with examples.

Display File System Disk Space Usage

To display the disk space usage of file systems, you can use the df command followed by the -h option. This option displays the output in a human-readable format, making it easier to understand. The following is an example of how to use the df command with the -h option:

$ df -h

This command will display the disk space usage of all the file systems on the system in human-readable format.

Show Output in Human-Readable Format

By default, the df command displays the output in 1K blocks. However, you can use the -h option to display the output in a more readable format. The -h option displays the output in units of K, M, G, or T, depending on the size of the file system. The following is an example of how to use the df command with the -h option:

$ df -h /dev/sda1

This command will display the disk space usage of the file system mounted at /dev/sda1 in human-readable format.

In conclusion, the df command is a useful tool for managing disk space usage on a Linux system. By using the -h option, you can display the output in a more readable format, making it easier to understand.

DF Command Options

The df command in Linux provides information about the disk space usage of mounted file systems. It can be used to display information about the total disk space, used space, and available space on a file system. Additionally, df can be used to display inode information and use POSIX output format.

Including and Excluding File Systems

The -x option can be used to exclude certain file systems from the output of df. For example, to exclude the /dev file system, the following command can be used:

$ df -x dev

On the other hand, the -t option can be used to include only certain file systems in the output of df. For example, to display information only about the ext4 file system, the following command can be used:

$ df -t ext4

Displaying Inodes Information

In addition to displaying disk space usage, df can also be used to display information about inodes. The -i option can be used to display inode information for each file system. For example:

$ df -i

This will display the number of inodes used and available for each file system.

Using POSIX Output Format

The -P option can be used to display output in POSIX format. This format is useful for scripting and parsing the output of df. For example:

$ df -P

This will display output in the following format:

Filesystem     1024-blocks  Used Available Capacity Mounted on
/dev/sda1           123456  7891    115565     7%    /

Overall, the df command provides a useful way to display disk space usage information in Linux. By using options such as -x, -t, -i, and -P, users can customize the output to meet their specific needs.

Analyzing df Output

Understanding File System Types

The df command in Linux displays information about the file system usage on the system. It shows the amount of disk space used and available on each file system. The output of the df command can vary depending on the type of file system being used.

There are several common file system types in Linux, including ext2, ext3, ext4, btrfs, xfs, and tmpfs. Each file system type has its own characteristics and features. For example, ext4 is the default file system type in most Linux distributions and provides support for larger file sizes and faster file system checks.

When analyzing the output of the df command, it is important to understand the file system type being used. This can help in identifying any potential issues or limitations with the file system.

Interpreting Mount Points

The df command also displays information about the mount points on the system. A mount point is a directory on the file system where a separate file system is mounted. The output of the df command shows the mount point, the file system type, and the amount of disk space used and available on that file system.

When analyzing the output of the df command, it is important to understand the purpose of each mount point. For example, the / mount point is the root directory of the file system and contains all of the files and directories on the system. Other mount points may be used for specific purposes, such as mounting a separate file system for storing user data.

In summary, understanding the file system types and mount points is crucial when analyzing the output of the df command. This information can help in identifying any potential issues or limitations with the file system and can aid in troubleshooting any problems that may arise.

Advanced Examples of df Command

Combining df with Other Commands

The df command can be combined with other commands to get more detailed information about disk usage. For example, to get a list of the top 10 largest files in a directory, you can use the du command to find the file sizes and then sort the output using the sort command. Finally, you can use the head command to display only the top 10 results.

du -a /path/to/directory | sort -n -r | head -n 10

Another useful command is grep, which can be used to filter the output of df based on specific criteria. For example, to display only the file systems that are mounted on a specific device, you can use the following command:

df -h | grep /dev/sda1

This will display only the file system that is mounted on /dev/sda1.

Scripting with df Command

The df command can also be used in scripts to automate disk usage monitoring. For example, you can create a script that checks the disk usage of a file system and sends an email alert if the usage exceeds a certain threshold.

#!/bin/bash

THRESHOLD=90
FILESYSTEM=/dev/sda1

df -h $FILESYSTEM | awk '{ print $5 }' | tail -n 1 | sed 's/%//' | while read USAGE
do
  if [ $USAGE -gt $THRESHOLD ]; then
    echo "Disk usage on $FILESYSTEM is above $THRESHOLD%" | mail -s "Disk Alert" [email protected]
  fi
done

This script checks the disk usage of /dev/sda1 and sends an email alert if the usage exceeds 90%. The awk, tail, and sed commands are used to extract the usage percentage from the output of df. The while loop reads the usage percentage and compares it to the threshold value. If the usage is above the threshold, an email alert is sent using the mail command.

Troubleshooting Common df Command Issues

Resolving Disk Space Discrepancies

Sometimes, the output of the df command may show discrepancies in the amount of disk space used or available. One common reason for this is that some files or directories may be hidden or inaccessible to the user running the command. To resolve this issue, the user can try running the df command with the -a option to display all files and directories, including hidden ones.

Another reason for disk space discrepancies could be that some files or directories are being used by other processes and are therefore not visible to the user running the command. In this case, the user can try running the lsof command to identify which processes are using the files or directories in question. Once the processes have been identified, the user can stop them or move the files to a different location.

Handling df Command Errors

Sometimes, the df command may fail to run or produce errors. One common error is the “File system is read-only” error, which occurs when the file system is mounted as read-only. To resolve this error, the user can try remounting the file system as read-write using the mount command.

Another common error is the “No such file or directory” error, which occurs when the file or directory specified in the command does not exist. To resolve this error, the user can double-check the file or directory name and path, and ensure that they are correct.

In some cases, the df command may produce inaccurate or incomplete output. This could be due to various reasons, such as file system errors or hardware failures. To resolve this issue, the user can try running the fsck command to check and repair file system errors, or run hardware diagnostics to identify any hardware issues.

Best Practices for Monitoring Disk Usage

Monitoring disk usage is an essential task for system administrators to ensure that the system runs smoothly and efficiently. The df command is a powerful tool that can be used to monitor disk usage in Linux systems. Here are some best practices for using the df command:

1. Use the -h option

When running the df command, it is recommended to use the -h option, which displays the output in a human-readable format. This makes it easier to understand the disk usage information, as the sizes are displayed in a more understandable format, such as “10G” instead of “10000000”.

2. Focus on critical filesystems

System administrators should focus on monitoring critical filesystems such as root, var, and home directories. These filesystems are essential for the system to function correctly, and any issues with them can cause significant problems.

3. Schedule regular checks

Regular checks of disk usage can help identify potential issues before they become critical. It is recommended to schedule regular checks using tools such as cron or systemd timers to ensure that the system is always monitored.

4. Use visual aids

Using visual aids such as graphs or charts can make it easier to understand the disk usage information. Tools such as gnuplot or rrdtool can be used to create visual representations of the disk usage data.

By following these best practices, system administrators can effectively monitor disk usage and ensure that their systems run smoothly and efficiently.

Frequently Asked Questions

How do you display disk space usage in human-readable format using the df command?

To display disk space usage in human-readable format using the df command, you can use the ‘-h’ option with the command. For example, the command ‘df -h’ will display disk space usage in gigabytes, megabytes, and kilobytes instead of the default block size.

What is the difference between ‘df’ and ‘du’ commands in Linux?

The ‘df’ command displays information about the file system usage, including the total size, used space, and available space on a file system. On the other hand, the ‘du’ command displays the disk usage of files and directories in a file system.

How can you interpret the output of the df command?

The output of the df command shows the total size, used space, and available space on a file system. The ‘Filesystem’ column shows the device or partition name, while the ‘Mounted on’ column shows the mount point. The ‘Use%’ column shows the percentage of used space on the file system.

What are the common options used with the df command to customize its output?

The common options used with the df command to customize its output include ‘-h’ to display disk space usage in human-readable format, ‘-T’ to display the file system type, ‘-i’ to display the inode usage, and ‘-x’ to exclude file systems of a specific type.

How can you check the file system type along with the disk space usage with df?

To check the file system type along with the disk space usage with df, you can use the ‘-T’ option with the command. For example, the command ‘df -Th’ will display disk space usage in human-readable format along with the file system type.

What method is used to display only specific file systems with the df command?

To display only specific file systems with the df command, you can use the ‘-x’ option with the command and specify the file system type to exclude. For example, the command ‘df -x tmpfs -h’ will exclude the tmpfs file system and display disk space usage in human-readable format for all other file systems.

Last Updated on February 10, 2024 by admin

Share this post on social

Copyright 2022 Linuxguidehq.com. All rights reserved. No part of this website may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the copyright owner.