Preface
On Linux systems, understanding disk information in the system is crucial for system administrators and users. By understanding the disk situation on the system, you can effectively manage storage space, diagnose problems, and optimize performance.
df command
df
Commands are a commonly used tool to display the disk space usage of the file system. It lists the relevant information about the mounted file system, including the name of the file system, the mount point, the total size, used space, available space, and the percentage of use.
How to use:
df -h
-h
Options indicate that the results are displayed in a human-readable format, which will be easier to understand.
Sample output:
File system capacity Used Available Used% Mounting point /dev/sda1 20G 8.4G 11G 44% / /dev/sdb1 100G 60G 40G 60% /mnt/data
In this example, we can see two file systems/dev/sda1
and/dev/sdb1
, they are mounted in the root directory respectively/
and/mnt/data
, displays their capacity, used space, available space, and usage percentage.
du command
du
The command is used to estimate the disk space usage of a specified file or directory. It can recursively calculate the disk usage of a directory and its subdirectories and report it in bytes.
How to use:
du -h /path/to/directory
-h
The option also means displaying the results in a human-readable format.
Sample output:
4.0K /path/to/directory/subdirectory1 2.0M /path/to/directory/subdirectory2 1.5G /path/to/directory/subdirectory3
In this example, we/path/to/directory
The directory is useddu
Command, which recursively displays disk usage for the directory and its subdirectories, displayed in a human-readable format.
lsblk command
lsblk
Commands are used to list block device information in the system, including disks, partitions and their relationships. It provides an intuitive view showing the hierarchy of all block devices in the system.
How to use:
lsblk
Sample output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk └─sda1 8:1 0 20G 0 part / sdb 8:16 0 100G 0 disk └─sdb1 8:17 0 100G 0 part /mnt/data
In this example,lsblk
The command displays two disks in the system.sda
andsdb
, and their respective partition information. You can see the name of each device, the primary/secondary device number, whether it is removable, size, read-only attributes, type, and mount point.
fdisk command
fdisk
Command is a tool for disk partitioning, which allows users to partition disks on the system, edit partition tables, etc.
How to use:
fdisk -l
-l
Options represent the partition information of all disks in the system.
Sample output:
disk /dev/sda:20 GiB,20971520000 byte,40960000 Sectors ... /dev/sda1 2048 40959999 40957952 20G 83 Linux disk /dev/sdb:100 GiB,107374182400 byte,209715200 Sectors ... /dev/sdb1 2048 209715199 209713152 100G 83 Linux
In this example,fdisk
The command displays partition information of all disks in the system, including the start sector, end sector, size, type, etc. of the partition.
ls command
Althoughls
Commands are usually used to list files and subdirectories in directories, but they can also be used to view some special files, such as device files.
How to use:
ls -l /dev/sd*
This command will be listed/dev
In the directorysd
The device file at the beginning, including the hard disk device file.
Sample output:
brw-rw---- 1 root disk 8, 0 Jan 1 00:00 /dev/sda brw-rw---- 1 root disk 8, 1 Jan 1 00:00 /dev/sda1 brw-rw---- 1 root disk 8, 16 Jan 1 00:00 /dev/sdb brw-rw---- 1 root disk 8, 17 Jan 1 00:00 /dev/sdb1
In this example, we usels
The command lists two disk devices/dev/sda
and/dev/sdb
, and their partitions/dev/sda1
and/dev/sdb1
。
cat command
cat
Commands are usually used to connect files and print their contents, but in/proc
In the file system, there are some special files used to display system information, including disk information.
How to use:
cat /proc/partitions
This command prints out a list of all partitions in the system, including disks, partitions, and their sizes.
Sample output:
major minor #blocks name 8 0 20971520 sda 8 1 20971392 sda1 8 16 104857600 sdb 8 17 104857568 sdb1
In this example, we usecat
The command has been printed/proc/partitions
The content of the file displays the information of all disks and partitions in the system, including device numbers, sizes, etc.
mount command
mount
The command is used to mount the file system to a specified mount point, and can also be used to view mounted file system information.
How to use:
mount | grep '^/dev'
This command will list all mounted file systems and then passgrep
Filter out to/dev
The equipment at the beginning.
Sample output:
/dev/sda1 on / type ext4 (rw,relatime) /dev/sdb1 on /mnt/data type ext4 (rw,relatime)
In this example, we usemount
The command lists all mounted file systems and passesgrep
Filtered out/dev
The devices at the beginning display their mounting information.
The above is the detailed explanation of the seven commands that list disk information on Linux. For more information about the commands that list disk information on Linux, please pay attention to my other related articles!