df[1](disk free)命令用于报告文件系统的磁盘空间使用情况,包括已用空间和可用空间。
基本语法:
df [选项]... [文件]...
可以直接输入命令,无参数使用。
$ dfFilesystem 1K-blocks Used Available Use% Mounted onoverlay 61202244 6409624 51651296 12% /tmpfs 65536 0 65536 0% /devshm 65536 0 65536 0% /dev/shm/dev/vda1 61202244 6409624 51651296 12% /etc/hoststmpfs 2006452 0 2006452 0% /proc/scsitmpfs 2006452 0 2006452 0% /sys/firmware
也可以指定文件或目录。
$ df /homeFilesystem 1K-blocks Used Available Use% Mounted onoverlay 61202244 6409624 51651296 12% /$ df /dev/shmFilesystem 1K-blocks Used Available Use% Mounted onshm 65536 0 65536 0% /dev/shm
- 如果参数是已挂载文件系统的设备文件,显示该
文件系统的空间信息
1. 显示所有文件系统
df -adf --all
$ df --allFilesystem 1K-blocks Used Available Use% Mounted onoverlay 61202244 6409624 51651296 12% /proc 0 0 0 - /proctmpfs 65536 0 65536 0% /devdevpts 0 0 0 - /dev/ptssysfs 0 0 0 - /syscgroup 0 0 0 - /sys/fs/cgroupmqueue 0 0 0 - /dev/mqueueshm 65536 0 65536 0% /dev/shm/dev/vda1 61202244 6409624 51651296 12% /etc/resolv.conf/dev/vda1 61202244 6409624 51651296 12% /etc/hostname/dev/vda1 61202244 6409624 51651296 12% /etc/hostsdevpts 0 0 0 - /dev/consoleproc 0 0 0 - /proc/busproc 0 0 0 - /proc/fsproc 0 0 0 - /proc/irqproc 0 0 0 - /proc/sysproc 0 0 0 - /proc/sysrq-triggertmpfs 65536 0 65536 0% /proc/interruptstmpfs 65536 0 65536 0% /proc/kcoretmpfs 65536 0 65536 0% /proc/keystmpfs 65536 0 65536 0% /proc/timer_listtmpfs 2006452 0 2006452 0% /proc/scsitmpfs 2006452 0 2006452 0% /sys/firmware
2. 人性化显示
df -hdf --human-readable
$ df -hFilesystem Size Used Avail Use% Mounted onoverlay 59G 6.2G 50G 12% /tmpfs 64M 0 64M 0% /devshm 64M 0 64M 0% /dev/shm/dev/vda1 59G 6.2G 50G 12% /etc/hoststmpfs 2.0G 0 2.0G 0% /proc/scsitmpfs 2.0G 0 2.0G 0% /sys/firmware
df -Hdf --si
$ df -HFilesystem Size Used Avail Use% Mounted onoverlay 63G 6.6G 53G 12% /tmpfs 68M 0 68M 0% /devshm 68M 0 68M 0% /dev/shm/dev/vda1 63G 6.6G 53G 12% /etc/hoststmpfs 2.1G 0 2.1G 0% /proc/scsitmpfs 2.1G 0 2.1G 0% /sys/firmware
3. 指定块大小
df -B 1Gdf --block-size=1G
$ df -B 1GFilesystem 1G-blocks Used Available Use% Mounted onoverlay 59 7 50 12% /tmpfs 1 0 1 0% /devshm 1 0 1 0% /dev/shm/dev/vda1 59 7 50 12% /etc/hoststmpfs 2 0 2 0% /proc/scsitmpfs 2 0 2 0% /sys/firmware
df -k
$ df -kFilesystem 1K-blocks Used Available Use% Mounted onoverlay 61202244 6409624 51651296 12% /tmpfs 65536 0 65536 0% /devshm 65536 0 65536 0% /dev/shm/dev/vda1 61202244 6409624 51651296 12% /etc/hoststmpfs 2006452 0 2006452 0% /proc/scsitmpfs 2006452 0 2006452 0% /sys/firmware
4. 显示 inode 信息
df -idf --inodes
inode 包含文件的元数据(所有者、权限、时间戳等)
$ df -iFilesystem Inodes IUsed IFree IUse% Mounted onoverlay 3907584 225278 3682306 6% /tmpfs 501613 17 501596 1% /devshm 501613 1 501612 1% /dev/shm/dev/vda1 3907584 225278 3682306 6% /etc/hoststmpfs 501613 1 501612 1% /proc/scsitmpfs 501613 1 501612 1% /sys/firmware
5. 限制显示范围
df -ldf --local
$ df -lFilesystem 1K-blocks Used Available Use% Mounted onoverlay 61202244 6409624 51651296 12% /tmpfs 65536 0 65536 0% /devshm 65536 0 65536 0% /dev/shm/dev/vda1 61202244 6409624 51651296 12% /etc/hoststmpfs 2006452 0 2006452 0% /proc/scsitmpfs 2006452 0 2006452 0% /sys/firmware
df -t ext4df --type=ext4
df -t ext4 -t xfs
$ df -t ext4Filesystem 1K-blocks Used Available Use% Mounted on/dev/vda1 61202244 6409624 51651296 12% /etc/hosts
df -x nfsdf --exclude-type=nfs
$ df -x ext4Filesystem 1K-blocks Used Available Use% Mounted onoverlay 61202244 6409624 51651296 12% /tmpfs 65536 0 65536 0% /devshm 65536 0 65536 0% /dev/shmtmpfs 2006452 0 2006452 0% /proc/scsitmpfs 2006452 0 2006452 0% /sys/firmware
6. 自定义输出格式
df --output=target,size,used,avail,pcent
示例:
$ df --output=target,size,used,avail,pcentMounted on 1K-blocks Used Avail Use%/ 61202244 6409624 51651296 12%/dev 65536 0 65536 0%/dev/shm 65536 0 65536 0%/etc/hosts 61202244 6409624 51651296 12%/proc/scsi 2006452 0 2006452 0%/sys/firmware 2006452 0 2006452 0%
# 显示所有字段$ df --outputFilesystem Type Inodes IUsed IFree IUse% 1K-blocks Used Avail Use% File Mounted onoverlay overlay 3907584 225278 3682306 6% 61202244 6409624 51651296 12% - /tmpfs tmpfs 501613 17 501596 1% 65536 0 65536 0% - /devshm tmpfs 501613 1 501612 1% 65536 0 65536 0% - /dev/shm/dev/vda1 ext4 3907584 225278 3682306 6% 61202244 6409624 51651296 12% - /etc/hoststmpfs tmpfs 501613 1 501612 1% 2006452 0 2006452 0% - /proc/scsitmpfs tmpfs 501613 1 501612 1% 2006452 0 2006452 0% - /sys/firmware
7. POSIX 兼容模式
df -Pdf --portability
- 不受环境变量影响(除非设置了 POSIXLY_CORRECT)
$ df --portabilityFilesystem 1024-blocks Used Available Capacity Mounted onoverlay 61202244 6409624 51651296 12% /tmpfs 65536 0 65536 0% /devshm 65536 0 65536 0% /dev/shm/dev/vda1 61202244 6409624 51651296 12% /etc/hoststmpfs 2006452 0 2006452 0% /proc/scsitmpfs 2006452 0 2006452 0% /sys/firmware
8. 显示文件系统类型
df -Tdf --print-type
- 常见类型:ext4、xfs、btrfs、nfs、ntfs、vfat 等
$ df -TFilesystem Type 1K-blocks Used Available Use% Mounted onoverlay overlay 61202244 6409624 51651296 12% /tmpfs tmpfs 65536 0 65536 0% /devshm tmpfs 65536 0 65536 0% /dev/shm/dev/vda1 ext4 61202244 6409624 51651296 12% /etc/hoststmpfs tmpfs 2006452 0 2006452 0% /proc/scsitmpfs tmpfs 2006452 0 2006452 0% /sys/firmware
9. 同步数据
df --sync
$ df --syncFilesystem 1K-blocks Used Available Use% Mounted onoverlay 61202244 6409624 51651296 12% /tmpfs 65536 0 65536 0% /devshm 65536 0 65536 0% /dev/shm/dev/vda1 61202244 6409624 51651296 12% /etc/hoststmpfs 2006452 0 2006452 0% /proc/scsitmpfs 2006452 0 2006452 0% /sys/firmware
df --no-sync
10. 显示总计
df --total
$ df --totalFilesystem 1K-blocks Used Available Use% Mounted onoverlay 61202244 6409624 51651296 12% /tmpfs 65536 0 65536 0% /devshm 65536 0 65536 0% /dev/shm/dev/vda1 61202244 6409624 51651296 12% /etc/hoststmpfs 2006452 0 2006452 0% /proc/scsitmpfs 2006452 0 2006452 0% /sys/firmwaretotal 126548464 12819248 107446568 11% -
最佳实践
- 脚本中使用时:使用
--output确保输出格式稳定
尤其是排查磁盘空间满时的问题,除了容量外,还要查看 inode。当然 inode 的规划要根据服务器的用途,在格式化磁盘时就要做好。
参考资料
[1] df: https://man.archlinux.org/man/df.1