Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Linux disk management


May 22, 2021 Linux


Table of contents


Linux disk management

Linux disk management is directly related to the performance of the entire system.

Linux disk management uses three commands commonly for df, du, and fdisk.

  • df: Lists the overall disk usage of the file system
  • du: Check disk space usage
  • fdisk: For disk partitioning

Df

df command parameter function: Check the disk space footprint of the file system. T his command can be used to obtain information such as how much space the hard disk is taking up and how much space is left.

Grammar:

df [-ahikHTm] [目录或文件名]

Options and parameters:

  • -a: List all file systems, including system-specific /proc and other file systems;
  • -k: display each file system at KBytes capacity;
  • -m: display each file system at the capacity of MBytes;
  • -h: Self-displayed in formats such as GBytes, MBytes, KBytes, etc., which are easier for people to read;
  • -H: the way in which the M=1024K is replaced by M=1000K;
  • -T: displays the file system type, along with the partition's fileystem name (e.g. ext3);
  • -i: Does not use the hard drive capacity, but is displayed in the number of inodes

Instance 1

List all the file systems in the system!

[root@www ~]# df
Filesystem      1K-blocks      Used Available Use% Mounted on
/dev/hdc2         9920624   3823112   5585444  41% /
/dev/hdc3         4956316    141376   4559108   4% /home
/dev/hdc1          101086     11126     84741  12% /boot
tmpfs              371332         0    371332   0% /dev/shm

Under Linux, if df doesn't have any options, all (without special in-memory file systems and swaps) will be listed at 1 Kbytes capacity by default!

Instance 2

The capacity results are displayed in an easy-to-read capacity format

[root@www ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdc2             9.5G  3.7G  5.4G  41% /
/dev/hdc3             4.8G  139M  4.4G   4% /home
/dev/hdc1              99M   11M   83M  12% /boot
tmpfs                 363M     0  363M   0% /dev/shm

Instance 3

List all the special file formats and names in the system

[root@www ~]# df -aT
Filesystem    Type 1K-blocks    Used Available Use% Mounted on
/dev/hdc2     ext3   9920624 3823112   5585444  41% /
proc          proc         0       0         0   -  /proc
sysfs        sysfs         0       0         0   -  /sys
devpts      devpts         0       0         0   -  /dev/pts
/dev/hdc3     ext3   4956316  141376   4559108   4% /home
/dev/hdc1     ext3    101086   11126     84741  12% /boot
tmpfs        tmpfs    371332       0    371332   0% /dev/shm
none   binfmt_misc         0       0         0   -  /proc/sys/fs/binfmt_misc
sunrpc  rpc_pipefs         0       0         0   -  /var/lib/nfs/rpc_pipefs

Instance 4

Display the available disk capacity under /etc in an easy-to-read capacity format

[root@www ~]# df -h /etc
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdc2             9.5G  3.7G  5.4G  41% /

du

The Linux du command also sees the usage space, but unlike the df command, the Linux du command is a view of the space used by the file and directory disk, or is there a difference with the df command, which is described here.

Grammar:

du [-ahskm] 文件或目录名称

Options and parameters:

  • -a: Lists all file and directory capacity, because by default only the number of files under the directory is counted.
  • -h: shown in a capacity format (G/M) that is easier for people to read;
  • -s: List the total amount, not each individual directory occupancy capacity;
  • -S: Excludes totals in subdirections, which is a bit different from -s.
  • -k: shown in KBytes listed capacity;
  • -m: shown in MBytes listed capacity;

Instance 1

List all file capacity in the current directory

[root@www ~]# du
8       ./test4     <==每个目录都会列出来 8       ./test2 ....中间省略.... 12      ./.gconfd   <==包括隐藏文件的目录 220     .           <==这个目录(.)所占用的总量 

When you enter du directly without any options, du analyzes the hard disk space occupied by the files and directories in the current directory.

Instance 2

The capacity of the file is also listed

[root@www ~]# du -a
12      ./install.log.syslog   <==有文件的列表了 8       ./.bash_logout 8       ./test4 8       ./test2 ....中间省略.... 12      ./.gconfd 220     . 

Instance 3

Check the capacity occupied by each directory under the root

[root@www ~]# du -sm /*
7       /bin
6       /boot
.....中间省略....
0       /proc
.....中间省略....
1       /tmp
3859    /usr     <==系统初期最大就是他了啦! 77      /var 

Wildcard s to represent each directory.

Unlike df, the du command actually searches the file system for all the file data.


fdisk

fdisk is Linux's disk partition table operation tool.

Grammar:

fdisk [-l] 装置名称

Options and parameters:

  • -l: Output all partitioned content of the device that is followed. If there is only fdisk -l, the system lists the partitions of the devices that can be searched throughout the system.

Instance 1

List all partition information

[root@AY120919111755c246621 tmp]# fdisk -l

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1        2550    20480000   83  Linux
/dev/xvda2            2550        2611      490496   82  Linux swap / Solaris

Disk /dev/xvdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x56f40944

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb2               1        2610    20964793+  83  Linux

Instance 2

Find the disk on which the root of your system is located and check the hard drive for information

[root@www ~]# df /            <==注意:重点在找出磁盘文件名而已 
Filesystem           1K-blocks      Used Available Use% Mounted on 
/dev/hdc2              9920624   3823168   5585388  41% /  
[root@www ~]# fdisk /dev/hdc  <==仔细看,不要加上数字喔! 
The number of cylinders for this disk is set to 5005. 
There is nothing wrong with that, but this is larger than 1024, 
and could in certain setups cause problems with: 
1) software that runs at boot time (e.g., old versions of LILO) 
2) booting and partitioning software from other OSs   
    (e.g., DOS FDISK, OS/2 FDISK)  

