df 命令

On the internet you will find plenty of tools for checking disk space utilization in Linux. However, Linux has a strong built in utility called ‘df‘. The ‘df‘ command stand for “disk filesystem“, it is used to get full summary of available and used disk space usage of file system on Linux system.

Using ‘-h‘ parameter with (df -h) will shows the file system disk space statistics in “human readable” format, means it gives the details in bytes, mega bytes and gigabyte.

Useful df Command Examples

This article explain a way to get the full information of Linux disk space usage with the help of ‘df‘ command with their practical examples. So, you could better understand the usage of df command in Linux.

1. Check File System Disk Space Usage

The “df” command displays the information of device name, total blocks, total disk space, used disk space, available disk space and mount points on a file system.

[root@tecmint ~]# df

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/cciss/c0d0p2     78361192  23185840  51130588  32% /
/dev/cciss/c0d0p5     24797380  22273432   1243972  95% /home
/dev/cciss/c0d0p3     29753588  25503792   2713984  91% /data
/dev/cciss/c0d0p1       295561     21531    258770   8% /boot
tmpfs                   257476         0    257476   0% /dev/shm

2. Display Information of all File System Disk Space Usage

The same as above, but it also displays information of dummy file systems along with all the file system disk usage and their memory utilization.

[root@tecmint ~]# df -a

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/cciss/c0d0p2     78361192  23186116  51130312  32% /
proc                         0         0         0   -  /proc
sysfs                        0         0         0   -  /sys
devpts                       0         0         0   -  /dev/pts
/dev/cciss/c0d0p5     24797380  22273432   1243972  95% /home
/dev/cciss/c0d0p3     29753588  25503792   2713984  91% /data
/dev/cciss/c0d0p1       295561     21531    258770   8% /boot
tmpfs                   257476         0    257476   0% /dev/shm
none                         0         0         0   -  /proc/sys/fs/binfmt_misc
sunrpc                       0         0         0   -  /var/lib/nfs/rpc_pipefs

3. Show Disk Space Usage in Human Readable Format

Have you noticed that above commands displays information in bytes, which is not readable yet all, because we are in a habit of reading the sizes in megabytes, gigabytes etc. as it makes very easy to understand and remember.

The df command provides an option to display sizes in Human Readable formats by using ‘-h’ (prints the results in human readable format (e.g., 1K 2M 3G)).

