linux学习lesson14

本文详细介绍Linux系统中df和du命令的使用方法,包括不同参数的解释与实例,以及如何利用fdisk进行磁盘分区操作,涵盖主分区、扩展分区的创建与管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1 df命令

2 du命令

3 磁盘分区


1 df命令

  • 命令dfdisk filesystem的简写)用于查看已挂载磁盘的总容量、使用容量、剩余容量等,可以不加任何参数,默认以KB为单位显

-i:表示查看inodes的使用状况,如已使用100%,即使磁盘空间有富余,也会提示磁盘空间已满

-h:表示使用合适的单位显示,例如GB

-k-m:分别表示以KBMB为单位显示

[root@worker1 ~]# df -i
Filesystem       Inodes IUsed    IFree IUse% Mounted on
/dev/sda3      18812928 24912 18788016    1% /
devtmpfs         232207   370   231837    1% /dev
tmpfs            234575     1   234574    1% /dev/shm
tmpfs            234575   427   234148    1% /run
tmpfs            234575    13   234562    1% /sys/fs/cgroup
/dev/sda1        204800   330   204470    1% /boot
tmpfs            234575     1   234574    1% /run/user/0
[root@worker1 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  1.9G   17G  11% /
devtmpfs        908M     0  908M   0% /dev
tmpfs           917M     0  917M   0% /dev/shm
tmpfs           917M  8.5M  908M   1% /run
tmpfs           917M     0  917M   0% /sys/fs/cgroup
/dev/sda1       197M  103M   94M  53% /boot
tmpfs           184M     0  184M   0% /run/user/0
[root@worker1 ~]# df -k
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       18802688 1956628  16846060  11% /
devtmpfs          928828       0    928828   0% /dev
tmpfs             938300       0    938300   0% /dev/shm
tmpfs             938300    8700    929600   1% /run
tmpfs             938300       0    938300   0% /sys/fs/cgroup
/dev/sda1         201388  105224     96164  53% /boot
tmpfs             187664       0    187664   0% /run/user/0
[root@worker1 ~]# df -m
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/sda3          18362  1911     16452  11% /
devtmpfs             908     0       908   0% /dev
tmpfs                917     0       917   0% /dev/shm
tmpfs                917     9       908   1% /run
tmpfs                917     0       917   0% /sys/fs/cgroup
/dev/sda1            197   103        94  53% /boot
tmpfs                184     0       184   0% /run/user/0
  • free命令可以查看swap状态
[root@worker1 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1876604      106036     1642596        8704      127972     1637472
Swap:       1952764           0     1952764

 

2 du命令

  • 命令dudisk useage)用来查看某个目录或文件所占空间的大小,其格式为 du [-abckmsh] [文件或者目录名]

-a:表示全部文件和目录的大小都列出来。如果后面不加任何选项和参数,则只会列出目录(包含子目录)的大小。如果du命令不指定单位的话,默认显示单位为“KB

-b:表示列出的值以B为单位输出。

-k:表示以KB为单位输出,这和默认不加任何选项的输出值是一样的。

-m:表示以MB为单位输出。

-h:表示系统自动调节单位。例如,如果文件太小,可能就几千字节,就以KB为单位显示;如果文件大到千兆字节,就以GB为单位显示。若一个文件小于4KB,当使用-k选项时,也会显示4KB,同理,使用-m选项时,也会有类似问题,这里涉及到系统存储单位为4096B即4KB

-c:表示最后加总

-s:表示只列出总和

[root@worker1 ~]# du -a /root/
4    /root/.bash_logout
4    /root/.bash_profile
4    /root/.bashrc
4    /root/.cshrc
4    /root/.tcshrc
4    /root/anaconda-ks.cfg
16    /root/.bash_history
4    /root/.ssh/known_hosts
4    /root/.ssh/id_rsa
4    /root/.ssh/id_rsa.pub
4    /root/.ssh/authorized_keys
16    /root/.ssh
0    /root/file1.txt.bak
0    /root/file2.txt.bak
4    /root/.lesshst
4    /root/dir1/file1.txt
4    /root/dir1
4    /root/file.txt
4    /root/hard.txt.bak
4    /root/file_windows.txt
4    /root/.Xauthority
84    /root/
[root@worker1 ~]# du -b /tmp/
6    /tmp/.ICE-unix
6    /tmp/.X11-unix
6    /tmp/.XIM-unix
6    /tmp/.font-unix
6    /tmp/.Test-unix
23    /tmp/lesson8/dir1/dir2
121    /tmp/lesson8/dir1
138    /tmp/lesson8
6    /tmp/lesson9/dir1
6    /tmp/lesson9/dir2/dir3
6    /tmp/lesson9/dir2/dir4
6    /tmp/lesson9/dir2/file2.txt
78    /tmp/lesson9/dir2
112    /tmp/lesson9
27    /tmp/dir2
6    /tmp/dir1/dir2
51    /tmp/dir1
4500    /tmp/
[root@worker1 ~]# du -k /tmp/
0    /tmp/.ICE-unix
0    /tmp/.X11-unix
0    /tmp/.XIM-unix
0    /tmp/.font-unix
0    /tmp/.Test-unix
4    /tmp/lesson8/dir1/dir2
8    /tmp/lesson8/dir1
8    /tmp/lesson8
0    /tmp/lesson9/dir1
0    /tmp/lesson9/dir2/dir3
0    /tmp/lesson9/dir2/dir4
0    /tmp/lesson9/dir2/file2.txt
0    /tmp/lesson9/dir2
0    /tmp/lesson9
0    /tmp/dir2
0    /tmp/dir1/dir2
4    /tmp/dir1
20    /tmp/
[root@worker1 ~]# du -m /tmp/
0    /tmp/.ICE-unix
0    /tmp/.X11-unix
0    /tmp/.XIM-unix
0    /tmp/.font-unix
0    /tmp/.Test-unix
1    /tmp/lesson8/dir1/dir2
1    /tmp/lesson8/dir1
1    /tmp/lesson8
0    /tmp/lesson9/dir1
0    /tmp/lesson9/dir2/dir3
0    /tmp/lesson9/dir2/dir4
0    /tmp/lesson9/dir2/file2.txt
0    /tmp/lesson9/dir2
0    /tmp/lesson9
0    /tmp/dir2
0    /tmp/dir1/dir2
1    /tmp/dir1
1    /tmp/
[root@worker1 ~]# du -h /tmp/
0    /tmp/.ICE-unix
0    /tmp/.X11-unix
0    /tmp/.XIM-unix
0    /tmp/.font-unix
0    /tmp/.Test-unix
4.0K    /tmp/lesson8/dir1/dir2
8.0K    /tmp/lesson8/dir1
8.0K    /tmp/lesson8
0    /tmp/lesson9/dir1
0    /tmp/lesson9/dir2/dir3
0    /tmp/lesson9/dir2/dir4
0    /tmp/lesson9/dir2/file2.txt
0    /tmp/lesson9/dir2
0    /tmp/lesson9
0    /tmp/dir2
0    /tmp/dir1/dir2
4.0K    /tmp/dir1
20K    /tmp/
[root@worker1 ~]# du -c /tmp/
0    /tmp/.ICE-unix
0    /tmp/.X11-unix
0    /tmp/.XIM-unix
0    /tmp/.font-unix
0    /tmp/.Test-unix
4    /tmp/lesson8/dir1/dir2
8    /tmp/lesson8/dir1
8    /tmp/lesson8
0    /tmp/lesson9/dir1
0    /tmp/lesson9/dir2/dir3
0    /tmp/lesson9/dir2/dir4
0    /tmp/lesson9/dir2/file2.txt
0    /tmp/lesson9/dir2
0    /tmp/lesson9
0    /tmp/dir2
0    /tmp/dir1/dir2
4    /tmp/dir1
20    /tmp/
20    total
[root@worker1 ~]# du -s /tmp/
20    /tmp/
  • 习惯上,比较常用的形式是 du -sh
[root@worker1 ~]# du -sh /tmp/
20K    /tmp/

 

 

3 磁盘分区

  • 在虚拟机关机状态,增加一个硬盘

3.1虚拟机->设置

3.2选择硬盘,点击下一步

 

3.3保持默认即可

3.4选择第一个,点下一步

3.5硬盘大小设置,因为做实验,本人设置5GB,点击下一步

3.6保持默认,点击完成

3.7点击完成

3.8开始界面就会有两个硬盘了,然后开启虚拟机

  • fdisk是Linux下硬盘的分区工具,是一个非常实用的命令,但是fdisk只能划分小于2TB的分区
  • fdisk [-l ] [设备名称],其选项只有-l。选项-l后面不加设备名称,会直接列出系统中所有的磁盘设备以及分区表;加上设备名称,则会列出该设备的分区表查看系统的硬盘信息
[root@worker1 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000d76be

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4317183     1952768   82  Linux swap / Solaris
/dev/sda3         4317184    41943039    18812928   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
  • 由上信息,增加的硬盘是/dev/sdb/,fdisk /dev/sdb进行硬盘分区
[root@worker1 ~]# fdisk /dev/sdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x530aa6fb.

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

Command (m for help):

p:表示打印当前磁盘的分区情况

n:表示建立一个新的分区

w:表示保存

q:表示退出

d:表示删除一个分区

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x530aa6fb

   Device Boot      Start         End      Blocks   Id  System
  • 以上信息可以看到,硬盘还没有分区:
  • 现在进行,分4个主分区,前三个分区各为1GB,最后一个2G,步骤:
Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x530aa6fb

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):1    //因为这里保持默认值1,直接按回车了,这个分区号是可选的
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):2
First sector (2099200-10485759, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-10485759, default 10485759): +1G
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):3
First sector (4196352-10485759, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-10485759, default 10485759): +1G
Partition 3 of type Linux and of size 1 GiB is set

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
Selected partition 4
First sector (6293504-10485759, default 6293504):
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-10485759, default 10485759):
Using default value 10485759
Partition 4 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x530aa6fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200     4196351     1048576   83  Linux
/dev/sdb3         4196352     6293503     1048576   83  Linux
/dev/sdb4         6293504    10485759     2096128   83  Linux

