Linux Disk Management
Linux Disk Management is directly related to the performance of the whole system.
Linux Disk Management commonly uses three commands for DF, Du, and Fdisk.
- DF: List 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 consumption of the file system. You can use this command to get information about how much space the hard disk is taking up, and how much space is left.
Grammar:
df [-ahikHTm] [目录或文件名]
Options :
Options |
Description |
-A or--all |
Contains all the file systems; |
--block-size= <区块大小> |
Displays the number of chunks in the specified chunk size; |
-H or--human-readable |
Display information in a more readable manner; |
-H or--si |
Same as-h parameter, but is calculated as the unit of Bytes instead of the Bytes; |
-I. or--inodes |
Displays information about the inode; |
-K or--kilobytes |
The specified chunk size is 1024 bytes; |
-L or--local |
Only the local file system is displayed; |
-M or--megabytes |
The specified chunk size is 1048576 bytes; |
--no-sync |
Do not perform the sync instruction until the disk usage information is obtained, this is a preset value; |
-P or--portability |
Use the POSIX output format; |
--sync |
Before obtaining the disk usage information, perform the sync instruction first; |
-T <文件系统类型> or <文件系统类型> --type= |
Displays only disk information for the specified file system type; |
-T or--print-type |
Displays the type of file system; |
-X <文件系统类型> or <文件系统类型> --exclude-type= |
Do not display disk information for the specified file system type; |
--help |
Display Help; |
--version |
Displays version information. |
Example 1
List all the file systems in the system!
[[email protected] ~]# df文件系统 1K-块 已用 可用 已用% 挂载点/dev/mapper/centos-root 17811456 1318704 16492752 8% /devtmpfs 488788 0 488788 0% /devtmpfs 499848 0 499848 0% /dev/shmtmpfs 499848 6808 493040 2% /runtmpfs 499848 0 499848 0% /sys/fs/cgroup/dev/sda1 1038336 127464 910872 13% /boottmpfs 99972 0 99972 0% /run/user/0
Under Linux If DF does not have any options, the default is to list all of the system's (non-special in-memory file systems and Swaps) in 1 Kbytes capacity!
Example 2
Display capacity results in an easy-to-read capacity format
[[email protected] ~]# df -h文件系统 容量 已用 可用 已用% 挂载点/dev/mapper/centos-root 17G 1.3G 16G 8% /devtmpfs 478M 0 478M 0% /devtmpfs 489M 0 489M 0% /dev/shmtmpfs 489M 6.7M 482M 2% /runtmpfs 489M 0 489M 0% /sys/fs/cgroup/dev/sda1 1014M 125M 890M 13% /boottmpfs 98M 0 98M 0% /run/user/0
Example 3
List all special file formats and names in the system
[[email protected] ~]# df -aTFilesystem Type 1K-blocks Used Available Use% Mounted on/dev/hdc2 ext3 9920624 3823112 5585444 41% /proc proc 0 0 0 - /procsysfs sysfs 0 0 0 - /sysdevpts devpts 0 0 0 - /dev/pts/dev/hdc3 ext3 4956316 141376 4559108 4% /home/dev/hdc1 ext3 101086 11126 84741 12% /boottmpfs tmpfs 371332 0 371332 0% /dev/shmnone binfmt_misc 0 0 0 - /proc/sys/fs/binfmt_miscsunrpc rpc_pipefs 0 0 0 - /var/lib/nfs/rpc_pipefs
Example 4
Displays the available disk capacity under/etc in an easy-to-read capacity format
[[email protected] ~]# df -h /etcFilesystem Size Used Avail Use% Mounted on/dev/hdc2 9.5G 3.7G 5.4G 41% /
Du
The Linux du command also looks at the use of space, but unlike the DF command, the Linux du command is a view of the space used by the file and directory disks, or some difference from the DF command, which describes the Linux du command.
Grammar:
du [-ahskm] 文件或目录名称
Options :
Options |
Description |
-A or-all |
Displays the size of individual files in the directory. |
-B or-bytes |
When the directory or file size is displayed, in bytes. |
-C or--total |
Displays the sum of all directories or files in addition to the size of individual directories or files. |
-K or--kilobytes |
Output in kilobytes (1024bytes). |
-M or--megabytes |
Output in megabytes. |
-S or--summarize |
Only totals are displayed, and only the last plus total value is listed. |
-H or--human-readable |
The readability of the information is improved by using K,M,G as the unit. |
-X or--one-file-xystem |
The file system at the beginning of processing, if encountered in other different file system directory is skipped. |
-L <符号链接> or <符号链接> --dereference |
Displays the source file size of the symbolic link specified in the options. |
-S or--separate-dirs |
When displaying the size of an individual directory, it does not contain the size of its subdirectories. |
-X <文件> or <文件> --exclude-from= |
In the <文件> specified directory or file. |
--exclude= <目录或文件> |
Skips over the specified directory or file. |
-D or--dereference-args |
Displays the source file size for the specified symbolic link. |
-H or--si |
The same as the-h parameter, but the k,m,g is converted to 1000 units. |
-L or--count-links |
Duplicate the hardware linked file. |
Example 1
List all file sizes in the current directory
[[email protected] ~]# du8 ./test4 <==每个目录都会列出来8 ./test2....中间省略....12 ./.gconfd <==包括隐藏文件的目录220 . <==这个目录(.)所占用的总量
When direct input du does not have any options, du parses the hard disk space occupied by the files and directories in the directory that is currently located.
Example 2
The capacity of the file is also listed
[[email protected] ~]# du -a12 ./install.log.syslog <==有文件的列表了8 ./.bash_logout8 ./test48 ./test2....中间省略....12 ./.gconfd220 .
Example 3
Check the capacity of each directory under the root directory
[[email protected] ~]# du -sm /*7 /bin6 /boot.....中间省略....0 /proc.....中间省略....1 /tmp3859 /usr <==系统初期最大就是他了啦!77 /var
Wildcard characters * to represent each directory.
Unlike DF, du is a command that actually searches the file system for all of the file data.
Fdisk
FDISK is a disk partition table manipulation tool for Linux.
Grammar:
fdisk [-l] 装置名称
Options:
Options |
Description |
-B <分区大小> |
Specify the size of each partition; |
-L |
Lists the partition table status for the specified peripheral device; |
-S <分区编号> |
Outputs the specified partition size to the standard output, in chunks; |
-U |
With the "-L" parameter list, the number of partitions is replaced by the number of columns to represent the starting address of each partition; |
-V |
Displays version information. |
Example 1
List all partition information
[[email protected] ~]# fdisk -lDisk /dev/xvda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000 Device Boot Start End Blocks Id System/dev/xvda1 * 1 2550 20480000 83 Linux/dev/xvda2 2550 2611 490496 82 Linux swap / SolarisDisk /dev/xvdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x56f40944 Device Boot Start End Blocks Id System/dev/xvdb2 1 2610 20964793+ 83 Linux
Example 2
Locate the disk in the root directory on your system and check the relevant information on that drive.
[[email protected] ~]# df / <==注意:重点在找出磁盘文件名而已Filesystem 1K-blocks Used Available Use% Mounted on/dev/hdc2 9920624 3823168 5585388 41% /[[email protected] ~]# 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 entering m, you will see these commands below
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 when you leave Fdisk q
, and all actions will not take effect! On the contrary, it 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
Do you want to leave without saving? Press Q on the right! Don't just press W!
Using the p
split table information that can list the current disk, the top half of this information displays the status of the overall disk.
Disk Formatting (MKFS)
After the disk is split, it is natural to format the file system, the format of the command is very simple, using the mkfs
(make filesystem) command.
Syntax :
mkfs [-t 文件系统格式] 装置文件名
Options :
Options |
Description |
Fs |
Specifies the parameters when the file system is established; |
-T <文件系统类型> |
Specify what file system to build; |
-V |
Display version information and detailed use method; |
-V |
Show a brief use method; |
-C |
Before making the file system, check whether the partition has a bad track. |
Example 1
View supported file formats for MKFS
[[email protected] ~]# mkfs[tab][tab]mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.msdos mkfs.vfat
Pressing two [tab] will find the file format supported by MKFS as shown above.
Example 2
Format the partition/dev/hdc6 (you can specify your own partition) as the Ext3 file system:
[[email protected] ~]# mkfs -t ext3 /dev/hdc6mke2fs 1.39 (29-May-2006)Filesystem label= <==这里指的是分割槽的名称(label)OS type: LinuxBlock 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 userFirst data block=0Maximum filesystem blocks=51589939216 block groups32768 blocks per group, 32768 fragments per group15712 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912Writing inode tables: doneCreating journal (8192 blocks): done <==有日志记录Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 34 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.# 这样就创建起来我们所需要的 Ext3 文件系统了!简单明了!
Disk inspection
The fsck (file system check) is used to check and maintain inconsistent file systems.
If the system is out of power or a disk problem occurs, the fsck command can be used to check the file system.
Grammar:
fsck [-t 文件系统] [-ACay] 装置名称
Options and Parameters:
Options |
Description |
-A |
Automatically repair the file system without asking any questions; |
-A |
According to the contents of the/etc/fstab configuration file, check all the file system listed in the file; |
-N |
Does not execute the instruction, only lists the action which the actual execution will carry on; |
-P |
When used with the "-A" parameter, all file systems are checked at the same time; |
-R |
Use interactive mode to ask questions when performing a repair, allowing the user to confirm and determine the processing mode; |
-R |
When used with the "-A" parameter, the file system will skip/directory is not checked; |
-S |
Perform inspection operations sequentially, rather than concurrently; |
-T <文件系统类型> |
Specifies the type of file system to check; |
-T |
When the fsck command is executed, the header information is not displayed; |
-V |
Displays the instruction execution process. |
-Y |
The option specifies to detect that each file is automatically entered Yes and can perform a # fsck-y full check fix when unsure of those that are not normal. |
Example 1
See how many file system support FSCK commands the system has:
[[email protected] ~]# fsck[tab][tab]fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.msdos fsck.vfat
Example 2
To force detection of/DEV/HDC6 partitions:
[[email protected] ~]# 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 sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary informationvbird_logical: 11/251968 files (9.1% non-contiguous), 36926/1004046 blocks
If you do not add-f option, because this file system has not been the problem, the check is very fast! If you add the-f mandatory Check, an item is displayed.
Disk mount and dismount
Linux Disk mounts use mount
commands to uninstall the use umount
command.
The Mount command is used to load the file system to the specified mount point. The most common use of this command is to mount the CDROM so that we can access the data in the CDROM because you insert the disc into the CDROM, Linux does not mount automatically, and you must manually complete the mount using the Linux mount command.
Disk mount Syntax:
mount [-t vfstype] [-o options] device dirmount [-t 文件系统] [-L Label名] [-o 额外选项] [-n] 装置文件名 挂载点
- t verbose options:
? Disc or disc Image: iso9660
? DOS FAT16 file system: MSDOS
? Windows 9x FAT32 file system: VFAT
? Windows NT NTFS file system: NTFS
? Mount Windows file network share: SMBFS (requires kernel support) Recommended CIFS
? UNIX (LINUX) file network share: NFS
- o verbose options:
? Loop: Used to attach a file as a hard disk partition on the system
? RO: Hook device with read-only method
? RW: Mount device with read-write mode
? Iocharset: Specifies the character set used to access the file system, such as Iocharset=utf8
? Remount: Re-mount
Example 1
In the default way, mount the/dev/hdc6 you just created onto/MNT/HDC6!
[[email protected] ~]# mkdir /dev/hdc6[[email protected] ~]# mount /dev/hdc6 /mnt/hdc6[[email protected] ~]# dfFilesystem 1K-blocks Used Available Use% Mounted on.....中间省略...../dev/hdc6 1976312 42072 1833836 3% /mnt/hdc6
Disk Offload Command Umount
Grammar:
umount [-fn] 装置文件名或挂载点
Options and Parameters:
- -F: Forced removal! can be used in situations such as a network file system (NFS) that cannot be read;
- -N: Dismount without upgrading/etc/mtab.
Uninstalling/DEV/HDC6
[[email protected] ~]# umount /dev/hdc6
Linux Disk Management