一、df(disk free):列出文件系统的整体磁盘使用量
df命令参数功能:检查文件系统的磁盘空间占用情况。
df [-ahikHTm] [目录或文件名]选项与参数:
-h:以人类可读的方式显示输出结果(例如,使用 KB、MB、GB 等单位)。
-T:显示文件系统的类型。
-t <文件系统类型>:只显示指定类型的文件系统。
-i:显示 inode 使用情况。
-H:该参数是 -h 的变体,但是使用 1000 字节作为基本单位而不是 1024 字节。这意味着它会以 SI(国际单位制)单位(例如 MB、GB)而不是二进制单位(例如 MiB、GiB)来显示磁盘使用情况。
-k:这个选项会以 KB 作为单位显示磁盘空间使用情况。
-a:该参数将显示所有的文件系统,包括虚拟文件系统,例如 proc、sysfs 等。如果没有使用该选项,默认情况下,df 命令不会显示虚拟文件系统。
实例 1
将系统内所有的文件系统列出来!
$ dfFilesystem 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% /boottmpfs 371332 0 371332 0% /dev/shm
在 Linux 底下如果 df 没有加任何选项,那么默认会将系统内所有的 (不含特殊内存内的文件系统与 swap) 都以 1 Kbytes 的容量来列出来!
实例 2
将容量结果以易读的容量格式显示出来
$ df -hFilesystem 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% /boottmpfs 363M 0 363M 0% /dev/shm
实例 3
将系统内的所有特殊文件格式及名称都列出来
$ df -aTFilesystem Type 1K-blocks Used Available Use% Mounted on/dev/hdc2 ext3 9920624 3823112 5585444 41% /proc proc 0 0 0 - /procsysfs sysfs 0 0 0 - /sysdevpts devpts 0 0 0 - /dev/pts/dev/hdc3 ext3 4956316 141376 4559108 4% /home/dev/hdc1 ext3 101086 11126 84741 12% /boottmpfs tmpfs 371332 0 371332 0% /dev/shmnone binfmt_misc 0 0 0 - /proc/sys/fs/binfmt_miscsunrpc rpc_pipefs 0 0 0 - /var/lib/nfs/rpc_pipefs
实例 4
将 /etc 底下的可用的磁盘容量以易读的容量格式显示
$ df -h /etcFilesystem Size Used Avail Use% Mounted on/dev/hdc2 9.5G 3.7G 5.4G 41% /
二、Linux du 命令是对文件和目录磁盘使用的空间的查看。
语法:
du(disk used):检查磁盘空间使用量
du [-ahskm] 文件或目录名称选项与参数:
-a :列出所有的文件与目录容量,因为默认仅统计目录底下的文件量而已。
-h :以人们较易读的容量格式 (G/M) 显示;
-s :仅显示指定目录或文件的总大小,而不显示其子目录的大小。
-S :包括子目录下的总计,与 -s 有点差别。
-k :以 KBytes 列出容量显示;
-m :以 MBytes 列出容量显示;
实例 1
只列出当前目录下的所有文件夹容量(包括隐藏文件夹):
$ du8 ./test4 #每个目录都会列出来8 ./test2....中间省略....12 ./.gconfd #包括隐藏文件的目录220 . #这个目录(.)所占用的总量
直接输入 du 没有加任何选项时,则 du 会分析当前所在目录里的子目录所占用的硬盘空间。
实例 2
将文件的容量也列出来
$ du -a12 ./install.log.syslog #有文件的列表了8 ./.bash_logout8 ./test48 ./test2....中间省略....12 ./.gconfd220 .
实例 3
检查根目录底下每个目录所占用的容量
$ du -sm /*7 /bin6 /boot.....中间省略....0 /proc.....中间省略....1 /tmp3859 /usr #系统初期最大就是他了啦!77 /var
通配符 * 来代表每个目录。
三、fdisk 是 Linux 的磁盘分区表操作工具。
语法:
fdisk [-l] 装置名称选项与参数:
-l :输出后面接的装置所有的分区内容。若仅有 fdisk -l 时, 则系统将会把整个系统内能够搜寻到的装置的分区均列出来。
实例 1
列出所有分区信息
$ fdisk -lDisk /dev/xvda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Device Boot Start End Blocks Id System/dev/xvda1 * 1 2550 20480000 83 Linux/dev/xvda2 2550 2611 490496 82 Linux swap / SolarisDisk /dev/xvdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x56f40944Device Boot Start End Blocks Id System/dev/xvdb2 1 2610 20964793+ 83 Linux
实例 2
找出你系统中的根目录所在磁盘,并查阅该硬盘内的相关信息
$ 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 actiona toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partition #删除一个partitionl list known partition typesm print this menun add a new partition #新增一个partitiono create a new empty DOS partition tablep print the partition table #在屏幕上显示分割表q quit without saving changes #不储存离开fdisk程序s create a new empty Sun disklabelt change a partition's system idu change display/entry unitsv verify the partition tablew 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 #磁头、扇区与磁柱大小Units = cylinders of 16065 * 512 = 8225280 bytes #每个磁柱的大小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# 装置文件名 启动区否 开始磁柱 结束磁柱 1K大小容量 磁盘分区槽内的系统Command (m for help): q
想要不储存按下 q ,不要随便按 w .
使用 p 可以列出目前这颗磁盘的分割表信息,这个信息的上半部在显示整体磁盘的状态。
四、磁盘格式化
磁盘分割完毕后自然就是要进行文件系统的格式化,格式化使用 mkfs(make filesystem) 命令。
语法:
mkfs [-t 文件系统格式] 装置文件名选项与参数:
-t :可以接文件系统格式,例如 ext3, ext2, vfat 等(系统有支持才会生效)
实例 1
查看 mkfs 支持的文件格式
$ mkfs[tab][tab]mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.msdos mkfs.vfat
按下两个[tab],会发现 mkfs 支持的文件格式如上所示。
实例 2
将分区 /dev/hdc6(可指定你自己的分区) 格式化为 ext3 文件系统:
$ mkfs -t ext3 /dev/hdc6mke2fs 1.39 (29-May-2006)Filesystem label= #这里指的是分割槽的名称(label)OS type: LinuxBlock size=4096 (log=2) #block 的大小配置为 4KFragment size=4096 (log=2)251392 inodes, 502023 blocks #由此配置决定的inode/block数量25101 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=51589939216 block groups32768 blocks per group, 32768 fragments per group15712 inodes per groupSuperblock backups stored on blocks:32768, 98304, 163840, 229376, 294912Writing inode tables: doneCreating journal (8192 blocks): done #有日志记录Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 34 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.#这样就创建起来我们所需要的 Ext3 文件系统了!简单明了!
五、磁盘检验
fsck(file system check)用来检查和维护不一致的文件系统。
若系统掉电或磁盘发生问题,可利用fsck命令对文件系统进行检查。
语法:
fsck [-t 文件系统] [-ACay] 装置名称选项与参数:
-t : 给定档案系统的型式,若在 /etc/fstab 中已有定义或 kernel 本身已支援的则不需加上此参数
-s : 依序一个一个地执行 fsck 的指令来检查
-A : 对/etc/fstab 中所有列出来的 分区(partition)做检查
-C : 显示完整的检查进度
-d : 打印出 e2fsck 的 debug 结果
-p : 同时有 -A 条件时,同时有多个 fsck 的检查一起执行
-R : 同时有 -A 条件时,省略 / 不检查
-V : 详细显示模式
-a : 如果检查有错则自动修复
-r : 如果检查有错则由使用者回答是否修复
-y : 选项指定检测每个文件是自动输入yes,在不确定那些是不正常的时候,可以执行 # fsck -y 全部检查修复。
实例 1
查看系统有多少文件系统支持的 fsck 命令:
$ fsck[tab][tab]fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.msdos fsck.vfat
实例 2
强制检测 /dev/hdc6 分区:
$ fsck -C -f -t ext3 /dev/hdc6fsck 1.39 (29-May-2006)e2fsck 1.39 (29-May-2006)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary informationvbird_logical: 11/251968 files (9.1% non-contiguous), 36926/1004046 blocks
六、磁盘挂载与卸除
Linux 的磁盘挂载使用 mount 命令,卸载使用 umount 命令。
磁盘挂载语法:
mount [-t 文件系统] [-L Label名] [-o 额外选项] [-n] 装置文件名 挂载点实例 1
用默认的方式,将刚刚创建的 /dev/hdc6 挂载到 /mnt/hdc6 上面!
$ mkdir /mnt/hdc6$ mount /dev/hdc6 /mnt/hdc6$ dfFilesystem 1K-blocks Used Available Use% Mounted on.....中间省略...../dev/hdc6 1976312 42072 1833836 3% /mnt/hdc6
磁盘卸载命令 umount 语法:
umount [-fn] 装置文件名或挂载点选项与参数:
-f :强制卸除!可用在类似网络文件系统 (NFS) 无法读取到的情况下;
-n :不升级 /etc/mtab 情况下卸除。
卸载/dev/hdc6
$ umount /dev/hdc6