The three commands commonly used in Linux disk management are df, du, and fdisk.

Linux disk management

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

The three commands commonly used in Linux disk management are df, du, and fdisk.

  • df (full English Name: disk full): lists the overall disk usage of the file system
  • du (full English Name: disk used): check the disk space usage
  • fdisk: used for disk partitioning

df

df command parameter function: check the disk space occupation of the file system. You can use this command to obtain information such as how much space the hard disk is occupied and how much space is left at present.

Syntax:

df [-ahikHTm] [Directory or file name]

Options and parameters:

  • -a: List all file systems, including system specific file systems such as / proc;
  • -k: Display each file system with the capacity of KBytes;
  • -m: Display each file system with MBytes capacity;
  • -h: Self display in GBytes, MBytes, KBytes and other formats that are easy for people to read;
  • -H: Replace the carry mode of M=1024K with M=1000K;
  • -T: Display the file system type, and list it together with the file system name of the partition (for example, ext3);
  • -i: Instead of hard disk capacity, it is displayed in the number of inode s

Example 1

List all 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 does not add any options, all files in the system (excluding file systems and swap in special memory) will be listed with a capacity of 1 Kbytes by default!

Example 2

Display the capacity results in a readable 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

Example 3

List all 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

Example 4

Display the available disk capacity under / etc in a readable 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 is also used to view the space used, but different from the df command, the Linux du command is used to view the space used by files and directory disks, or it is different from the df command. Here is the Linux du command.

Syntax:

du [-ahskm] File or directory name

Options and parameters:

  • -a: List the capacity of all files and directories, because only the amount of files under the directory is counted by default.
  • -h: Displayed in a more readable capacity format (G/M);
  • -s: List the total amount instead of the occupied capacity of each individual directory;
  • -s: It does not include the total under the subdirectory, which is a little different from - s.
  • -k: Display capacity in KBytes;
  • -m: List the capacity display in MBytes;

Example 1

Only list the capacity of all folders in the current directory (including hidden folders):

[root@www ~]# du
8       ./test4     <==Each directory will be listed
8       ./test2
....Middle ellipsis....
12      ./.gconfd   <==Directory containing hidden files
220     .           <==This directory(.)Total occupied

If you directly enter du without adding any options, du will analyze the hard disk space occupied by subdirectories in the current directory.

Example 2

The capacity of the file is also listed

[root@www ~]# du -a
12      ./install.log.syslog   <==There's a list of files
8       ./.bash_logout
8       ./test4
8       ./test2
....Middle ellipsis....
12      ./.gconfd
220     .

Example 3

Check the capacity of each directory under the root directory

[root@www ~]# du -sm /*
7       /bin
6       /boot
.....Middle ellipsis....
0       /proc
.....Middle ellipsis....
1       /tmp
3859    /usr     <==He was the biggest in the early stage of the system!
77      /var

Wildcard * to represent each directory.

Unlike df, the du command actually goes directly to the file system to search for all file data.

fdisk

fdisk is a Linux disk partition table operation tool.

Syntax:

fdisk [-l] Device name

Options and parameters:

  • -l: Output all partition contents of the device followed. If there is only fdisk -l, the system will list the partitions of devices that can be found in the whole system.

Example 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

Example 2

Find the disk where the root directory of your system is located and check the relevant information in the hard disk

[root@www ~]# DF / < = = note: the key point is to find out the disk file name
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hdc2              9920624   3823168   5585388  41% /

[root@www ~]# Fdisk / dev / HDC < = = look carefully and don't add numbers!
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):     <==Waiting for your input!

After entering m, you will see the introduction of these commands below

Command (m for help): m   <== input m After that, you will see the introduction of these commands below
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition            <==Delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition           <==Add a new one partition
   o   create a new empty DOS partition table
   p   print the partition table     <==Display split table on screen
   q   quit without saving changes   <==Leave without saving fdisk program
   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  <==Write the action just to the split table
   x   extra functionality (experts only)

Press q when leaving fdisk, then all actions will not take effect! On the contrary, pressing w means that the action takes effect.

Command (m for help): p  <== The status of the current disk can be output here

Disk /dev/hdc: 41.1 GB, 41174138880 bytes        <==File name and capacity of this disk
255 heads, 63 sectors/track, 5005 cylinders      <==Head, sector, and column sizes
Units = cylinders of 16065 * 512 = 8225280 bytes <==Size of each magnetic column

   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
# Device file name boot area no start column end column 1K capacity disk partition slot system

Command (m for help): q

Want to leave without saving? Just press q! Don't press w!

Use p to list the partition table information of the current disk. The upper half of this information displays the status of the whole disk.

Disk formatting

After the disk partition is completed, it is natural to format the file system. The format command is very simple. Use the mkfs (make file system) command.

Syntax:

mkfs [-t File system format] Device file name

Options and parameters:

  • -t: File system formats can be used, such as ext3, ext2, vfat, etc. (the system will take effect only if supported)

Example 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 two [tab s] and you will find that the file formats supported by mkfs are as shown above.

Example 2

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

[root@www ~]# mkfs -t ext3 /dev/hdc6
mke2fs 1.39 (29-May-2006)
Filesystem label=                <==This refers to the name of the dividing slot(label)
OS type: Linux
Block size=4096 (log=2)          <==block The size of is configured as 4 K 
Fragment size=4096 (log=2)
251392 inodes, 502023 blocks     <==Determined by this configuration inode/block quantity
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 <==Logged
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.
# This creates the Ext3 file system we need! Simple and clear!

Disk inspection

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

If the system loses power or the disk has problems, you can use the fsck command to check the file system.

Syntax:

fsck [-t file system] [-ACay] Device name

Options and parameters:

  • -t: given the type of file system, if it is defined in / etc/fstab or supported by the kernel itself, this parameter does not need to be added
  • -s: execute fsck instructions one by one to check
  • -A: check all the partition s listed in / etc/fstab
  • -C: display the complete inspection progress
  • -d: print e2fsck's debug results
  • -p: when - A condition exists at the same time, multiple fsck checks are performed at the same time
  • -R: omit / do not check when there is - A condition at the same time
  • -V: detailed display mode
  • -a: if there is an error, it will be repaired automatically
  • -r: if there is an error in the inspection, the user will answer whether to repair it
  • -Y: the option specifies that yes is automatically entered to detect each file. When you are not sure that those files are abnormal, you can perform # fsck -y all inspection and repair.

Example 1

To see how many file systems support fsck commands:

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

Example 2

Force detection of / 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 the - f option is not added, the check process is very fast because there has been no problem with the file system! If -f mandatory check is added, the process will be displayed one by one.

Disk mounting and dismounting

Linux disk mount uses the mount command, and unmount uses the umount command.

Disk mount syntax:

mount [-t file system] [-L Label name] [-o Additional options] [-n]  Device file name mount point

Example 1

Mount the newly created / dev/hdc6 to / mnt/hdc6 by default!

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

Disk unmount command umount syntax:

umount [-fn] Device file name or mount point

Options and parameters:

  • -f: Forced removal! It can be used when similar network file system (NFS) cannot read;
  • -n: Dismount without upgrading / etc/mtab.

Uninstall / dev/hdc6

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

Posted by Ryyo on Mon, 29 Nov 2021 12:04:30 -0800