Linux Filesystem
The quality of Linux disk management is directly related to the performance of the entire system.
The three commonly used commands for Linux disk management are `df`, `du`, and `fdisk`.
* **df** (abbreviation for disk free): Lists the overall disk usage of the file system.
* **du** (abbreviation for disk used): Checks disk space usage.
* **fdisk**: Used for disk partitioning.
* * *
## df
The `df` command's function is to check the disk space usage of the file system. You can use this command to find out how much space the hard disk has occupied and how much space is still available.
Syntax:
`df `
Options and parameters:
* `-h`: Displays the output in a human-readable format (e.g., using units like KB, MB, GB).
* `-T`: Displays the type of the file system.
* `-t `: Only displays file systems of the specified type.
* `-i`: Displays inode usage.
* `-H`: This parameter is a variant of `-h`, but uses 1000 bytes as the base unit instead of 1024 bytes. This means it will display disk usage in SI (International System of Units) units (e.g., MB, GB) rather than binary units (e.g., MiB, GiB).
* `-k`: This option displays disk space usage in KB units.
* `-a`: This parameter will display all file systems, including virtual file systems such as `proc`, `sysfs`, etc. If this option is not used, by default, the `df` command will not display virtual file systems.
### 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
In Linux, if `df` is used without any options, it will by default list all file systems in the system (excluding special memory file systems and swap) with a capacity of 1 Kbytes!
### Example 2
Display the capacity results in an easy-to-read 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 an easy-to-read 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 check space usage, but unlike the `df` command, the `du` command checks the disk space used by files and directories. There are some differences from the `df` command. Here, we introduce the Linux `du` command.
Syntax:
`du filename or directory name`
Options and parameters:
* -a: Lists the capacity of all files and directories, because by default it only counts the files under the directory.
* -h: Displays in a human-readable capacity format (G/M);
* -s: Only displays the total size of the specified directory or file, not the size of its subdirectories.
* -S: Includes the total under subdirectories, which is slightly different from -s.
* -k: Lists the capacity in KBytes;
* -m: Lists the capacity in MBytes;
### Example 1
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 ....Omitted in the middle....12 ./.gconfd <==Including directories with hidden files 220 . <==The total capacity occupied by this directory (.)
When you enter `du` directly without any options, `du` will analyze the hard disk space occupied by the subdirectories in the current directory.
### Example 2
List the capacity of files as well
[root@www ~]# du -a 12 ./install.log.syslog <==There is a list of files 8 ./.bash_logout 8 ./test4 8 ./test2 ....Omitted in the middle....12 ./.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 <==This is the largest at the beginning of the system! 77 /var
The wildcard `*` represents each directory.
Unlike `df`, the `du` command actually goes directly into the file system to search for all file data.
* * *
## fdisk
`fdisk` is a tool for operating the disk partition table in Linux.
Syntax:
`fdisk device name`
Options and parameters:
* -l: Outputs all partition information for the device that follows. If only `fdisk -l` is used, the system will list the partitions of all devices that can be found in the entire 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 / SolarisDisk /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 of that hard disk
[root@www ~]# df / <==Note: The key is to find the disk filename Filesystem 1K-blocks Used Available Use% Mounted on /dev/hdc2 9920624 3823168 5585388 41% /[root@www ~]# fdisk /dev/hdc <==Look carefully, 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 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 following command introduction
Command (m for help): m <== After entering m, you will see the following command introduction 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 <==Exit the fdisk program without saving 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 previous actions to the partition table x extra functionality (experts only)
When leaving `fdisk`, press `q`, and none of the actions will take effect! Conversely, pressing `w` means the actions will take effect.
Command (m for help): p <== Here you can output the current status of the disk Disk /dev/hdc: 41.1 GB, 41174138880 bytes <==The filename and capacity of this disk 255 heads, 63 sectors/track, 5005 cylinders <==The size of the heads, sectors, and cylinders Units = cylinders of 16065 * 512 = 8225280 bytes <==The 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 filename Bootable Start cylinder End cylinder 1K size capacity System in the disk partition slot Command (m for help): q
Want to leave without saving? Just press `q`! Don't press `w` carelessly!
Using `p` can list the partition table information of this disk. The upper part of this information displays the overall status of the disk.
* * *
### Disk Formatting
After the disk partitioning is completed, the next step is to format the file system. The formatting command is very simple, using the `mkfs` (make filesystem) command.
Syntax:
`mkfs device filename`
Options and parameters:
* -t: Can be followed by the filesystem format, such as ext3, ext2, vfat, etc. (It will only take effect if the system supports it).
### Example 1
Check the file formats supported by mkfs
[root@www ~]# mkfs mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.msdos mkfs.vfat
Pressing twice will show the file formats supported by mkfs as listed 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 (label) of the partition OS type: LinuxBlock size=4096 (log=2) <==The block size is configured as 4K Fragment size=4096 (log=2)251392 inodes, 502023 blocks <==The number of inodes/blocks determined by this configuration 25101 blocks (5.00%) reserved for the super user First data block=0Maximum filesystem blocks=51589939216 block groups 32768 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 <==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.# This creates the Ext3 file system we need! Simple and clear!
* * *
## Disk Checking
`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 device name`
Options and parameters:
* -t: Specifies the type of the file system. If it is already defined in /etc/fstab or supported by the kernel itself, this parameter is not required.
* -s: Executes the fsck commands sequentially one by one for checking.
* -A: Checks all partitions listed in /etc/fstab.
* -C: Displays the complete check progress.
* -d: Prints the debug results of e2fsck.
* -p: When used with the -A condition, multiple fsck checks are executed together.
* -R: When used with the -A condition, omits / and does not check it.
* -V: Verbose display mode.
* -a: Automatically repairs if errors are found during the check.
* -r: If errors are found during the check, the user is prompted whether to repair.
* -y: This option specifies to automatically input yes for each file check. When you are unsure which ones are abnormal, you can execute `# fsck -y` to check and repair everything.
### Example 1
Check how many file systems supported by the system have the fsck command:
[root@www ~]# fsck fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.msdos fsck.vfat
### Example 2
Force check the /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, since this file system has never had problems, the check process is very fast! If `-f` is added to force the check, the process will be displayed item by item.
* * *
## Disk Mounting and Unmounting
Linux disk mounting uses the `mount` command, and unmounting uses the `umount` command.
Disk mounting syntax:
`mount device filename mount point`
### Example 1
Use the default method to mount the newly created /dev/hdc6 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 .....Omitted in the middle...../dev/hdc6 1976312 42072 1833836 3% /mnt/hdc6
Disk unmounting command `umount` syntax:
`umount device filename or mount point`
Options and parameters:
* -f: Force unmount! Can be used in situations like network file systems (NFS) that cannot be read;
* -n: Unmounts without upgrading /etc/mtab.
Unmount /dev/hdc6
[root@www ~]# umount /dev/hdc6
YouTip