Linux disk management commands and usage examples, linux disk command examples
Linux disk management
Linux disk management is directly related to the performance of the entire system.
Linux disk management commonly uses three commands: df, du, and fdisk.
Df: list the total disk usage of the file system du: Check disk space usage fdisk: used for disk partitioning
Df
Df Command Parameter Function: Checks disk space usage of the file system. You can use this command to obtain the space occupied by the hard disk and the remaining space.
Syntax:
Df [-ahikHTm] [directory or file name]
Option:
Option |
Description |
-A or-all |
Including all file systems; |
-Block-size = <block size> |
Displays the number of Blocks Based on the specified block size; |
-H or-human-readable |
Display information in a readable manner; |
-H or-si |
The parameter is the same as the-h parameter, but is calculated in 1000 Bytes rather than 1024 Bytes; |
-I or-inodes |
Displays inode information; |
-K or-kilobytes |
The size of the specified block is 1024 bytes; |
-L or-local |
Only the local file system is displayed; |
-M or-megabytes |
The size of the specified block is 1048576 bytes; |
-No-sync |
Do not execute the sync command before obtaining disk usage information. This is a preset value; |
-P or-portability |
Use the POSIX output format; |
-Sync |
Run the sync command before obtaining disk usage information; |
-T <file system type> or-type = <file system type> |
Displays only the disk information of the specified file system type; |
-T or-print-type |
Displays the type of the file system; |
-X <file system type> or-exclude-type = <file system type> |
Do Not Display disk information of the specified file system type; |
-Help |
Show help; |
-Version |
Displays version information. |
Instance 1
List all file systems in the system!
[Root @ ylg ~] # Df file system 1 K-block used available % mount point/dev/mapper/centos-root 17811456 1318704 16492752 8%/devtmpfs 488788 0 488788 0%/devtmpfs 499848 0 499848/dev /shmtmpfs 499848 6808 493040 2%/runtmpfs 499848 0 499848 0%/sys/fs/cgroup/dev/sda1 1038336 127464 910872 13%/boottmpfs 99972 0 99972/run/user/0
In Linux, if df does not have any options, all (excluding the file system and swap in special memory) in the system will be listed as 1 Kbytes by default!
Instance 2
Display the capacity results in a readable capacity format
[Root @ ylg ~] # Df-H file system capacity in use % available mount point/dev/mapper/centos-root 17G 1.3G 16G 8%/devtmpfs 478 M 0 478 M 0%/devtmpfs 489 M 0 489 M 0%/dev/shmtmpfs 489 M 6.7 M 482 M 2%/runtmpfs 489 M 0 489 M 0%/sys/fs/cgroup/dev/sda1 1014 M 125 M 890 M 13% M /boottmpfs 98 M 0 98 M 0%/run/user/0
Instance 3
List all the special file formats and names in the system.
[root@ylg ~]# 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
Instance 4
Display the available disk capacity under/etc in readable capacity format
[root@www ~]# df -h /etcFilesystem Size Used Avail Use% Mounted on/dev/hdc2 9.5G 3.7G 5.4G 41% /
Du
The Linux du command is used to view the space used. However, unlike the df command, the Linux du command is used to view the space used by files and directory disks. It is also different from the df command, here we will introduce the Linux du command.
Syntax:
Du [-ahskm] file or directory name
Option:
Option |
Description |
-A or-all |
Displays the size of individual files in the directory. |
-B or-bytes |
Displays directories or files in bytes. |
-C or-total |
In addition to displaying the size of individual directories or files, it also displays the total number of all directories or files. |
-K or-kilobytes |
Output in KB (1024 bytes. |
-M or-megabytes |
Output in MB. |
-S or-summarize |
Only show total, only list the last added total value. |
-H or-human-readable |
The Unit is K, M, and G to improve information readability. |
-X or-one-file-xystem |
Take the file system at the beginning of processing as the standard. If it encounters another different file system directory, it will be skipped. |
-L <symbolic link> or-dereference <symbolic link> |
Displays the source file size of the symbolic link specified in the option. |
-S or-separate-dirs |
Displays the size of individual directories in hours without the size of their subdirectories. |
-X <File> or-exclude-from = <File> |
Specify a directory or file in <File>. |
-Exclude = <directory or File> |
Skips the specified directory or file. |
-D or-dereference-args |
Displays the source file size of the specified symbolic link. |
-H or-si |
The parameter is the same as the-h parameter, but K, M, and G are in the conversion unit of 1000. |
-L or-count-links |
Repeat the computing hardware link file. |
Instance 1
List all file capacities in the current directory
[Root @ ylg ~] # Du8. /test4 <= each directory is listed 8. /test2 .... omitted in the middle .... 12. /. gconfd <= contains the directory of the hidden file 220. <= This Directory (.) total amount occupied
If you directly enter du without any options, du will analyze the hard disk space occupied by the files and directories in the current directory.
Instance2
List the file capacity
[Root @ ylg ~] # Du-a12. /install. log. syslog <= a list of objects. 8. /. bash_logout8. /test48. /test2 .... omitted in the middle .... 12. /. gconfd220.
Instance3
Check the capacity occupied by each directory under the root directory
[Root @ ylg ~] # Du-sm/* 7/bin6/boot ..... omitted in the middle .... 0/proc ..... omitted in the middle .... 1/tmp3859/usr <= He is the biggest one in the early stage of the system! 77/var
Wildcard * represents each directory.
Unlike df, The du command searches all file data directly in the file system.
Fdisk
Fdisk is a Linux Disk Partition Table operation tool.
Syntax:
Fdisk [-l] device name
Option:
Option |
Description |
-B <partition size> |
Specify the size of each partition; |
-L |
Lists the Partition Table Status of the specified peripheral device; |
-S <Partition Number> |
Outputs the specified partition size to the standard output, in blocks; |
-U |
With the "-l" parameter list, the number of partitions replaces the number of cylinders to indicate the start address of each partition; |
-V |
Displays version information. |
Instance1
List all partition information
[root@ylg ~]# 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
Instance2
Find the disk where your system's root directory is located, and check the relevant information in the disk.
[Root @ ylg ~] # Df/<= Note: focus on finding the disk file name. Filesystem 1K-blocks Used Available Use % Mounted on/dev/hdc2 9920624 3823168 5585388/[root @ ylg ~] # Fdisk/dev/hdc <= do not add a number! The number of cylinders for this disk is set to 5005. there is nothing wrong with that, but this is larger than 1024, and cocould 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): <== wait for your input!
After entering m, you will see the following commands
Command (m for help): m <= after input m, the following commands introduce 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 partition o create a new empty DOS partition table p print the partition table <= display the split table q quit without saving on the screen changes <= do not store 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 preceding action to the split Table x extra functionality (experts only)
If you press q when you leave fdisk, all actions will not take effect! On the contrary, pressing w means that the action takes effect.
Command (m for help): p <= The current Disk status can be output here: Disk/dev/hdc: 41.1 GB, 41174138880 bytes <= File Name and capacity of the Disk: 255 heads, 63 sectors/track, 5005 cylinders <= head, sector, and magnetic column Size 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 start zone no start magnetic Column End 1 K size capacity system Command in disk partition slot (m for help): q
Do you want to leave without storage? Press q! Don't press w!
You can use p to list the partition table information of the current disk. The upper half of this information shows the overall disk status.
Disk formatting (mkfs)
After the disk is split, you need to format the file system. The format command is very simple. Use the mkfs (make filesystem) command.
Syntax:
Mkfs [-t File System Format] Device File Name
Option:
Option |
Description |
Fs |
Specifies the parameters used to create a file system; |
-T <file system type> |
Specifies the file system to be created; |
-V |
Display version information and detailed usage; |
-V |
Shows a brief usage; |
-C |
Before making an archive system, check whether the partition has a bad track. |
Instance1
View the file formats supported by mkfs
[root@ylg ~]# mkfs[tab][tab]mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.msdos mkfs.vfat
Press two [tabs] and you will find that the file formats supported by mkfs are shown above.
Instance2
Format/dev/hdc6 (you can specify your own partition) as the ext3 File System:
[Root @ ylg ~] # Mkfs-t ext3/dev/hdc6mke2fs 1.39 (29-May-2006) Filesystem label = <= The Name Of The split slot (label) OS type: linuxBlock size = 4096 (log = 2) <= the size of the block is 4 K Fragment size = 4096 (log = 2) 251392 inodes, 502023 blocks <= the number of inode/blocks determined by this configuration is 25101 blocks (5.00%) reserved for the super userFirst data block = 0 Maximum filesystem blocks = 51589939216 block groups32768 blocks per group, 32768 fragments per group15712 inodes Per groupSuperblock backups stored on blocks: 32768,983 04, 163840,229 376, 294912 Writing inode tables: doneCreating journal (8192 blocks): done <= There is a log record 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. # create the Ext3 file system we need! Simple and clear!
Disk Inspection
The fsck (file system check) is used to check and maintain inconsistent file systems.
If the system power is down or the disk is faulty, use the fsck command to check the file system.
Syntax:
Fsck [-t file system] [-ACay] device name
Options and parameters:
Option |
Description |
- |
Automatically fixes the file system and does not ask any questions; |
- |
Check all file systems listed in the file according to the/etc/fstab configuration file; |
-N |
If no command is executed, only the actions that will be performed are listed; |
-P |
When used with the "-A" parameter, all file systems are checked simultaneously; |
-R |
The interactive mode is used to ask questions during fixing, so that users can confirm and determine the handling method; |
-R |
When used with the "-A" parameter, the file system skipping the/directory will not be checked; |
-S |
Check jobs in sequence rather than at the same time; |
-T <file system type> |
Specifies the file system type to be checked; |
-T |
The title information is not displayed when the fsck command is executed; |
-V |
Displays the instruction execution process. |
-Y |
The option specifies that yes is automatically entered for each file to be detected. If you are not sure about which files are abnormal, run # fsck-y to check and fix all files. |
Instance1
View the number of supported fsck commands in the system:
[root@ylg ~]# fsck[tab][tab]fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.msdos fsck.vfat
Instance2
Force check/dev/hdc6 partition:
[root@ylg ~]# 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 the-f option is not added, the check is very fast because the file system has never encountered any problems! If the-f force check is added, a display process is performed.
Disk mounting and dismounting
For Linux disk mounting, run the mount command and the umount command.
Mount commandIt is used to load the file system to the specified loading point. This command is most commonly used to mount cdrom so that we can access the data in cdrom. Because you insert the CD into cdrom, Linux does not automatically mount it, you must use the Linux mount command to manually mount the disk.
Disk Mount Syntax:
Mount [-t vfstype] [-o options] device dirmount [-t file system] [-L Label name] [-o additional options] [-n] device file name mount point
-TDetailed options:
CD or CD image: iso9660
DOS fat16 File System: msdos
Windows 9x fat32 File System: vfat
Windows NT ntfs file system: ntfs
Mount Windows File network sharing: Recommended cifs for smbfs (Kernel support required)
Network Sharing for UNIX (LINUX) files: nfs
-ODetailed options:
Loop: used to connect a file to the system as a hard disk partition.
Ro: mounting a device in read-only mode
Rw: mounting a device in read/write mode
Iocharset: Specifies the character set used to access the file system, for example, iocharset = utf8
Remount: remount
Instance 1
Mount the created/dev/hdc6 to/mnt/hdc6 by default!
[Root @ ylg ~] # Mkdir/dev/hdc6 [root @ ylg ~] # Mount/dev/hdc6/mnt/hdc6 [root @ ylg ~] # DfFilesystem 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 disallow! It can be detached without upgrading/etc/mtab.
Uninstall/dev/hdc6
[root@ylg ~]# umount /dev/hdc6