磁盤分區及管理

df

df命令用於顯示磁盤分區上的可使用的磁盤空間。默認顯示單位爲KB。可以利用該命令來獲取硬盤被佔用了多少空間,目前還剩下多少空間等信息。

參數:

-a:包含全部的文件系統;
--block-size=<區塊大小>:以指定的區塊大小來顯示區塊數目;
-h:以可讀性較高的方式來顯示信息;
-H:與-h參數相同,但在計算時是以1000 Bytes爲換算單位而非1024 Bytes;
-i:顯示inode的信息;
-l:僅顯示本地端的文件系統;
-m:指定區塊大小爲1048576字節;
--help:顯示幫助;
--version:顯示版本信息。

實例:  
查看系統磁盤設備,默認是KB爲單位:

[root@localhost ~]# df
文件系統               1K-塊        已用     可用 已用% 掛載點
/dev/sda2            146294492  28244432 110498708  21% /
/dev/sda1              1019208     62360    904240   7% /boot
tmpfs                  1032204         0   1032204   0% /dev/shm
/dev/sdb1            2884284108 218826068 2518944764   8% /data1

使用-h選項以KB以上的單位來顯示,可讀性高:

[root@localhost ~]# df -h
文件系統              容量  已用 可用 已用% 掛載點
/dev/sda2             140G   27G  106G  21% /
/dev/sda1             996M   61M  884M   7% /boot
tmpfs                1009M     0 1009M   0% /dev/shm
/dev/sdb1             2.7T  209G  2.4T   8% /data1

查看全部文件系統:

[root@localhost ~]# df -a
文件系統               1K-塊        已用     可用 已用% 掛載點
/dev/sda2            146294492  28244432 110498708  21% /
proc                         0         0         0   -  /proc
sysfs                        0         0         0   -  /sys
devpts                       0         0         0   -  /dev/pts
/dev/sda1              1019208     62360    904240   7% /boot
tmpfs                  1032204         0   1032204   0% /dev/shm
/dev/sdb1            2884284108 218826068 2518944764   8% /data1

du

du命令也是查看使用空間的,但是與df命令不同的是 du命令是對文件和目錄磁盤使用的空間的查看,還是和df命令有一些區別的。

參數:

-k 以KB(1024bytes)爲單位輸出。
-m 以MB爲單位輸出。
-s 僅顯示總計,只列出最後加總的值。
-h 以K,M,G爲單位,提高信息的可讀性。
-S 顯示個別目錄的大小時,並不含其子目錄的大小。

實例:  
顯示目錄或者文件所佔空間:

[root@localhost test]# du
608 ./test6
308 ./test4
4 ./scf/lib
4 ./scf/service/deploy/product
4 ./scf/service/deploy/info
12 ./scf/service/deploy
16 ./scf/service
4 ./scf/doc
4 ./scf/bin
32 ./scf
8 ./test3
1288 .

只顯示當前目錄下面的子目錄的目錄大小和當前目錄的總的大小,最下面的1288爲當前目錄的總大小

顯示指定文件所佔空間:

[root@localhost test]# du log2017.log
300 log2017.log

查看指定目錄的所佔空間:

[root@localhost test]# du scf
4 scf/lib
4 scf/service/deploy/product
4 scf/service/deploy/info
12 scf/service/deploy
16 scf/service
4 scf/doc
4 scf/bin
32 scf

顯示多個文件所佔空間:

[root@localhost test]# du log30.tar.gz log31.tar.gz
4 log30.tar.gz
4 log31.tar.gz

只顯示總和的大小:

[root@localhost test]# du -s
1288 .

[root@localhost test]# du -s scf
32 scf

[root@localhost test]# cd ..
[root@localhost soft]# du -s test
1288 test

Tips:

ls -lh 看到的大小和du相比
ls -lh是實際大小;du是佔用多少空間,一般都是4K的整數倍(取決與你格式化時候指定的block大小)

fdisk

fdisk命令用於觀察硬盤實體使用情況,也可對硬盤分區

實例操作:  
選擇要進行操作的磁盤:

[root@localhost ~]# fdisk /dev/sdb

輸入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    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)

輸入p列出磁盤目前的分區情況:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           1        8001   8e  Linux LVM
/dev/sdb2               2          26      200812+  83  Linux

輸入d然後選擇分區,刪除現有分區:

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Selected partition 2

查看分區情況,確認分區已經刪除:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes    Device Boot      Start         End      Blocks   Id  System

Command (m for help):

輸入n建立新的磁盤分區,首先建立兩個主磁盤分區:

Command (m for help): n
Command action    e   extended    p   primary partition (1-4)
p    #建立主分區
Partition number (1-4): 1  #分區號
First cylinder (1-391, default 1):  #分區起始位置
Using default value 1
last cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100  #分區結束位置,單位爲扇區

Command (m for help): n #再建立一個分區
Command action    e   extended    p   primary partition (1-4)
p     #建立主分區
Partition number (1-4): 2  #分區號爲2
First cylinder (101-391, default 101):
Using default value 101
Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M  #分區結束位置,單位爲M

確認分區建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux

再建立一個邏輯分區:

Command (m for help): n
Command action    e   extended    p   primary partition (1-4)
e  #選擇擴展分區
Partition number (1-4): 3
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
Using default value 391

確認擴展分區建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux
/dev/sdb3             126         391     2136645    5  Extended

在擴展分區上建立兩個邏輯分區:

Command (m for help): n
Command action    l   logical (5 or over)    p   primary partition (1-4)
l #選擇邏輯分區
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M     Command (m for help): n
Command action    l   logical (5 or over)    p   primary partition (1-4)
l
First cylinder (176-391, default 176):
Using default value 176
Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
Using default value 391

確認邏輯分區建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux
/dev/sdb3             126         391     2136645    5  Extended
/dev/sdb5             126         175      401593+  83  Linux
/dev/sdb6             176         391     1734988+  83  Linux

Command (m for help):

以上操作:在硬盤sdb建立了2個主分區(sdb1,sdb2),1個擴展分區(sdb3),2個邏輯分區(sdb5,sdb6)  
注意:主分區和擴展分區的磁盤號位1-4,也就是說最多有4個主分區或者擴展分區,邏輯分區開始的磁盤號爲5,因此沒有sdb4。

最後對分區操作進行保存:

Command (m for help): w
The partition table has been altered!

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

建立好分區之後,還需要對分區進行格式化才能在系統中使用磁盤。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章