• df和du的区别


    du和df的不同

    空间计算方式的不同

    总结一下,就是df 看一个挂载点的时候,就只看这个挂载点,如果这个挂载点下面又挂载了其它的分区,那df在计算空间的时候并不会算进去,而du -sh则会算进去;

    # 默认情况下,因为df关注的是全局文件系统,所以df看目录比du看要大一些,如下所示:
    [08:11root@c7 ~]# df -h /mnt
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root   50G  3.5G   47G   7% /
    [08:11root@c7 ~]# du -sh /mnt
    0	/mnt
    [08:11root@c7 ~]# cd /mnt ; ls
    [08:12root@c7 /mnt]# 
    [08:12root@c7 /mnt]# dd if=/dev/zero of=./5G.txt bs=1G count=5 
    5+0 records in
    5+0 records out
    5368709120 bytes (5.4 GB) copied, 23.1128 s, 232 MB/s
    [08:12root@c7 /mnt]# df -h /mnt
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root   50G  8.5G   42G  17% /
    [08:12root@c7 /mnt]# du -sh /mnt
    5.0G	/mnt
    
    
    # 向目标目录/mnt的sdb1目录把/dev/sdb1分区挂载上,由于sdb1是空的,所以空间也看不出来有多大变化,如下所示:
    [08:13root@c7 /mnt]# fdisk -l /dev/sdb1
    Disk /dev/sdb1: 21.5 GB, 21473787904 bytes, 41940992 sectors
    [08:13root@c7 /mnt]# mkdir /mnt/sdb1
    [08:14root@c7 /mnt]# mount /dev/sdb1 /mnt/sdb1
    [08:14root@c7 /mnt]# df -h /mnt
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root   50G  8.5G   42G  17% /
    [08:14root@c7 /mnt]# du -sh /mnt
    5.1G	/mnt
    
    # 但当我向/mnt/sdb1目录下写入一个2G的东西时,df -h还是没变,因为df -h 看/mnt时并没有把/mnt下的sdb1占用的空间给算上;
    # 而du -sh 看/mnt的时候,不仅/mnt本身占用的空间会算了,而且还会把/mnt下sdb1文件夹所占用的空间给算上;
    [08:14root@c7 /mnt]# cd /mnt/sdb1
    [08:15root@c7 /mnt/sdb1]# dd if=/dev/zero of=./2G.txt bs=1G count=2
    2+0 records in
    2+0 records out
    2147483648 bytes (2.1 GB) copied, 1.50843 s, 1.4 GB/s
    [08:15root@c7 /mnt/sdb1]# cd
    [08:15root@c7 ~]# df -h /mnt
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root   50G  8.5G   42G  17% /
    [08:15root@c7 ~]# du -sh /mnt
    7.1G	/mnt
    [08:15root@c7 ~]#
    

    空间释放

    # 关于du和df的空间释放,当一个文件被程序占用时,du会释放,它认为文件已经被删除,而df在文件系统角度来看则没有删除;
    [08:31root@c7 ~]# df -h /mnt
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root   50G  8.5G   42G  17% /
    [08:32root@c7 ~]# du -sh /mnt
    5.0G	/mnt
    [08:32root@c7 ~]# cd /mnt; ls
    5G.txt  sdb1
    [08:32root@c7 /mnt]# rm -rf 5G.txt 
    [08:32root@c7 /mnt]# cd
    [08:32root@c7 ~]# df -h /mnt
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root   50G  8.5G   42G  17% /
    [08:32root@c7 ~]# du -sh /mnt
    4.0K	/mnt
    
    # 找到该目录下已经被删除的文件记录,将占用进程干掉即可;
    [08:33root@c7 ~]# lsof | grep deleted
    vim       1340         root    3r      REG              253,0 5368709120    1281084 /mnt/5G.txt (deleted)
    [08:37root@c7 ~]# kill -9 1340 
    [08:38root@c7 ~]# lsof | grep deleted
    [08:38root@c7 ~]# df -h /mnt/
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root   50G  3.5G   47G   7% /
    [08:38root@c7 ~]# du -h /mnt
    0	/mnt/sdb1
    4.0K	/mnt
    
  • 相关阅读:
    Tomcat 调优的技巧
    双亲委派模型
    字典树实现
    Python获取房价信息和导出EXCEL
    日志检索关键字并截取上下行关联内容
    GC日志分析
    Linux 查看 删除进程
    Rotate partitions in DB2 on z
    C++ STL string
    DB2 for z: system catalog tables
  • 原文地址:https://www.cnblogs.com/yizhangheka/p/16274328.html
Copyright © 2020-2023  润新知