2017-11-2 Linux磁盘管理

本文介绍Linux环境下磁盘管理和分区的基本命令,包括查看磁盘使用情况(df)、查看目录占用空间(du)及磁盘分区(fdisk)的方法。适用于初学者快速上手。

查看磁盘或者目录的容量

命令 : df

“df” 查看已挂载磁盘的总容量、使用容量、剩余容量等,可以不加任何参数,默认是按k为单位显示的。

[root@node79 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       50101352 1457188  48644164   3% /
devtmpfs          490164       0    490164   0% /dev
tmpfs             499968       0    499968   0% /dev/shm
tmpfs             499968    6820    493148   2% /run
tmpfs             499968       0    499968   0% /sys/fs/cgroup
/dev/sda1         201380  111396     89984  56% /boot
tmpfs              99996       0     99996   0% /run/user/0

常用选项 -h,比较直观的显示容量

[root@node79 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        48G  1.4G   47G   3% /
devtmpfs        479M     0  479M   0% /dev
tmpfs           489M     0  489M   0% /dev/shm
tmpfs           489M  6.7M  482M   2% /run
tmpfs           489M     0  489M   0% /sys/fs/cgroup
/dev/sda1       197M  109M   88M  56% /boot
tmpfs            98M     0   98M   0% /run/user/0
常用选项 -i,查看inode使用情况

[root@node79 ~]# df -i
Filesystem       Inodes IUsed    IFree IUse% Mounted on
/dev/sda3      25062912 54374 25008538    1% /
devtmpfs         122541   372   122169    1% /dev
tmpfs            124992     1   124991    1% /dev/shm
tmpfs            124992   449   124543    1% /run
tmpfs            124992    16   124976    1% /sys/fs/cgroup
/dev/sda1        102400   330   102070    1% /boot
tmpfs            124992     1   124991    1% /run/user/0

命令 : du

“du” 用来查看某个目录或文件所占空间大小.

语法 : du [-abckmsh] [文件或者目录名] 常用的参数有:

“-a” 全部文件与目录大小都列出来。如果不加任何选项和参数只列出目录(包含子目录)大小。

[root@node79 ~]# du -h /root
8.0K	/root/.ssh
0	/root/test
0	/root/234
48K	/root
[root@node79 ~]# du -sh /root
48K	/root

[root@node79 ~]# du -sh /etc/passwd
4.0K	/etc/passwd
[root@node79 ~]# ls -lh /etc/passwd
-rw-r--r--. 1 root root 1.1K Nov  2 10:31 /etc/passwd

磁盘的分区 fdisk

命令 : fdisk

fdisk 是Linux下硬盘的分区工具,是一个非常实用的命令,但是fdisk只能划分小于2T的分区。

语法 : fdisk [-l ] [设备名称] 选项只有一个。

“-l” 后边不跟设备名会直接列出系统中所有的磁盘设备以及分区表,加上设备名会列出该设备的分区表。

[root@node79 ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x00049e93

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
/dev/sda3         4605952   104857599    50125824   83  Linux


添加一块硬盘

“fdisk” 如果不加 “-l” 则进入另一个模式,在该模式下,可以对磁盘进行分区操作。

[root@node79 ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x00049e93

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
/dev/sda3         4605952   104857599    50125824   83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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


[root@node79 ~]# fdisk /dev/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 0xff0f8423.

Command (m for help): 


如果您输入 ‘m’ 会列出常用的命令:

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   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)

Command (m for help): 
“p” 打印当前磁盘的分区情况。

‘n’ 建立一个新的分区。

‘w’ 保存操作。

‘q’ 退出。

‘d’ 删除一个分区

下面把刚才增加的磁盘/dev/sdb进行分区操作。先使用 ‘p’ 命令看一下/dev/sdb的分区状况:



Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xff0f8423

   Device Boot      Start         End      Blocks   Id  System

使用 ‘n’ 命令新建分区,它会提示是要 ‘e’ (扩展分区) 还是 ‘p’ (主分区) [1]  选择是 ‘p’, 于是输入 ‘p’ 然后回车

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-20971519, default 2048): 1
Last sector, +sectors or +size{K,M,G} (4194304-20971519, default 20971519): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xff0f8423

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1         4194304     8388607     2097152   83  Linux

Command (m for help): 

在linux中最多只能创建4个主分区,那如果您想多创建几个分区如何做?很容易,在创建完第三个分区后,创建第四个分区时选择扩展分区。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值