preface

This article focuses on three common Commands for Linux disk management: df, du, and fdisk.

The df command

Df display free disk Space: displays the available and used space of each disk.

yangan@yangan-X555LI:~$df -h Filesystem Size Used Avail Use% Mounted on udev 5.8g 0 5.8g 0% /dev TMPFS 1.2g 2.3m 1.2g 1% /run /dev/sdb2 110G 38G 66G 37% / /dev/sda1 100M 44M 57M 44% /boot/efi TMPFS 1.2g 20K 1.2g 1% /run/user/121 TMPFS 1.2g 60K 1.2g 1% /run/user/1000 /dev/sda5 141G 74G 67G 53% /media/yangan/Program TMPFS 1.2g 0 1.2g 0% /run/user/0Copy the code

/dev/sda1: a indicates the first hard disk. If it is the second hard disk, the value is B, and so on. 1 indicates the primary partition. 1, 2, 3, and 4 are primary partitions. Logical partitions start from the fifth partition and reach a maximum of 16.

In Linux, a hard disk can have a maximum of four primary partitions (including extended partitions), and each extended partition has a primary partition number. That is, a hard disk can have a maximum of four primary partitions and extended partitions.

Du command

The full name of DU is display disk Usage Statistics, which shows disk usage. Df is a macro view of disk usage, while DU is a micro view of file size.

#-b: displays the directory or file size, in bytes
$ du -b test.log
363	test.log

#-h: displays the directory or file size in a human-readable way
$ du -h test.log4.0 K test. The log
#By default, directories and subdirectories are displayed
$ du -h test1/
4.0K	test1/test/test1/test2
8.0K	test1/test/test1
12K	test1/test
16K	test1/

#-s: indicates that subdirectories are not displayed
$ du -sh test1/
16K	test1/
Copy the code

Fdisk command

Fdisk is a disk partitioning tool for viewing disk partitions. Sometimes a new hard disk is added to the machine, but it is not mounted. This cannot be checked by using df -h. You need to check by using fdisk -l.

1. Check the disk partition information

$ fdisk -lDisk /dev/sdb: 536.9 GB, 536870912000 bytes, 1048576000 Sector Units = Sector of 1 x 512 = 512 bytes Sector size (logical/physical) : 512 bytes / 512 bytes I/O size (minimum/best) : 512 bytes / 512 bytes...Copy the code

2. Partition the disk (carefully try to back it up!)

$ fdisk /dev/sdbCommand (m for help): n # Leave the rest of the prompts as default
#Save the partition
Command (m for help): w 

#Format partitions. Centos7 uses ext4 file system
mkfs.ext4 /dev/sdb1

#Mount the disk to the data directory
mount /dev/sdb1 /data

#The disk is automatically mounted
vim /etc/fstab  
>/dev/sdb1 /data ext4 defaults 0 0
Copy the code

Write in the last

Dear bosses, creation is not easy, but it needs to be honed and summarized constantly, welcome to pay attention to me, I am Yan Gan, I will share with you all kinds of practical programming knowledge and skills, your praise and attention is the biggest motivation for my progress and creation!!