liunx命令5 df、du、fdisk、磁盘管理相关命令

df查看命令

[root@wangchao ~]# df              //查看分区

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/sda3       18244476 5919104  11391948  35% /

tmpfs             515244      72    515172   1% /dev/shm

/dev/sda1         194241   26001    158000  15% /boot

[root@wangchao ~]# free            //查看swap使用情况

             total       used       free     shared    buffers     cached

Mem:       1030492     419328     611164       1348      66492     194216

-/+ buffers/cache:     158620     871872

Swap:      2097148          0    2097148

[root@wangchao ~]# df -h                     //已合适单位显示

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        18G  5.7G   11G  35% /

tmpfs           504M   72K  504M   1% /dev/shm

/dev/sda1       190M   26M  155M  15% /boot

[root@wangchao ~]# df -k -m                 //KM为单位显示

Filesystem     1M-blocks  Used Available Use% Mounted on

/dev/sda3          17817  5781     11125  35% /

tmpfs                504     1       504   1% /dev/shm

/dev/sda1            190    26       155  15% /boot

[root@wangchao ~]# df -i                    //查看inode使用情况

Filesystem      Inodes  IUsed   IFree IUse% Mounted on

/dev/sda3      1166880 160054 1006826   14% /

tmpfs           128811      4  128807    1% /dev/shm

/dev/sda1        51200     38   51162    1% /boot

 

 

 

du 命令

[root@wangchao ~]# du /etc/                 //查看文件,大小使用情况

[root@wangchao ~]#  du -h /etc/             //已合适大小显示etc使用情况

[root@wangchao ~]# du -sh /etc/             //查看/etc/大小使用情况

40M     /etc/

 

[root@wangchao ~]# ls -lh /etc/init.d/iptables

-rwxr-xr-x. 1 root root 11K Oct 15  2014 /etc/init.d/iptables

[root@wangchao ~]# du -sh /etc/init.d/iptables

12K     /etc/init.d/iptables

[root@wangchao ~]# ls -lb /etc/init.d/iptables                          //b为单位显示

-rwxr-xr-x. 1 root root 10688 Oct 15  2014 /etc/init.d/iptables

[root@wangchao ~]# du -sb /etc/init.d/iptables

10688   /etc/init.d/iptables

查看文件大小的两个命令,两者大小显示不一样

[root@wangchao ~]# touch 12

[root@wangchao ~]# echo "12">12

[root@wangchao ~]# ls -lh 12                       //查看新建文件大小为3b

-rw-r--r--. 1 root root 3 Jul  8 20:24 12

[root@wangchao ~]# ls -lb 12

-rw-r--r--. 1 root root 3 Jul  8 20:24 12

[root@wangchao ~]# du -sh 12                    //查看该文件大小为4k

4.0K    12

//为什么呢:du命令,查看文件占用磁盘空间大小,(理解为一个磁盘分成一块一块,最小一块为4k,)。ls命令,查看文件本身实际的大小。

 

fdisk命令

[root@wangchao ~]# fdisk -l                     //查看磁盘,分区使用情况

 

Disk /dev/sda: 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: 0x00052035

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              26         287     2097152   82  Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3             287        2611    18668544   83  Linux

 

[root@wangchao ~]# init 0             //关闭虚拟机加一块硬盘(SCSIS

[root@wangchao ~]# fdisk -l            //查看添加的一块硬盘

 

Disk /dev/sda: 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: 0x00052035

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              26         287     2097152   82  Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3             287        2611    18668544   83  Linux

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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

 

[root@wangchao ~]# fdisk -l /dev/sda            //查看一块指定的磁盘

 

Disk /dev/sda: 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: 0x00052035

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              26         287     2097152   82  Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3             287        2611    18668544   83  Linux

[root@wangchao ~]# fdisk -l /dev/sdb

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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

 

[root@wangchao ~]# fdisk /dev/sdb                     //fdisk新磁盘

 

Command (m for help): p                             //打印当前使用命令

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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: 0xcacffefa

 

   Device Boot      Start         End      Blocks   Id  System

 

Command (m for help): n                       //n新建分区

Command action

   e   extended

   p   primary partition (1-4)

p                                          //p主分区,e扩展分区

Partition number (1-4): 1                      

First cylinder (1-1305, default 1): 1

Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +2G            //2G大小

 

Command (m for help): p                      //打印

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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: 0xcacffefa

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         262     2104483+  83  Linux

 

Command (m for help): n                          //n新建分区

Command action

   e   extended

   p   primary partition (1-4)

e                                             //e扩展分区

Partition number (1-4): 2

First cylinder (263-1305, default 263): 263

Last cylinder, +cylinders or +size{K,M,G} (263-1305, default 1305): +5G

 

Command (m for help): p

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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: 0xcacffefa

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         262     2104483+  83  Linux

/dev/sdb2             263         916     5253255    5  Extended

 

Command (m for help): l                  //查看分区id

 

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris

 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-

 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-

 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-

 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx

 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data

 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .

 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility

 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt

 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access

 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O

 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor

 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs

 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT

 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/

