Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The working principle of du and df under linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how du and df work under linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how du and df work under linux.

1. The principle introduces the working principle of 1.1du

The du command calls the system call fstat one by one for statistical files to get the file size. Its data is based on files, so it has a lot of flexibility, not necessarily for one partition, and can operate across multiple partitions. If there are a lot of files in the target directory, du will be very slow.

1.2 how df works

The df command uses the statfs system call to directly read the super block information of the partition to get the partition usage. Its data is based on partition metadata, so it can only be applied to the entire partition. Because df reads the super block directly, the running speed is not affected by the file.

two。 Experimental simulation

A common df and du inconsistency is a problem caused by a file being deleted while a process handle is still caused. When a file is deleted, it is no longer visible in the file system directory, so du will no longer count it. However, if there is still a running process holding a handle to the deleted file, the file will not actually be deleted on disk, and the information in the partition super block will not be changed, so that df will still count the deleted file.

First check the disk and path

[root@zhjk115 app] # df-h

Filesystem Size Used Avail Use% Mounted on

/ dev/mapper/VolGroup-lv_root

45g 8.0g 35g 19% /

Tmpfs 4.0G 1.5G 2.5G 37% / dev/shm

/ dev/mapper/VolGroup-lv_app

255G 42G 201G 18% / app

/ dev/sda1 485m 38m 422m 9% / boot

[root@zhjk115 app] #

[root@zhjk115 app] # pwd

/ app

Create the file of 1G university with dd command

[root@zhjk115 app] # dd if=/dev/zero of=/app/test.iso bs=1024k count=1000

1000000 records in

1000000 records out

1048576000 bytes (1.0GB) copied, 4.31891 s, 243MB/s

Check the results of df and du, which are currently consistent

[root@zhjk115 app] # df-h

Filesystem Size Used Avail Use% Mounted on

/ dev/mapper/VolGroup-lv_root

45g 8.0g 35g 19% /

Tmpfs 4.0G 1.5G 2.5G 37% / dev/shm

/ dev/mapper/VolGroup-lv_app

255G 43G 200G 18% / app

/ dev/sda1 485m 38m 422m 9% / boot

[root@zhjk115 app] # du-sh

43G

The simulation process is using test.iso files

[root@zhjk115 app] # tail-f test.iso &

[1] 22349

[root@zhjk115 app] # ps-ef | grep tail

Root 22349 21633 28 09:56 pts/1 00:00:01 tail-f test.iso

Root 22353 21633 0 09:56 pts/1 00:00:00 grep tail

By deleting the test.iso file, we can see that the results of df and du are inconsistent.

[root@zhjk115 app] # rm-rf test.iso

[root@zhjk115 app] # df-h

Filesystem Size Used Avail Use% Mounted on

/ dev/mapper/VolGroup-lv_root

45g 8.0g 35g 19% /

Tmpfs 4.0G 1.5G 2.5G 37% / dev/shm

/ dev/mapper/VolGroup-lv_app

255G 43G 200G 18% / app

/ dev/sda1 485m 38m 422m 9% / boot

[root@zhjk115 app] # du-sh

42G

Use lsof to see which process is using / app/test.iso

[root@zhjk115 app] # lsof | grep test.iso

Tail 22349 root 3r REG 253,2 1048576000 12 / app/test.iso

Manual kill occupies the process of test.iso file. At this time, the results of du and df are the same.

[root@zhjk115 app] # kill-9 22349

[1] + Killed tail-f test.iso

[root@zhjk115 app] # du-sh

42G

[root@zhjk115 app] # df-h

Filesystem Size Used Avail Use% Mounted on

/ dev/mapper/VolGroup-lv_root

45g 8.0g 35g 19% /

Tmpfs 4.0G 1.5G 2.5G 37% / dev/shm

/ dev/mapper/VolGroup-lv_app

255G 42G 201G 18% / app

/ dev/sda1 485m 38m 422m 9% / boot

Conclusion:

This experiment is mainly aimed at the use of the Linux environment. This problem is caused by the release of the file handle of the process. In many cases, files such as cleaning logs are shown by du as freed space, but the df space is still in use. You can use echo (or >) instead of rm to avoid this situation. At the same time, you can also see which process is in use, and you can manually clean up, restart the application or wait for release.

Note: the same is true when some logs of the oracle host are cleaned but the df display space is not freed. Generally speaking, you can wait for a period of time, otherwise you need to restart the database instance to free up the space.

At this point, I believe you have a deeper understanding of "how du and df work under linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report