Linux command to display disk space usage: df

In Linux systems, dfthe command is a utility used to display disk space usage. Its name comes from "disk free". Through dfcommands, users can easily understand the disk usage of each file system in the system, including total space, used space, free space and other information.

dfThe commands are mainly used in the following aspects:

  • Display statistics for all mounted file systems.
  • Provides the total capacity, used space, free space, and usage percentage of the file system space.
  • Supports displaying data in a human-readable format to facilitate user understanding and analysis.

Basic syntax of df command

Command format

dfThe basic format of the command is as follows:

df [选项] [文件系统]
  • 选项: Used to specify dfdifferent behaviors of commands, which can be selected according to needs.
  • 文件系统: Optional parameter used to specify to display information about a specific file system.

Command options

The following are some commonly used dfcommand options:

  • -h: Displays disk space information in human-readable format, using units such as K, M, G, etc.
  • -T: Display the file system type.
  • -a: Display all file systems, including virtual file systems.
  • --total: Displays total information for all columns.

dfThe parameters of the command are mainly the mount point of the file system. By specifying the mount point, the corresponding file system information can be filtered and displayed.

Example

1. Basic use

df

The above command will display the disk space usage of all mounted file systems.

2. Display in human-readable format

df -h

This command displays the file system's disk space information in more readable units (such as K, M, G).

3. Display the specified file system

df /dev/sda1

The above command only displays /dev/sda1the disk space usage of the file system.

Common usage of df command

Show file system information

1. Display all mounted file systems

df -a

By using -athe options, dfthe command will display all file systems, including virtual file systems, and the corresponding disk space usage.

2. Only display the specified file system

df /dev/sda1

Using the file system's mount point as a parameter allows you to display disk space information only for a specific file system.

Show disk space usage

1. Display in human-readable format

df -h

By adding -hoptions, dfthe command displays disk space information in a human-readable format, using units such as K, M, G, etc., making it easier to read.

2. Display the space usage of the specified directory

df -h /home/user

Specify the directory path as a parameter to view the disk space usage of the file system where the directory is located.

Advanced usage of df command

List inode information

df -i

By adding -ioptions, dfthe command will display inode information for the file system, including the number of inodes used, the number of remaining inodes, and the inode usage.

Monitor disk space changes

watch df -h

Using watchcommand combination df -h, you can monitor changes in disk space in real time. This is useful for promptly detecting excessive or drastic changes in space usage.

###Check file system type

df -T

By adding -Toptions, dfthe command will display file system types including ext4, xfs, etc. This is very helpful for understanding the types of file systems in the system.

Practical examples of df command

Monitor space on critical file systems

Sometimes, we need to regularly monitor the space of critical file systems to ensure the normal operation of the system. Here is a simple case:

watch df -h / /home /var

The above command will monitor the disk space changes of the root directory (/), user home directory (/home) and /var directory in real time. This is useful for quickly identifying any file system space usage that exceeds a threshold.

Case 2: Find directories that occupy large disk space

Sometimes, we need to find out the directory that takes up a lot of disk space so that we can free up space or optimize it in time. The following is an example:

du -h --max-depth=1 / | sort -hr

This command uses duthe command to find the disk space usage of each subdirectory under the root directory, and sortsorts it in reverse order according to the space size. This way, users can easily find the directories that take up the most disk space.

Case 3: Regularly check the health status of the file system

Regularly checking the health of your file system is part of system management. The following is an example:

df -hT > /var/log/df_health_check.log

The above command dfsaves the output of the command to /var/log/df_health_check.loga file. By setting up periodic tasks, you can view the historical space usage of the file system in the log and check whether the file system type is normal.

Frequently asked questions and solutions

Not enough disk space

Disk space is reported as full, preventing the system from functioning properly.

Use dfcommands to find directories or files that take up a lot of space, and consider cleaning or migrating unnecessary data. If there is insufficient space, you may want to consider expanding the disk capacity.

Mount point error

A file system cannot be mounted.

Check /etc/fstabthe mount information in the file to make sure the device and mount point are set correctly. Use mount -athe command to check and mount all /etc/fstabfile systems defined in.

File system corruption

dfThe command displays the file system as read-only or reports that the file system is corrupt.

Use fsckcommands to check and repair file systems. In single-user mode, run fsckto fix a file system that cannot be mounted.

“df: ‘/path/to/mount_point’: Stale file handle” 错误

"Stale file handle" error is displayed.

This may be caused by an expired mount point or a problem with the network file system. Remount the file system, check the network connection or restart related services.

When using dfcommands, there are some considerations and suggestions that can help users manage disk space more efficiently and avoid potential problems.

Regularly use dfcommands to check disk space usage to ensure that insufficient space problems are discovered and resolved in a timely manner. Set up regular tasks or monitoring system tools to monitor space changes in key file systems in real time.

File system corruption can result in severe data loss or system instability. Regularly use fsckcommands to check and repair the file system to ensure the health of the file system.

During the system planning and deployment stages, plan disk space allocation appropriately. Make sure critical file systems have enough space to prevent system crashes due to insufficient disk space.

Use dfthe command with caution, especially before deleting files or performing file system operations. Make sure you understand the options and parameters of the command to avoid unnecessary data loss or system failure.

Before performing disk space management operations, ensure that important files and data have been backed up. This prevents the risk of data loss due to misoperation.

Summarize

Through this article, we introduce dfthe commands in Linux in detail, and delve into their basic syntax, common usage, advanced usage, and actual cases. Understanding and being proficient in using dfthe command is crucial for system administrators and developers because it provides a comprehensive understanding of disk space usage and helps ensure the normal operation of the system.

dfThe command can not only be used to view the total capacity, used space and free space of the file system, but also can display the inode information of the file system, monitor changes in disk space, and even check the type of file system. Through actual cases, we demonstrate dfthe application of commands in monitoring key file systems and finding directories that occupy large disk space.

There are some common issues you need to pay attention to when using dfthe command, such as insufficient disk space, incorrect mount points, and file system corruption. Properly planning disk space, regularly checking file system health status, and backing up important files are key steps to ensure stable system operation.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/135398590