English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux Disk Management

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

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

  • df: List the overall disk usage of the file system

  • du: Check disk space usage

  • fdisk: Used for disk partitioning

df

df command parameter function: Check the disk space usage of the file system. You can use this command to get how much disk space is occupied, how much space is left, and other information.

Syntax:

df [-ahikHTm] [Directory or filename]

Options and Parameters:

  • -a  :List all file systems, including those unique to the system /proc and other file systems

  • -k  :Display each file system in KBytes capacity

  • -m  :Display each file system in MBytes capacity

  • -h  :Display in formats that are easier for people to read, such as GBytes, MBytes, KBytes, etc.

  • -H  :Display as M=1000K instead of M=1024K's rounding method;

  • -T  :Display the file system type, including the filesystem name of the partition (e.g., ext3) are also listed;

  • -i  :Display the number of inodes instead of hard disk capacity

Example 1

List all file systems within 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 is not added with any options, it will default to listing all the Also include those in special memory file systems and swap 1 List the capacity in Kbytes!

Example 2

Display the capacity result in a readable capacity format

[root@www ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdc2             9files (5G  3files (7G  5files (4G  41% /
/dev/hdc3             4files (8G  139M  4files (4G   4% /home
/dev/hdc1              99M   11M   83M  12% /boot
tmpfs                 363M  363M /dev/shm

Example 3

List all special file formats and names within the system

[root@www ~]# df -aT
Filesystem 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

将 /etc 底下的可用的磁盘容量以易读的容量格式显示

[root@www ~]# df -h /etc
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdc2             9files (5G  3files (7G  5files (4G  41% /

du

Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的,这里介绍Linux du命令。

Syntax:

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

Options and Parameters:

  • -a  :列出所有的文件与目录容量,因为默认仅统计目录底下的文件量而已。

  • -h  :以人们较易读的容量格式 (G/M) 显示;

  • -s  :列出总量而已,而不列出每个各别的目录占用容量;

  • -S  :不包括子目录下的总计,与 -s 有点差别。

  • -k  :以 KBytes 列出容量显示;

  • -m  :以 MBytes 列出容量显示;

Example 1

只列出当前目录下的所有文件夹容量(包括隐藏文件夹):

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

When du is entered directly without any options, du will analyze the disk space occupied by the files and directories in the current directory.

Example 2

List the file size as well

[root@www ~]# du -a
12      files (/install.log.syslog  <==The list of files is now present
8       files (/.bash_logout
8       files (/test4
8       files (/test2
....Omitted....
12      files (/.gconfd
220  .

Example 3

Check the capacity occupied by each directory under the root directory

[root@www ~]# du -sm /*
7       /bin
6       /boot
.....Omitted in the middle.....
0       /proc
.....Omitted in the middle.....
1       /tmp
3859    /usr <== It was the largest in the initial system!
77      /var

Wildcard * to represent each directory.

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

fdisk

fdisk is a Linux disk partition table operation tool.

Syntax:

fdisk [-l] device name

Options and Parameters:

  • -l : Output the partition content of the device followed. If only fdisk -l when      Then the system will list all partitions of all devices that can be searched within the entire system.

Example 1

List all partition information

[root@AY120919111755c246621 tmp]# fdisk -l
Disk /dev/xvda: 21files (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: 21files (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 within the hard disk

[root@www ~]# df /            <== Note: The focus is on finding the disk filename only
Filesystem           1K-blocks Used Available Use% Mounted on
/dev/hdc2              9920624   3823168   5585388  41% /
[root@www ~]# fdisk /dev/hdc <== Look carefully, do not add the number!
The number of cylinders for this disk is set to 5005files (
There is nothing wrong with that, but this is larger than 1024,
and could cause problems with in certain setups:
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): <== Wait for your input!

After entering m, you will see these command introductions below

Command (m for help): m <== After entering m, you will see these command introductions 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 partition
   o <== Create a new empty DOS partition table
   p <== Print the partition table <== Display the partition table on the screen
   q <== Quit without saving changes <== Do not save and leave the 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 actions to the partition table
   x <== Extra functionality (for experts only)

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

Command (m for help): p <== Here you can output the current status of the disk
Disk /dev/hdc: 41files (1 GB, 41174138880 bytes <== Name and capacity of this disk
255 heads, 63 sectors/track, 5005 cylinders <== Size of heads, sectors, and cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes <== Size of each cylinder
   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 Yes Start cylinder End cylinder  1K size capacity system inside the disk partition slot
Command (m for help): q

Do you want to exit without saving? Press q to do so! Don't press w randomly!

Use p to list the partition table information of the current disk, the upper part of which shows the overall status of the disk.

Disk formatting

After the disk partitioning is completed, it is natural to carry out the formatting of the file system, and the command for formatting is very simple, using the mkfs (make filesystem) command.

Syntax:

mkfs [-t File system format] Device file name

Options and Parameters:

  • -t : Can accept file system format, such as ext3, ext2, vfat, etc. (will take effect if the system supports it)

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], and you will find the file formats supported by mkfs as shown above.

Example 2

Format the partition /dev/hdc6(Can specify your own partition) formatted as ext3 Filesystem:

[root@www ~]# mkfs -t ext3 /dev/hdc6
mke2fs 1files (39 (29-May-2006)
Filesystem label= == here refers to the name (label) of the partition slot
OS type: Linux
Block size=4096 (log=2) == block size configuration 4K 
Fragment size=4096 (log=2)
251392 inodes, 502023 blocks == inode determined by this configuration/block quantity
25101 blocks (5(0.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 == having log records
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 Ext we need3 File system! Simple and clear!

Disk inspection

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

If the system powers off or the disk has a problem, 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 in /etc/fstab is already defined or the kernel itself supports it, no need to add this parameter

  • -s : Execute fsck instructions sequentially to check one by one

  • -A : For/etc/Check all partitions listed in fstab

  • -C : Display the complete check progress

  • -d : Print out e2fsck debug results

  • -p : At the same time -A When the condition is met, multiple fsck checks are executed together

  • -R : At the same time -A When condition is met, omit / Do not check

  • -V : Detailed display mode

  • -a : If there are errors in the check, it is automatically repaired

  • -r : If there are errors in the check, the user decides whether to repair

  • -y : The option specifies that yes is automatically entered for each file checked, and can be executed # fsck when unsure which are abnormal. -y Check and repair all.

Example 1

Check the number of file systems supported by the fsck command:

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

Example 2

Forced check /dev/hdc6 Partition:

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

blocks -If the f option is not added, -f Force check, and the process will be displayed item by item.

Disk Mounting and Unmounting

Linux uses the mount command for disk mounting and the umount command for unmounting.

Disk mount syntax:

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

Example 1

Using the default method, mount the just created /dev/hdc6 Mount to /mnt/hdc6 Above!

[root@www ~]# mkdir /mnt/hdc6
[root@www ~]# mount /dev/hdc6 /mnt/hdc6
[root@www ~]# df
Filesystem           1K-blocks Used Available Use% Mounted on
.....Omitted in the middle.....
/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: Force unmount! Available in cases similar to network file system (NFS) being unreadable;

  • -n: Do not upgrade /etc/Unmount under mtab situation.

Unmount/dev/hdc6

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