Linux磁盤(pán)管理好壞直接關(guān)系到整個(gè)系統(tǒng)的性能問(wèn)題。
Linux磁盤(pán)管理常用三個(gè)命令為df、du和fdisk。
df:列出文件系統(tǒng)的整體磁盤(pán)使用量
du:檢查磁盤(pán)空間使用量
fdisk:用于磁盤(pán)分區(qū)
df命令參數(shù)功能:檢查文件系統(tǒng)的磁盤(pán)空間占用情況??梢岳迷撁顏?lái)獲取硬盤(pán)被占用了多少空間,目前還剩下多少空間等信息。
語(yǔ)法:
df [-ahikHTm] [目錄或文件名]
選項(xiàng)與參數(shù):
-a :列出所有的文件系統(tǒng),包括系統(tǒng)特有的 /proc 等文件系統(tǒng);
-k :以 KBytes 的容量顯示各文件系統(tǒng);
-m :以 MBytes 的容量顯示各文件系統(tǒng);
-h :以人們較易閱讀的 GBytes, MBytes, KBytes 等格式自行顯示;
-H :以 M=1000K 取代 M=1024K 的進(jìn)位方式;
-T :顯示文件系統(tǒng)類(lèi)型, 連同該 partition 的 filesystem 名稱(chēng) (例如 ext3) 也列出;
-i :不用硬盤(pán)容量,而以 inode 的數(shù)量來(lái)顯示
將系統(tǒng)內(nèi)所有的文件系統(tǒng)列出來(lái)!
[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 沒(méi)有加任何選項(xiàng),那么默認(rèn)會(huì)將系統(tǒng)內(nèi)所有的 (不含特殊內(nèi)存內(nèi)的文件系統(tǒng)與 swap) 都以 1 Kbytes 的容量來(lái)列出來(lái)!
將容量結(jié)果以易讀的容量格式顯示出來(lái)
[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
將系統(tǒng)內(nèi)的所有特殊文件格式及名稱(chēng)都列出來(lái)
[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
將 /etc 底下的可用的磁盤(pán)容量以易讀的容量格式顯示
[root@www ~]# df -h /etc Filesystem Size Used Avail Use% Mounted on /dev/hdc2 9.5G 3.7G 5.4G 41% /
Linux du命令也是查看使用空間的,但是與df命令不同的是Linux du命令是對(duì)文件和目錄磁盤(pán)使用的空間的查看,還是和df命令有一些區(qū)別的,這里介紹Linux du命令。
語(yǔ)法:
du [-ahskm] 文件或目錄名稱(chēng)
選項(xiàng)與參數(shù):
-a :列出所有的文件與目錄容量,因?yàn)槟J(rèn)僅統(tǒng)計(jì)目錄底下的文件量而已。
-h :以人們較易讀的容量格式 (G/M) 顯示;
-s :列出總量而已,而不列出每個(gè)各別的目錄占用容量;
-S :不包括子目錄下的總計(jì),與 -s 有點(diǎn)差別。
-k :以 KBytes 列出容量顯示;
-m :以 MBytes 列出容量顯示;
只列出當(dāng)前目錄下的所有文件夾容量(包括隱藏文件夾):
[root@www ~]# du 8 ./test4 <==每個(gè)目錄都會(huì)列出來(lái) 8 ./test2 ....中間省略.... 12 ./.gconfd <==包括隱藏文件的目錄 220 . <==這個(gè)目錄(.)所占用的總量
直接輸入 du 沒(méi)有加任何選項(xiàng)時(shí),則 du 會(huì)分析當(dāng)前所在目錄的文件與目錄所占用的硬盤(pán)空間。
將文件的容量也列出來(lái)
[root@www ~]# du -a 12 ./install.log.syslog <==有文件的列表了 8 ./.bash_logout 8 ./test4 8 ./test2 ....中間省略.... 12 ./.gconfd 220 .
檢查根目錄底下每個(gè)目錄所占用的容量
[root@www ~]# du -sm /* 7 /bin 6 /boot .....中間省略.... 0 /proc .....中間省略.... 1 /tmp 3859 /usr <==系統(tǒng)初期最大就是他了啦! 77 /var
通配符 * 來(lái)代表每個(gè)目錄。
與 df 不一樣的是,du 這個(gè)命令其實(shí)會(huì)直接到文件系統(tǒng)內(nèi)去搜尋所有的文件數(shù)據(jù)。
fdisk 是 Linux 的磁盤(pán)分區(qū)表操作工具。
語(yǔ)法:
fdisk [-l] 裝置名稱(chēng)
選項(xiàng)與參數(shù):
-l :輸出后面接的裝置所有的分區(qū)內(nèi)容。若僅有 fdisk -l 時(shí), 則系統(tǒng)將會(huì)把整個(gè)系統(tǒng)內(nèi)能夠搜尋到的裝置的分區(qū)均列出來(lái)。
列出所有分區(qū)信息
[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
找出你系統(tǒng)中的根目錄所在磁盤(pán),并查閱該硬盤(pán)內(nèi)的相關(guān)信息
[root@www ~]# df / <==注意:重點(diǎn)在找出磁盤(pán)文件名而已 Filesystem 1K-blocks Used Available Use% Mounted on /dev/hdc2 9920624 3823168 5585388 41% / [root@www ~]# fdisk /dev/hdc <==仔細(xì)看,不要加上數(shù)字喔! 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 后,就會(huì)看到底下這些命令介紹
Command (m for help): m <== 輸入 m 后,就會(huì)看到底下這些命令介紹 Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition <==刪除一個(gè)partition l list known partition types m print this menu n add a new partition <==新增一個(gè)partition o create a new empty DOS partition table p print the partition table <==在屏幕上顯示分割表 q quit without saving changes <==不儲(chǔ)存離開(kāi)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 <==將剛剛的動(dòng)作寫(xiě)入分割表 x extra functionality (experts only)
離開(kāi) fdisk 時(shí)按下 q,那么所有的動(dòng)作都不會(huì)生效!相反的, 按下w就是動(dòng)作生效的意思。
Command (m for help): p <== 這里可以輸出目前磁盤(pán)的狀態(tài) Disk /dev/hdc: 41.1 GB, 41174138880 bytes <==這個(gè)磁盤(pán)的文件名與容量 255 heads, 63 sectors/track, 5005 cylinders <==磁頭、扇區(qū)與磁柱大小 Units = cylinders of 16065 * 512 = 8225280 bytes <==每個(gè)磁柱的大小 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 # 裝置文件名 啟動(dòng)區(qū)否 開(kāi)始磁柱 結(jié)束磁柱 1K大小容量 磁盤(pán)分區(qū)槽內(nèi)的系統(tǒng) Command (m for help): q
想要不儲(chǔ)存離開(kāi)嗎?按下 q 就對(duì)了!不要隨便按 w ?。?/p>
使用 p 可以列出目前這顆磁盤(pán)的分割表信息,這個(gè)信息的上半部在顯示整體磁盤(pán)的狀態(tài)。
磁盤(pán)分割完畢后自然就是要進(jìn)行文件系統(tǒng)的格式化,格式化的命令非常的簡(jiǎn)單,使用 mkfs(make filesystem) 命令。
語(yǔ)法:
mkfs [-t 文件系統(tǒng)格式] 裝置文件名
選項(xiàng)與參數(shù):
-t :可以接文件系統(tǒng)格式,例如 ext3, ext2, vfat 等(系統(tǒng)有支持才會(huì)生效)
查看 mkfs 支持的文件格式
[root@www ~]# mkfs[tab][tab] mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.msdos mkfs.vfat
按下兩個(gè)[tab],會(huì)發(fā)現(xiàn) mkfs 支持的文件格式如上所示。
將分區(qū) /dev/hdc6(可指定你自己的分區(qū)) 格式化為 ext3 文件系統(tǒng):
[root@www ~]# mkfs -t ext3 /dev/hdc6 mke2fs 1.39 (29-May-2006) Filesystem label= <==這里指的是分割槽的名稱(chēng)(label) OS type: Linux Block size=4096 (log=2) <==block 的大小配置為 4K Fragment size=4096 (log=2) 251392 inodes, 502023 blocks <==由此配置決定的inode/block數(shù)量 25101 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=515899392 16 block groups 32768 blocks per group, 32768 fragments per group 15712 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (8192 blocks): done <==有日志記錄 Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 34 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. # 這樣就創(chuàng)建起來(lái)我們所需要的 Ext3 文件系統(tǒng)了!簡(jiǎn)單明了!
fsck(file system check)用來(lái)檢查和維護(hù)不一致的文件系統(tǒng)。
若系統(tǒng)掉電或磁盤(pán)發(fā)生問(wèn)題,可利用fsck命令對(duì)文件系統(tǒng)進(jìn)行檢查。
語(yǔ)法:
fsck [-t 文件系統(tǒng)] [-ACay] 裝置名稱(chēng)
選項(xiàng)與參數(shù):
-t : 給定檔案系統(tǒng)的型式,若在 /etc/fstab 中已有定義或 kernel 本身已支援的則不需加上此參數(shù)
-s : 依序一個(gè)一個(gè)地執(zhí)行 fsck 的指令來(lái)檢查
-A : 對(duì)/etc/fstab 中所有列出來(lái)的 分區(qū)(partition)做檢查
-C : 顯示完整的檢查進(jìn)度
-d : 打印出 e2fsck 的 debug 結(jié)果
-p : 同時(shí)有 -A 條件時(shí),同時(shí)有多個(gè) fsck 的檢查一起執(zhí)行
-R : 同時(shí)有 -A 條件時(shí),省略 / 不檢查
-V : 詳細(xì)顯示模式
-a : 如果檢查有錯(cuò)則自動(dòng)修復(fù)
-r : 如果檢查有錯(cuò)則由使用者回答是否修復(fù)
-y : 選項(xiàng)指定檢測(cè)每個(gè)文件是自動(dòng)輸入yes,在不確定那些是不正常的時(shí)候,可以執(zhí)行 # fsck -y 全部檢查修復(fù)。
查看系統(tǒng)有多少文件系統(tǒng)支持的 fsck 命令:
[root@www ~]# fsck[tab][tab] fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.msdos fsck.vfat
強(qiáng)制檢測(cè) /dev/hdc6 分區(qū):
[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
如果沒(méi)有加上 -f 的選項(xiàng),則由于這個(gè)文件系統(tǒng)不曾出現(xiàn)問(wèn)題,檢查的經(jīng)過(guò)非常快速!若加上 -f 強(qiáng)制檢查,才會(huì)一項(xiàng)一項(xiàng)的顯示過(guò)程。
Linux 的磁盤(pán)掛載使用 mount 命令,卸載使用 umount 命令。
磁盤(pán)掛載語(yǔ)法:
mount [-t 文件系統(tǒng)] [-L Label名] [-o 額外選項(xiàng)] [-n] 裝置文件名 掛載點(diǎn)
用默認(rèn)的方式,將剛剛創(chuàng)建的 /dev/hdc6 掛載到 /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 .....中間省略..... /dev/hdc6 1976312 42072 1833836 3% /mnt/hdc6
磁盤(pán)卸載命令 umount 語(yǔ)法:
umount [-fn] 裝置文件名或掛載點(diǎn)
選項(xiàng)與參數(shù):
-f :強(qiáng)制卸除!可用在類(lèi)似網(wǎng)絡(luò)文件系統(tǒng) (NFS) 無(wú)法讀取到的情況下;
-n :不升級(jí) /etc/mtab 情況下卸除。
卸載/dev/hdc6
[root@www ~]# umount /dev/hdc6