10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b

11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor

12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor

14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary

16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS

17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE

18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto

1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep

1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT

1e  Hidden W95 FAT1

 

Command (m for help): p                     //看分区表

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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: 0xcacffefa

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         262     2104483+  83  Linux

/dev/sdb2             263         916     5253255    5  Extended

 

Command (m for help): n                       //出现l选项,l扩展分区,p主分区

Command action

   l   logical (5 or over)

   p   primary partition (1-4)

l                                                 //选扩展分区

First cylinder (263-916, default 263): p

First cylinder (263-916, default 263): l

First cylinder (263-916, default 263): 263

Last cylinder, +cylinders or +size{K,M,G} (263-916, default 916): +2G

 

Command (m for help): p

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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: 0xcacffefa

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         262     2104483+  83  Linux

/dev/sdb2             263         916     5253255    5  Extended

/dev/sdb5             263         524     2104483+  83  Linux

 

Command (m for help): w                                //w保存

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

[root@wangchao ~]# fdisk -l /dev/sdb                     //查看分区使用情况

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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: 0xcacffefa

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         262     2104483+  83  Linux

/dev/sdb2             263         916     5253255    5  Extended

/dev/sdb5             263         524     2104483+  83  Linux

[root@wangchao ~]#

 

 

 

磁盘格mke2fs

磁盘分区后,不能马上用,还需格式化成指定格式。

[root@wangchao ~]# cat /etc/filesystems              //查看磁盘有哪些格式

ext4

ext3

ext2

nodev proc

nodev devpts

iso9660

vfat

hfs

hfsplus

 

[root@wangchao ~]# mkfs.ext4 /dev/sdb5            ///dev/sdb5分区格式化成ext4格式

mke2fs 1.41.12 (17-May-2010)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

131648 inodes, 526120 blocks

26306 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=541065216

17 block groups

32768 blocks per group, 32768 fragments per group

7744 inodes per group

Superblock backups stored on blocks:

        32768, 98304, 163840, 229376, 294912

 

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 39 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

[root@wangchao ~]#  mke2fs -t ext4 -b 1024 -m 1 -L  wangchao  /dev/sdb1

///dev/sdb1格式化成ext4格式,-t指定格式,-b指定块大小为1024b.-m保留1%-L磁盘标签

mke2fs 1.41.12 (17-May-2010)

Filesystem label=wangchao

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

Stride=0 blocks, Stripe width=0 blocks

131584 inodes, 2104480 blocks

21044 blocks (1.00%) reserved for the super user

First data block=1

Maximum filesystem blocks=69468160

257 block groups

8192 blocks per group, 8192 fragments per group

512 inodes per group

Superblock backups stored on blocks:

        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553,

        1024001, 1990657

 

Writing inode tables: done

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 26 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

//mkfs.ext4mkfs.ext3mke2fs命令相似。mke2fs多一些功能

 

 

  

磁盘挂载与卸载

磁盘格式化后,要想写入数据还需挂载上。

root@wangchao ~]# fdisk -l /dev/sdb

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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: 0xcacffefa

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         262     2104483+  83  Linux

/dev/sdb2             263         916     5253255    5  Extended

/dev/sdb5             263         524     2104483+  83  Linux

 

[root@wangchao ~]# mount /dev/sdb5 /mnt/               ///dev/sdb5 挂载在/mnt/

[root@wangchao ~]# df -h                         //查看已将/dev/sdb5 挂载在/mnt/

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        18G  5.7G   11G  35% /

tmpfs           504M   72K  504M   1% /dev/shm

/dev/sda1       190M   26M  155M  15% /boot

/dev/sdb5       2.0G  3.1M  1.9G   1% /mnt

[root@wangchao ~]# df -T                             //查看磁盘分区类型

Filesystem     Type  1K-blocks    Used Available Use% Mounted on

/dev/sda3      ext4   18244476 5919796  11391256  35% /

tmpfs          tmpfs    515244      72    515172   1% /dev/shm

/dev/sda1      ext4     194241   26001    158000  15% /boot

/dev/sdb5      ext4    2005848    3096   1897528   1% /mnt

 

[root@wangchao ~]# mkdir /home/wangchao/123

[root@wangchao ~]# mount /dev/sdb1 /home/            //挂载后home下文件被覆盖

[root@wangchao ~]# umount /home/                   //卸载

 

[root@wangchao ~]# mount LABEL=wangchao /home/wangchao/123/        //使用标签挂载