Command (m for help):     <==等待你的输入! 

After you enter m, you'll see the following commands

Command (m for help): m   <== 输入 m 后,就会看到底下这些命令介绍 
Command action    
   a   toggle a bootable flag   
   b   edit bsd disklabel    
   c   toggle the dos compatibility flag    
   d   delete a partition            <==删除一个partition    
   l   list known partition types    
   m   print this menu    
   n   add a new partition           <==新增一个partition    
   o   create a new empty DOS partition table    
   p   print the partition table     <==在屏幕上显示分割表    
   q   quit without saving changes   <==不储存离开fdisk程序    
   s   create a new empty Sun disklabel    
   t   change a partition's system id    
   u   change display/entry units    
   v   verify the partition table    
   w   write table to disk and exit  <==将刚刚的动作写入分割表    
   x   extra functionality (experts only) 

Press q when you leave q all actions will not take effect! Conversely, w means that the action takes effect.

Command (m for help): p  <== 这里可以输出目前磁盘的状态  

Disk /dev/hdc: 41.1 GB, 41174138880 bytes        <==这个磁盘的文件名与容量 
255 heads, 63 sectors/track, 5005 cylinders      <==磁头、扇区与磁柱大小 
Units = cylinders of 16065 * 512 = 8225280 bytes <==每个磁柱的大小    
   Device Boot      Start         End      Blocks   Id  System 
/dev/hdc1   *           1          13      104391   83  Linux 
/dev/hdc2              14        1288    10241437+  83  Linux 
/dev/hdc3            1289        1925     5116702+  83  Linux 
/dev/hdc4            1926        5005    24740100    5  Extended 
/dev/hdc5            1926        2052     1020096   82  Linux swap / Solaris 
# 装置文件名 启动区否 开始磁柱    结束磁柱  1K大小容量 磁盘分区槽内的系统  

Command (m for help): q 

Want to leave without storage? P ress q and you're right! Don't just press w ah!

Use p list the split table information for the current disk, the upper half of which shows the state of the overall disk.


The disk is formatted

When the disk is split, it's natural to format the file system, which is very simple mkfs (make filesystem) command.

Grammar:

mkfs [-t 文件系统格式] 装置文件名

