0

when i'm writing df -h in my instance i'm getting this data:

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        7.7G     0  7.7G   0% /dev
tmpfs           7.7G     0  7.7G   0% /dev/shm
tmpfs           7.7G  408K  7.7G   1% /run
tmpfs           7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/nvme0n1p1   32G   24G  8.5G  74% /
tmpfs           1.6G     0  1.6G   0% /run/user/1000

but when i'm clicking sudo du -sh / i'm getting:

11G /

So in df -h, / size is 24G but in du -sh same directory the size is 11G. I'm trying to get some free space on my instance and can't find the files that cause that. What i'm missing? did df -h is really giving fake data?

Bernana
  • 245
  • 1
  • 12

1 Answers1

3

This question comes up quite often. The file system allocates disk blocks in the file system to record its data. This data is referred to as metadata which is not visible to most user-level programs (such as du). Examples of metadata are inodes, disk maps, indirect blocks, and superblocks.

The du command is a user-level program that isn't aware of filesystem metadata, while df looks at the filesystem disk allocation maps and is aware of file system metadata. df obtains true filesystem statistics, whereas du sees only a partial picture.

There are many causes on why the disk space used or available when running the du or df commands differs.

Perhaps the most common is deleted files. Files that have been deleted may still be open by at least one process. The entry for such files is removed from the associated directory, which makes the file inaccessible. Therefore the command du which only counts files does not take these files into account and comes up with a smaller value. As long as a process still has the deleted file in use, however, the associated blocks are not yet released in the file system, so df which works at the kernel level correctly displays these as occupied. You can find out if this is the case by running the following:

lsof | grep '(deleted)'

The fix for this issue would be to restart the services that still have those deleted files open.

The second most common cause is if you have a partition or drive mounted on top of a directory with the same name. For example, if you have a directory under / called backup which contains data and then you mount a new drive on top of that directory and label it /backup but it contains no data then the space used will show up with the df command even though the du command shows no files.

To determine if there are any files or directories hidden under an active mount point, you can try using a bind-mount to mount your / filesystem which will enable me to inspect underneath other mount points. Note, this is recommended only for experienced system administrators.

mkdir /tmp/tmpmnt
mount -o bind //tmp/tmpmnt
du /tmp/tmpmnt

After you have confirmed that this is the issue, the bind mount can be removed by running:

umount /tmp/tmpmnt/
rmdir /tmp/tmpmnt

Another possible cause might be filesystem corruption. If this is suspected, please make sure you have good backups, and at your convenience, please unmount the filesystem and run fsck.

Again, this should be done by experienced system administrators.

You can also check the calculation by running:

strace -e statfs df /

This will give you output similar to:

statfs("/", {f_type=XFS_SB_MAGIC, f_bsize=4096, f_blocks=20968699, f_bfree=17420469, 
f_bavail=17420469, f_files=41942464, f_ffree=41509188, f_fsid={val=[64769, 0]}, 
f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_RELATIME}) = 0
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 83874796 14192920 69681876 17% /
+++ exited with 0 +++

Notice the difference between f_bfree and f_bavail? These are the free blocks in the filesystem vs free blocks available to an unprivileged user. The used column is merely a calculation between the two.

Hope this will make your idea clear. Let me know if you still have any doubts.

  • First of all thank you very much on your detailed answer. Second I'm just trying to understand, my FileSystem mounting on '/' and using 75% of my storage, my actually log files and everything that i can "see" is 34%, i went through what you wrote but none of them actually help me to solve where i can find the missing 41% of storage, just want to clean the system. – Bernana Nov 25 '21 at 08:23
  • Okay the simple way to check the storage and deleting files and folders from your system using terminal is *ncdu* simply type "sudo apt install ncdu -y" and then type "ncdu /" you will your storage data. – Amogh Saxena - REXTER Nov 25 '21 at 08:33
  • If you want I can Add another answer posting the solution for so. – Amogh Saxena - REXTER Nov 25 '21 at 08:34
  • when i run the 'ncdu' command it's still showing me only 11G instead to show all the 25G that in usgae – Bernana Nov 25 '21 at 08:56
  • as i told only df command will give you realtime data. Dont believe on du datas. – Amogh Saxena - REXTER Nov 25 '21 at 09:39
  • So actually i don't have a way to find all data and delete some of data that i don't know and can't know actually – Bernana Nov 25 '21 at 11:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239579/discussion-between-amogh-saxena-rexter-and-bernana). – Amogh Saxena - REXTER Nov 25 '21 at 12:34