df、du 命令和磁盘分区

df 命令

用于显示磁盘分区上的可使用的磁盘空间。默认显示单位为KB。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息。

常用选项:

-m 以 MB为单位来显示信息;

-h 以可读性较高的方式来显示信息;

-i 显示 inode 的信息。

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   28G  1.1G   27G   4% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    912M     0  912M   0% /dev/shm
tmpfs                    912M  8.7M  904M   1% /run
tmpfs                    912M     0  912M   0% /sys/fs/cgroup
/dev/sda1                197M  121M   77M  62% /boot
tmpfs                    183M     0  183M   0% /run/user/0
[root@localhost ~]# df -i
Filesystem                Inodes IUsed    IFree IUse% Mounted on
/dev/mapper/centos-root 14575616 30917 14544699    1% /
devtmpfs                  230617   400   230217    1% /dev
tmpfs                     233381     1   233380    1% /dev/shm
tmpfs                     233381   498   232883    1% /run
tmpfs                     233381    16   233365    1% /sys/fs/cgroup
/dev/sda1                 102400   328   102072    1% /boot
tmpfs                     233381     1   233380    1% /run/user/0

用 df -h 查看还有空间,但是文件写不进去,可能是 inode 用满了。
tmpfs 是临时文件系统,重启后里面的内容会消失
/dev/shm 是内存,大小是内存的一半。


du 命令

也是查看使用空间的,但是与df命令不同的是 du 命令是对文件和目录磁盘使用的空间的查看

常用选项:
-s 仅显示总计,只列出最后加总的值;
-m 以 MB为单位来显示信息;

-h 以可读性较高的方式来显示信息;

[root@localhost log]# du  --列出所有目录
348     ./audit
8       ./tuned
2152    ./anaconda
0       ./rhsm
3480    .
[root@localhost log]# du -sh	--只列出当前目录的总和
3.4M    .
[root@localhost log]# du -sh *	--列出当前目录下的所有文件
2.2M    anaconda
348K    audit
36K     boot.log
0       btmp
8.0K    cron
124K    dmesg
124K    dmesg.old
4.0K    firewalld
4.0K    grubby_prune_debug
16K     lastlog
4.0K    maillog
596K    messages
0       rhsm
8.0K    secure
0       spooler
0       tallylog
8.0K    tuned
8.0K    vmware-vgauthsvc.log.0
16K     vmware-vmsvc.log
16K     wtmp
4.0K    yum.log


用 ls-lh 命令和 du -h 命令查看文件大小时,发现两个显示的结果不一致:

[root@localhost log]# ls -lh yum.log 
-rw-------. 1 root root 2.0K Oct 31 22:03 yum.log
[root@localhost log]# du -h yum.log  
4.0K    yum.log
原因是  du 是基于块的大小,ls是文件的本身大小。一个块最小是 4K ,一个文件至少占用一个块,当文件大小不到 4K 时,也只能一个文件占用一个块。


fdisk命令

用于观察硬盘实体使用情况,也可对硬盘分区。它采用传统的问答式界面。
常用选项:
-l 列出磁盘的具体信息。

[root@localhost log]# fdisk /dev/sdb	--对磁盘 sdb 进行分区
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xbb48a5a8.

Command (m for help): n					--新建一个分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p					--新建一个主分区
Partition number (1-4, default 1): 1	--指定分区号
First sector (2048-4194303, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): +500M	--指定大小为 500M
Partition 1 of type Linux and of size 500 MiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e					--新建一个扩展分区
Partition number (2-4, default 2): 2
First sector (1026048-4194303, default 1026048): 
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-4194303, default 4194303): +300M
Partition 2 of type Extended and of size 300 MiB is set


Command (m for help): d					--删除分区
Partition number (1,2, default 2): 2	--删除分区号为 2 的分区
Partition 2 is deleted

Command (m for help): p					--打印分区状态信息

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbb48a5a8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux

Command (m for help): w					--保存分区操作
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost log]# fdisk -l			--查看磁盘信息

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a8c5b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    62914559    31251456   8e  Linux LVM

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbb48a5a8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux

Disk /dev/mapper/centos-root: 29.9 GB, 29850861568 bytes, 58302464 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

总结:
在创建磁盘分区的时候,有三种分区可选,扩展分区(e),主分区(p),逻辑分区(l)。
最多只能创建4个分区(4个中不包括逻辑分区),如果想创建多个分区,可以是3个主分区和1个扩展分区组合。然后在扩展分区中,创建逻辑分区(可以创建无限个逻辑分区)。 
需要注意的是,扩展分区相当于一个空壳子,是不可以格式化的(不可以使用),只有它的子分区(逻辑分区)才可以格式化。
在fdisk中划分分区的时候,使用d选项可以删除你想要删除的那个分区,需要注意的是,当扩展分区中有子分区(逻辑分区)的时候,删除扩展分区会连同子分区一起删除,所以在删除分区的时候,一定要谨慎。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值