[root@wangchao ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        18G  5.7G   11G  35% /

tmpfs           504M   72K  504M   1% /dev/shm

/dev/sda1       190M   26M  155M  15% /boot

/dev/sdb5       2.0G  3.1M  1.9G   1% /mnt

/dev/sdb1       2.0G  3.1M  2.0G   1% /home/wangchao/123

 

 

[root@wangchao ~]# cd /mnt/

[root@wangchao mnt]# ls

lost+found

[root@wangchao mnt]# touch 123

[root@wangchao mnt]# umount /mnt/                         //在目录里卸载回失败

umount: /mnt: device is busy.

        (In some cases useful info about processes that use

         the device is found by lsof(8) or fuser(1))

[root@wangchao mnt]# cd                                 //先退出才能卸载

 

[root@wangchao 123]#  umount /dev/sdb1

umount: /home/wangchao/123: device is busy.

        (In some cases useful info about processes that use

         the device is found by lsof(8) or fuser(1))

[root@wangchao 123]#  umount -l /dev/sdb1          //加参数-l强制卸载,不退出卸载

 

 

 

 

分区表fstab

以上挂载为手动方式,重启后将失效,自动开机挂载方法如下:

[root@wangchao 123]# cat /etc/fstab                  //分区配置文件

 

#

# /etc/fstab

# Created by anaconda on Mon Jun  8 19:14:01 2015

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=41e6be85-0f8b-41ec-9559-f48a356d54f0 /                       ext4    defaults        1 1

UUID=ca50ee18-a125-4629-8c11-1b6134912239 /boot                   ext4    defaults        1 2

UUID=e7c33c68-670b-4a2a-b1aa-4ceacf4f0e86 swap                    swap    defaults        0 0

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults        0 0

proc                    /proc                   proc    defaults        0 0

 

[root@wangchao 123]# ls /etc/rc.local

/etc/rc.local       

//也可将mount写入,开机自动执行。该文件为开机就运行其中的命名

 

[root@wangchao 123]# cat /etc/fstab

 

#

# /etc/fstab

# Created by anaconda on Mon Jun  8 19:14:01 2015

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=41e6be85-0f8b-41ec-9559-f48a356d54f0 /                       ext4    defaults        1 1

UUID=ca50ee18-a125-4629-8c11-1b6134912239 /boot                   ext4    defaults        1 2

UUID=e7c33c68-670b-4a2a-b1aa-4ceacf4f0e86 swap                    swap    defaults        0 0

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults        0 0

proc                    /proc                   proc    defaults        0 0

 

//其中UUID为每个硬盘的标识,每个硬盘唯一。(不止硬盘设备,其他如网卡都有UUID

 

[root@wangchao 123]# blkid                    //查看硬盘的UUID

/dev/sda1: UUID="ca50ee18-a125-4629-8c11-1b6134912239" TYPE="ext4"

/dev/sda2: UUID="e7c33c68-670b-4a2a-b1aa-4ceacf4f0e86" TYPE="swap"

/dev/sda3: UUID="41e6be85-0f8b-41ec-9559-f48a356d54f0" TYPE="ext4"

/dev/sdb1: LABEL="wangchao" UUID="2be2198d-dcb8-4589-8a34-8b3d472cbece" TYPE="ext4"

/dev/sdb5: UUID="87dba34e-66d1-47b6-a990-9c285b4958db" TYPE="ext4"

 

[root@wangchao 123]# mount UUID="87dba34e-66d1-47b6-a990-9c285b4958db" /mnt/

//UUID="87dba34e-66d1-47b6-a990-9c285b4958db" 的硬盘挂载到/mnt/

 

[root@wangchao 123]# df -h                     //查看挂载

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        18G  5.7G   11G  35% /

tmpfs           504M   72K  504M   1% /dev/shm

/dev/sda1       190M   26M  155M  15% /boot

/dev/sdb5       2.0G  3.1M  1.9G   1% /mnt

 

 

 

[root@wangchao 123]# vi /etc/fstab

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults        0 0

proc                    /proc                   proc    defaults        0 0

/dev/sdb5               /mnt              ext4       defaults          0 0

 

///dev/sdb5 磁盘 /mnt 挂载点 ext4 格式  defaults  0(是否备份,否) 0开机自检,否

 

写入文件后生效,下次重启还是有效

 

[root@wangchao 123]# cd

[root@wangchao ~]# umount /mnt/

umount: /mnt/: not mounted

[root@wangchao ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        18G  5.7G   11G  35% /

tmpfs           504M   72K  504M   1% /dev/shm

[root@wangchao ~]# mount -a                      //自动挂载,是写入文件的自动挂载

[root@wangchao ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        18G  5.7G   11G  35% /

tmpfs           504M   72K  504M   1% /dev/shm

/dev/sda1       190M   26M  155M  15% /boot

/dev/sdb5       2.0G  3.1M  1.9G   1% /mnt

 

 

[root@wangchao ~]# vi /etc/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

 

touch /var/lock/subsys/local

mount /dev/sdb1 /home/wangchao/123

 

//下次重启自动执行mount /dev/sdb1 /home/wangchao/123命令

[root@wangchao ~]# mount                         //查看磁盘挂载加载了哪些选项

/dev/sda3 on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")

/dev/sda1 on /boot type ext4 (rw)

/dev/sdb5 on /mnt type ext4 (rw)

你可能感兴趣的:(fdisk,du,df,liunx命令5,磁盘管理相关命令)