Command (m for help): w //如果需要保存分区,输入w即可,保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • 查看刚才的分区状态
[root@worker1 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000d76be

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4317183     1952768   82  Linux swap / Solaris
/dev/sda3         4317184    41943039    18812928   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x530aa6fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200     4196351     1048576   83  Linux
/dev/sdb3         4196352     6293503     1048576   83  Linux
/dev/sdb4         6293504    10485759     2096128   83  Linux
[root@worker1 ~]#

 

  • 分区:主分区+扩展分区
  • 3个主分区,1个扩展分区,步骤:
  • 进入分区模式,输入d进行分区删除
[root@worker1 ~]# fdisk /dev/sdb
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): d
Partition number (1-4, default 4):4
Partition 4 is deleted

Command (m for help): d   
Partition number (1-3, default 3):3
Partition 3 is deleted

Command (m for help): d
Partition number (1,2, default 2):2  
Partition 2 is deleted

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x530aa6fb

   Device Boot      Start         End      Blocks   Id  System

Command (m for help):

删除分区之后,再重新创建分区:

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x530aa6fb

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n      
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):1
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +500M
Partition 1 of type Linux and of size 500 MiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):2
First sector (1026048-10485759, default 1026048):
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-10485759, default 10485759): +1G
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):3
First sector (3123200-10485759, default 3123200):
Using default value 3123200
Last sector, +sectors or +size{K,M,G} (3123200-10485759, default 10485759): +1G
Partition 3 of type Linux and of size 1 GiB is set

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e
Selected partition 4
First sector (5220352-10485759, default 5220352):
Using default value 5220352
Last sector, +sectors or +size{K,M,G} (5220352-10485759, default 10485759):
Using default value 10485759
Partition 4 of type Extended and of size 2.5 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (5222400-10485759, default 5222400):
Using default value 5222400
Last sector, +sectors or +size{K,M,G} (5222400-10485759, default 10485759): +1G
Partition 5 of type Linux and of size 1 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (7321600-10485759, default 7321600):
Using default value 7321600
Last sector, +sectors or +size{K,M,G} (7321600-10485759, default 10485759): +1G
Partition 6 of type Linux and of size 1 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 7
First sector (9420800-10485759, default 9420800):
Using default value 9420800
Last sector, +sectors or +size{K,M,G} (9420800-10485759, default 10485759):
Using default value 10485759
Partition 7 of type Linux and of size 520 MiB is set

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x530aa6fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux
/dev/sdb2         1026048     3123199     1048576   83  Linux
/dev/sdb3         3123200     5220351     1048576   83  Linux
/dev/sdb4         5220352    10485759     2632704    5  Extended
/dev/sdb5         5222400     7319551     1048576   83  Linux
/dev/sdb6         7321600     9418751     1048576   83  Linux
/dev/sdb7         9420800    10485759      532480   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
  • 查看新增硬盘分区后信息
[root@worker1 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000d76be

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4317183     1952768   82  Linux swap / Solaris
/dev/sda3         4317184    41943039    18812928   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x530aa6fb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux
/dev/sdb2         1026048     3123199     1048576   83  Linux
/dev/sdb3         3123200     5220351     1048576   83  Linux
/dev/sdb4         5220352    10485759     2632704    5  Extended
/dev/sdb5         5222400     7319551     1048576   83  Linux
/dev/sdb6         7321600     9418751     1048576   83  Linux
/dev/sdb7         9420800    10485759      532480   83  Linux
[root@worker1 ~]#

扩展学习parted分区gpt格式
http://www.apelearn.com/bbs/thread-7243-1-1.html​​​​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值