歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux 磁盤管理:df,du,fdisk,mkfs,fsck

Linux 磁盤管理:df,du,fdisk,mkfs,fsck

日期:2017/3/2 9:38:35   编辑:關於Linux

Linux磁盤管理好壞管理直接關系到整個系統的性能問題。
Linux磁盤管理常用幾個個命令為df,du,fdisk,mkfs,fsck。
1.df:列出文件系統的整體磁盤使用量
2.du:檢查磁盤空間使用量
3.fdisk:用於磁盤分區
4.mkfs:格式化
5.fsck:磁盤檢查

df

df命令參數功能:檢查文件系統的磁盤空間占用情況。可以利用該命令來獲取硬盤被占用了多少空間,目前還剩下多少空間等信息。

語法:
df [-ahikHTm] [目錄或文件名]

選項與參數:
1.-a :列出所有的文件系統,包括系統特有的 /proc 等文件系統;
2.-k :以 KBytes 的容量顯示各文件系統;
3.-m :以 MBytes 的容量顯示各文件系統;
4.-h :以人們較易閱讀的 GBytes, MBytes, KBytes 等格式自行顯示;
5.-H :以 M=1000K 取代 M=1024K 的進位方式;
6.-T :顯示文件系統類型, 連同該 partition 的 filesystem 名稱 (例如 ext3) 也列出;
7.-i :不用硬盤容量,而以 inode 的數量來顯示

實例 1
將系統內所有的文件系統列出來!
[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

在 Linux 底下如果 df 沒有加任何選項,那麼默認會將系統內所有的 (不含特殊內存內的文件系統與 swap) 都以 1 Kbytes 的容量來列出來!

實例 2
將容量結果以易讀的容量格式顯示出來
[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

實例 3
將系統內的所有特殊文件格式及名稱都列出來
[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

實例 4
將 /etc 底下的可用的磁盤容量以易讀的容量格式顯示
[root@www ~]# df -h /etc
Filesystem Size Used Avail Use% Mounted on
/dev/hdc2 9.5G 3.7G 5.4G 41% /


du

linux du命令也是查看使用空間的,但是與df命令不同的是Linux du命令是對文件和目錄磁盤使用的空間的查看,還是和df命令有一些區別的,這裡介紹Linux du命令。
語法:
du [-ahskm] 文件或目錄名稱

選項與參數:
1.-a :列出所有的文件與目錄容量,因為默認僅統計目錄底下的文件量而已。
2.-h :以人們較易讀的容量格式 (G/M) 顯示;
3.-s :列出總量而已,而不列出每個各別的目錄占用容量;
4.-S :不包括子目錄下的總計,與 -s 有點差別。
5.-k :以 KBytes 列出容量顯示;
6.-m :以 MBytes 列出容量顯示;

實例 1
列出目前目錄下的所有文件容量
[root@www ~]# du
8 ./test4 <==每個目錄都會列出來
8 ./test2
....中間省略....
12 ./.gconfd <==包括隱藏文件的目錄
220 . <==這個目錄(.)所占用的總量

直接輸入 du 沒有加任何選項時,則 du 會分析當前所在目錄的文件與目錄所占用的硬盤空間。

實例 2
將文件的容量也列出來
[root@www ~]# du -a
12 ./install.log.syslog <==有文件的列表了
8 ./.bash_logout
8 ./test4
8 ./test2
....中間省略....
12 ./.gconfd
220 .

實例 3
檢查根目錄底下每個目錄所占用的容量
[root@www ~]# du -sm /*
7 /bin
6 /boot
.....中間省略....
0 /proc
.....中間省略....
1 /tmp
3859 /usr <==系統初期最大就是他了啦!
77 /var

通配符 * 來代表每個目錄。
與 df 不一樣的是,du 這個命令其實會直接到文件系統內去搜尋所有的文件數據。

fdisk

fdisk 是 Linux 的磁盤分區表操作工具。
語法:
fdisk [-l] 裝置名稱

選項與參數:
1.-l :輸出後面接的裝置所有的分區內容。若僅有 fdisk -l 時, 則系統將會把整個系統內能夠搜尋到的裝置的分區均列出來。

實例 1
列出所有分區信息
[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 / Solaris

Disk /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

實例 2
找出你系統中的根目錄所在磁盤,並查閱該硬盤內的相關信息
[
root@www ~]# df / <==注意:重點在找出磁盤文件名而已
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdc2 9920624 3823168 5585388 41% /

[root@www ~]# fdisk /dev/hdc <==仔細看,不要加上數字喔!
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): <==等待你的輸入!

輸入 m 後,就會看到底下這些命令介紹
Command (m for help): m <== 輸入 m 後,就會看到底下這些命令介紹
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition <==刪除一個partition
l list known partition types
m print this menu
n add a new partition <==新增一個partition
o create a new empty DOS partition table
p print the partition table <==在屏幕上顯示分割表
q quit without saving changes <==不儲存離開fdisk程序
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)

離開 fdisk 時按下 q,那麼所有的動作都不會生效!相反的, 按下w就是動作生效的意思。
Command (m for help): p <== 這裡可以輸出目前磁盤的狀態

Disk /dev/hdc: 41.1 GB, 41174138880 bytes <==這個磁盤的文件名與容量
255 heads, 63 sectors/track, 5005 cylinders <==磁頭、扇區與磁柱大小

Copyright © Linux教程網 All Rights Reserved