Linux 磁盘管理

Linux 磁盘管理常用三个命令为 df、du 和 fdisk。

1. df

df命令参数功能:检查文件系统的磁盘空间占用情况。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息。

1.1 基本语法

语法:

df [-ahikHTm] [目录或文件名]

选项与参数:

1.2 实操案例1

将系统内所有的文件系统列出来。

[root@jiangnan ~]# dfFilesystem     1K-blocks     Used Available Use% Mounted ondevtmpfs          877596        0    877596   0% /devtmpfs             888096        0    888096   0% /dev/shmtmpfs             888096      736    887360   1% /runtmpfs             888096        0    888096   0%[root@jiangnan ~]#

在 Linux 底下如果 df 没有加任何选项,那么默认会将系统内所有的 (不含特殊内存内的文件系统与 swap) 都以 1 的容量来列出来!

1.3 实操案例2

将 /etc 底下的可用的磁盘容量以易读的容量格式显示

[root@jiangnan ~]# df -h /etcFilesystem     Size Used Avail Use% Mounted on/dev/vda1       40G   11G   27G  29% /[root@jiangnan ~]#

2. du

Linux du 命令也是查看使用空间的,但是与 df 命令不同的是 Linux du 命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的,这里介绍 Linux du 命令。

2.1 基本语法

du [-ahskm] 文件或目录名称

选项与参数:

2.2 实操案例1

只列出当前目录下的所有文件夹容量(包括隐藏文件夹):

[root@jiangnan data]# du12./include20./array24./redirect116.[root@jiangnan data]#

直接输入 du 没有加任何选项时linux格式化命令,则 du 会分析当前所在目录里的子目录所占用的硬盘空间。

2.3 实操案例2

将文件的容量也列出来

[root@jiangnan data]# du -a4./if2.sh4./data.sh4./for2.sh4./while1.sh4./fun.sh4./until.sh4./fun1.sh24./redirect116.[root@jiangnan data]#

注意:有时可以使用通配符 * 来代表每个目录。

与 df 不一样的是,du 这个命令其实会直接到文件系统内去搜寻所有的文件数据。

3. fdisk

fdisk 是 Linux 的磁盘分区表操作工具。

3.1 基本语法

fdisk [-l] 装置名称

选项与参数:

3.2 实操案例1

列出所有分区信息

[root@jiangnan data]# fdisk -l
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000bb9c1
Device Boot Start End Blocks Id System/dev/vda1 * 2048 83886046 41941999+ 83 Linux[root@jiangnan data]#

3.3 实操案例2

找出你系统中的根目录所在磁盘,并查阅该硬盘内的相关信息

[root@jiangnan data]# df /   # 找出磁盘文件名Filesystem     1K-blocks     Used Available Use% Mounted on/dev/vda1       41152812 11084324  28164780  29% /[root@jiangnan data]# fdisk /dev/vda   # 仔细看,不要加上数字喔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.

Command (m for help): mCommand action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition # 删除一个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 # 新增一个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)
Command (m for help): p # 这里可以输出目前磁盘的状态
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000bb9c1
Device Boot Start End Blocks Id System/dev/vda1 * 2048 83886046 41941999+ 83 Linux
Command (m for help): q # 离开 fdisk 时按下 q,那么所有的动作都不会生效!相反的, 按下w就是动作生效的意思。
[root@jiangnan data]#

想要不储存离开吗?按下 q 就对了!不要随便按 w 啊!

使用 p 可以列出目前这颗磁盘的分割表信息,这个信息的上半部在显示整体磁盘的状态。

4. 磁盘格式化

磁盘分割完毕后自然就是要进行文件系统的格式化,格式化的命令非常的简单linux格式化命令,使用 mkfs(make ) 命令。

4.1 基本语法

mkfs [-t 文件系统格式] 装置文件名

选项与参数:

如果想查看mkfs支持的文件格式,可以使用

[root@jiangnan data]# mkfs[tab][tab]mkfs         mkfs.btrfs   mkfs.cramfs mkfs.ext2   mkfs.ext3   mkfs.ext4   mkfs.minix   mkfs.xfs    [root@jiangnan data]# mkfs

按下两个[tab],会发现 mkfs 支持的文件格式如上所示。

4.2 实操案例

将分区 /dev/hdc6(可指定你自己的分区) 格式化为 ext3 文件系统:

[root@jiangnan ~]# 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, 294912
Writing inode tables: doneCreating journal (8192 blocks): done <==有日志记录Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 34 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.# 这样就创建起来我们所需要的 Ext3 文件系统了!简单明了!

5. 磁盘检验

fsck(file check)用来检查和维护不一致的文件系统。

若系统掉电或磁盘发生问题,可利用fsck命令对文件系统进行检查。

5.1 基本语法

fsck [-t 文件系统] [-ACay] 装置名称

选项与参数:

5.2 实操案例1

查看系统有多少文件系统支持的 fsck 命令:

[root@jiangnan data]# fsckfsck         fsck.btrfs   fsck.cramfs fsck.ext2   fsck.ext3   fsck.ext4   fsck.minix   fsck.xfs    [root@jiangnan data]# fsck

5.3 实操案例2

强制检测 /dev/hdc6 分区:

[root@jiangnan ~]# 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

如果没有加上 -f 的选项,则由于这个文件系统不曾出现问题,检查的经过非常快速!若加上 -f 强制检查,才会一项一项的显示过程。

6. 磁盘挂载与卸除

Linux 的磁盘挂载使用 mount 命令,卸载使用 命令。

6.1 基本语法

磁盘挂载语法:

mount [-t 文件系统] [-L Label名] [-o 额外选项] [-n] 装置文件名 挂载点

磁盘卸载命令 语法:

umount [-fn] 装置文件名或挂载点

选项与参数:

6.2 实操案例1

用默认的方式,将刚刚创建的 /dev/hdc6 挂载到 /mnt/hdc6 上面!

[root@jiangnan ~]# mkdir /mnt/hdc6[root@jiangnan ~]# mount /dev/hdc6 /mnt/hdc6[root@jiangnan ~]# dfFilesystem           1K-blocks     Used Available Use% Mounted on.....中间省略...../dev/hdc6              1976312     42072   1833836   3% /mnt/hdc6

6.3 实操案例2

卸载/dev/hdc6

[root@jiangnan ~]# umount /dev/hdc6

图片


限时特惠:
本站持续每日更新海量各大内部创业课程,一年会员仅需要98元,全站资源免费下载
点击查看详情

站长微信:Jiucxh

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注