Options and parameters:

  • -t: File system formats such as ext3, ext2, vfat, etc. can be connected (system support will only take effect)

Instance 1

View the file formats supported by mkfs

[root@www ~]# mkfs[tab][tab]
mkfs         mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.msdos   mkfs.vfat

Press down on the two tabs and you'll see that the mkfs-supported file format is shown above.

Instance 2

Format partition /dev/hdc6 (you can specify your own partition) as ext3 file system:

[root@www ~]# mkfs -t ext3 /dev/hdc6
mke2fs 1.39 (29-May-2006)
Filesystem label=                <==这里指的是分割槽的名称(label) 
OS type: Linux Block size=4096 (log=2)          <==block 的大小配置为 4K  
Fragment size=4096 (log=2) 
251392 inodes, 502023 blocks     <==由此配置决定的inode/block数量 
25101 blocks (5.00%) reserved for the super user 
First data block=0 
Maximum filesystem blocks=515899392 
16 block groups 32768 blocks per group, 
32768 fragments per group 
15712 inodes per group 
Superblock backups stored on blocks:         
        32768, 98304, 163840, 229376, 294912  

Writing inode tables: done 
Creating journal (8192 blocks): done <==有日志记录 
Writing superblocks and filesystem accounting information: done  

This filesystem will be automatically checked every 34 mounts or 
180 days, whichever comes first.  Use tune2fs -c or -i to override. 
# 这样就创建起来我们所需要的 Ext3 文件系统了!简单明了! 

Disk inspection

fsck (file system check) is used to check and maintain inconsistent file systems.

If the system loses power or there is a problem with the disk, the file system can be checked using the fsck command.

Grammar:

fsck [-t 文件系统] [-ACay] 装置名称

Options and parameters:

  • -t : Given the pattern of the archive system, this parameter is not required if it is already defined in /etc/fstab or if kernel itself is supported
  • -s : Check by executing fsck instructions one by one in sequence
  • -A : Check all the partitions listed in /etc/fstab
  • -C: Shows the complete progress of the inspection
  • -d: Print out the debug results for e2fsck
  • -p : When there is a -A condition at the same time, multiple fsck checks are performed together
  • -R : Omitted / not checked when there are also -A conditions
  • -V: Detailed display mode
  • -a : If there is an error in the check, it is fixed automatically
  • -r : If an error is checked, the user answers whether to fix it
  • -y : The option specifies that detecting each file is automatically entered yes, and when you are not sure which ones are abnormal, you can perform a full check fix of the sfsck -y.

Instance 1

See how many file systems support the fsck command:

[root@www ~]# fsck[tab][tab]
fsck         fsck.cramfs  fsck.ext2    fsck.ext3    fsck.msdos   fsck.vfat

Instance 2

Force detection /dev/hdc6 partition:

[root@www ~]# fsck -C -f -t ext3 /dev/hdc6 
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
vbird_logical: 11/251968 files (9.1% non-contiguous), 36926/1004046 blocks

If you don't have the option of adding -f, the check doesn't go very quickly because there's no problem with this file system! I f you add -f to force the check, it will be a display process one by one.


The disk is mounted and removed

Linux's disk mount uses the mount the use of the umount command.

Disk Mount syntax:

mount [-t 文件系统] [-L Label名] [-o 额外选项] [-n]  装置文件名  挂载点

Instance 1

By default, mount the /dev/hdc6 you just created to /mnt/hdc6!

[root@www ~]# mkdir /mnt/hdc6
[root@www ~]# mount /dev/hdc6 /mnt/hdc6
[root@www ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
.....中间省略.....
/dev/hdc6              1976312     42072   1833836   3% /mnt/hdc6

Disk unloading command umount

umount [-fn] 装置文件名或挂载点

Options and parameters:

  • -f: Force removal! Can be used in situations like network file systems (NFS) that cannot be read;
  • -n: Remove without upgrading /etc/mtab.

Uninstall/dev/hdc6

[root@www ~]# umount /dev/hdc6