[root@tecmint ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/cciss/c0d0p2      75G   23G   49G  32% /
/dev/cciss/c0d0p5      24G   22G  1.2G  95% /home
/dev/cciss/c0d0p3      29G   25G  2.6G  91% /data
/dev/cciss/c0d0p1     289M   22M  253M   8% /boot
tmpfs                 252M     0  252M   0% /dev/shm

4. Display Information of /home File System

To see the information of only device /home file system in human readable format use the following command.

[root@tecmint ~]# df -hT /home

Filesystem		Type    Size  Used Avail Use% Mounted on
/dev/cciss/c0d0p5	ext3     24G   22G  1.2G  95% /home

5. Display Information of File System in Bytes

To display all file system information and usage in 1024-byte blocks, use the option ‘-k‘ (e.g. –block-size=1K) as follows.

[root@tecmint ~]# df -k

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/cciss/c0d0p2     78361192  23187212  51129216  32% /
/dev/cciss/c0d0p5     24797380  22273432   1243972  95% /home
/dev/cciss/c0d0p3     29753588  25503792   2713984  91% /data
/dev/cciss/c0d0p1       295561     21531    258770   8% /boot
tmpfs                   257476         0    257476   0% /dev/shm

6. Display Information of File System in MB

To display information of all file system usage in MB (Mega Byte) use the option as ‘-m‘.

[root@tecmint ~]# df -m

Filesystem           1M-blocks      Used Available Use% Mounted on
/dev/cciss/c0d0p2        76525     22644     49931  32% /
/dev/cciss/c0d0p5        24217     21752      1215  95% /home
/dev/cciss/c0d0p3        29057     24907      2651  91% /data
/dev/cciss/c0d0p1          289        22       253   8% /boot
tmpfs                      252         0       252   0% /dev/shm

7. Display Information of File System in GB

To display information of all file system statistics in GB (Gigabyte) use the option as ‘df -h‘.

[root@tecmint ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/cciss/c0d0p2      75G   23G   49G  32% /
/dev/cciss/c0d0p5      24G   22G  1.2G  95% /home
/dev/cciss/c0d0p3      29G   25G  2.6G  91% /data
/dev/cciss/c0d0p1     289M   22M  253M   8% /boot
tmpfs                 252M     0  252M   0% /dev/shm

8. Display File System Inodes

Using ‘-i‘ switch will display the information of number of used inodes and their percentage for the file system.

[root@tecmint ~]# df -i

Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/cciss/c0d0p2    20230848  133143 20097705    1% /
/dev/cciss/c0d0p5    6403712  798613 5605099   13% /home
/dev/cciss/c0d0p3    7685440 1388241 6297199   19% /data
/dev/cciss/c0d0p1      76304      40   76264    1% /boot
tmpfs                  64369       1   64368    1% /dev/shm

9. Display File System Type

If you notice all the above commands output, you will see there is no file system type mentioned in the results. To check the file system type of your system use the option ‘T‘. It will display file system type along with other information.

[root@tecmint ~]# df -T

Filesystem		Type   1K-blocks  Used      Available Use% Mounted on
/dev/cciss/c0d0p2	ext3    78361192  23188812  51127616  32%   /
/dev/cciss/c0d0p5	ext3    24797380  22273432  1243972   95%   /home
/dev/cciss/c0d0p3	ext3    29753588  25503792  2713984   91%   /data
/dev/cciss/c0d0p1	ext3    295561     21531    258770    8%    /boot
tmpfs			tmpfs   257476         0    257476    0%   /dev/shm

10. Include Certain File System Type

If you want to display certain file system type use the ‘-t‘ option. For example, the following command will only display ext3 file system.

[root@tecmint ~]# df -t ext3

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/cciss/c0d0p2     78361192  23190072  51126356  32% /
/dev/cciss/c0d0p5     24797380  22273432   1243972  95% /home
/dev/cciss/c0d0p3     29753588  25503792   2713984  91% /data
/dev/cciss/c0d0p1       295561     21531    258770   8% /boot

11. Exclude Certain File System Type

If you want to display file system type that doesn’t belongs to ext3 type use the option as ‘-x‘. For example, the following command will only display other file systems types other than ext3.

[root@tecmint ~]# df -x ext3

Filesystem           1K-blocks      Used Available Use% Mounted on
tmpfs                   257476         0    257476   0% /dev/shm

12. Display Information of df Command.

Using ‘–help‘ switch will display a list of available option that are used with df command.

[root@tecmint ~]# df --help

Usage: df [OPTION]... [FILE]...
Show information about the file system on which each FILE resides,
or all file systems by default.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all             include dummy file systems
  -B, --block-size=SIZE use SIZE-byte blocks
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
  -H, --si              likewise, but use powers of 1000 not 1024
  -i, --inodes          list inode information instead of block usage
  -k                    like --block-size=1K
  -l, --local           limit listing to local file systems
      --no-sync         do not invoke sync before getting usage info (default)
  -P, --portability     use the POSIX output format
      --sync            invoke sync before getting usage info
  -t, --type=TYPE       limit listing to file systems of type TYPE
  -T, --print-type      print file system type
  -x, --exclude-type=TYPE   limit listing to file systems not of type TYPE
  -v                    (ignored)
      --help     display this help and exit
      --version  output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Report bugs to <bug-coreutils@gnu.org>.

磁盘是Linux系统中一项非常重要的资源,如何对其进行有效的管理直接关系到整个系统的性能问题。在常用的命令当中,df用于检查文件系统磁盘占用情况,du检查磁盘空间占用情况,而fdisk用于磁盘分区,是作者比较常用的三个命令,下面分享一些这方面的使用心得。

AD:51CTO 网+首届APP创新评选大赛火热启动——超百万资源等你拿!

【51CTO独家特稿】磁盘是Linux系统中一项非常重要的资源,如何对其进行有效的管理直接关系到整个系统的性能问题。对Linux磁盘管理稍微有一些学习和经验的朋友们应该都知道df、du和fdisk这三个常用命令:df用于检查文件系统磁盘占用情况,du检查磁盘空间占用情况,而fdisk用于磁盘分区。这三个工具是本人在进行Linux磁盘管理时常用的工具,下面跟大家分享一些使用心得。

作者介绍:李洋(博客),博士毕业于中科院计算所。10多年来一直从事计算机网络信息安全研发工作,曾主持和参与多项国家重点项目以及信息安全系统和企业信息安全系统的研发工作。具有Linux系统应用、管理、安全及内核的研发经验,擅长网络安全技术、协议分析、Linux系统安全技术、Linux系统及网络管理、Linux内核开发等。

1.df

df命令可以获取硬盘被占用了多少空间,目前还剩下多少空间等信息,它也可以显示所有文件系统对i节点和磁盘块的使用情况。

df命令各个选项的含义如下:

  -a:显示所有文件系统的磁盘使用情况,包括0块(block)的文件系统,如/proc文件系统。
  -k:以k字节为单位显示。
  -i:显示i节点信息,而不是磁盘块。
  -t:显示各指定类型的文件系统的磁盘空间使用情况。
  -x:列出不是某一指定类型文件系统的磁盘空间使用情况(与t选项相反)。
  -T:显示文件系统类型。

我们先看看使用df命令的例子:

//列出各文件系统的磁盘空间使用情况
#df
Filesystem           1k-blocks      Used   Available Use% Mounted on
/dev/hda5               381139     332921     28540  93% /
/dev/hda1                46636      6871     37357  16% /boot
/dev/hda3             10041144   6632528   2898556  70% /home
none                    127372         0    127372   0% /dev/shm
/dev/hda2             27474876  24130460   1948772  93% /usr
/dev/hda6               256667    232729     10686  96% /var

第1列是代表文件系统对应的设备文件的路径名(一般是硬盘上的分区);第2列给出分区包含的数据块(1024字节)的数目;第3,4列分别表示已用的和可用的数据块数目。

◆用户也许会感到奇怪,第3,4列块数之和不等于第2列中的块数。这是因为默认的每个分区都留了少量空间供系统管理员使用的缘故。即使遇到普通用户空间已满的情况,管理员仍能登录和留有解决问题所需的工作空间。清单中Use%列表示普通用户空间使用的百分比,若这一数字达到100%,分区仍然留有系统管理员使用的空间。

最后,Mounted on列表示文件系统的安装点。

//列出各文件系统的i节点使用情况。
#df -ia
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/hda5              98392   23919   74473   25% /
none                       0       0       0    -  /proc
/dev/hda1              12048      38   12010    1% /boot
none                       0       0       0    -  /dev/pts
/dev/hda3            1275456  355008  920448   28% /home
none                   31843       1   31842    1% /dev/shm
/dev/hda2            3489792  133637 3356155    4% /usr
/dev/hda6              66264    9876   56388   15% /var
 
//列出文件系统的类型。
#df -T
Filesystem    Type   1k-blocks      Used Available Use% Mounted on
/dev/hda5     ext3      381139    332921     28540  93% /
/dev/hda1     ext3       46636      6871     37357  16% /boot
/dev/hda3     ext3    10041144   6632528   2898556  70% /home
none         tmpfs      127372         0    127372   0% /dev/shm
/dev/hda2     ext3    27474876  24130460   1948772  93% /usr
/dev/hda6     ext3      256667    232729     10686  96% /var2

2. du

du的英文原义为“disk usage”,含义为显示磁盘空间的使用情况,统计目录(或文件)所占磁盘空间的大小。该命令的功能是逐级进入指定目录的每一个子目录并显示该目录占用文件系统数据块(1024字节)的情况。若没有给出指定目录,则对当前目录进行统计。

df命令的各个选项含义如下:

  -s:对每个Names参数只给出占用的数据块总数。
  -a:递归地显示指定目录中各文件及子目录中各文件占用的数据块数。若既不指定-s,也不指定-a,则只显示Names中的每一个目录及其中的各子目录所占的磁盘块数。
  -b:以字节为单位列出磁盘空间使用情况(系统默认以k字节为单位)。
  -k:以1024字节为单位列出磁盘空间使用情况。
  -c:最后再加上一个总计(系统默认设置)。
  -l:计算所有的文件大小,对硬链接文件,则计算多次。
  -x:跳过在不同文件系统上的目录不予统计。

下面举例说明du命令的使用:

//查看/mnt目录占用磁盘空间的情况
#du –abk /mnt
1       /mnt/cdrom
1       /mnt/floppy
3       /mnt
 
//列出各目录所占的磁盘空间,但不详细列出每个文件所占的空间
#du
3684    ./log
84      ./libnids-1.17/doc
720     ./libnids-1.17/src
32      ./libnids-1.17/samples
1064    ./libnids-1.17
4944    .

输出清单中的第1列是以块为单位计的磁盘空间容量,第2列列出目录中使用这些空间的目录名称。

◆这可能是一个很长的清单,有时只需要一个总数。这时可在du命令中加-s选项来取得总数:

#du –s /mnt 
3       /mnt
 
//列出所有文件和目录所占的空间(使用a选项),并以字节为单位(使用b选项)来计算大小
#du –ab /root/mail
6144    mail/sent-mail
1024    mail/saved-messages
8192    mail

3、fdisk

fdisk可以划分磁盘分区。下面给出使用Fdisk命令进行磁盘分区的操作步骤:

#fdisk /dev/had    //使用/dev/had作为默认的分区设备
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
   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)

用户通过提示键入“m”,可以显示Fdisk命令各个参数的说明。

Fdisk有很多参数,可是经常使用的只有几个。

◆在Linux分区过程,一般是先通过p参数来显示硬盘分区表信息,然后根据信息确定将来的分区。如下所示:

Disk /dev/sda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot    Start       End    Blocks   Id  System
/dev/hda1   *        41       522   3871665   83  Linux
/dev/hda2             1        40    321268+  82  Linux swap
 
Partition table entries are not in disk order
Command (m for help):

◆如果想完全改变硬盘的分区格式,就可以通过d参数一个一个地删除存在的硬盘分区。删除完毕,就可以通过n参数来增加新的分区。当按下“n”后,可以看到如下所示:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
   p
   Partiton number(1-4):1
   First cylinder(1-1023):1
   Last cylinder or + size or +sizeK or + sizeM(1-1023):+258M

这里要选择新建的分区类型,是主分区还是扩展分区;并选择p或是e。然后就是设置分区的大小。

◆要提醒注意的是,如果硬盘上有扩展分区,就只能增加逻辑分区,不能增加扩展分区。

◆在增加分区的时候,其类型都是默认的Linux Native,如果要把其中的某些分区改变为其他类型,例如Linux Swap或FAT32等,可以通过命令t来改变,当按下“t”改变分区类型的时候,系统会提示要改变哪个分区、以及改变为什么类型(如果想知道系统所支持的分区类型,键入l),如下所示:

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap)

改变完了分区类型,就可以按下“w”,保存并退出。如果不想保存,那么可以选择“q”直接退出,如下所示:

Command (m for help):w

通过如上的操作,就可以按照需要成功地划分磁盘分区了。

原文地址:https://www.cnblogs.com/kex1n/p/5124639.html