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

How to check directory size and hard disk size by linux

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the relevant knowledge of "how to check the directory size and hard disk size of linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Linux command to view directory size and hard disk size:

# du-ks-in all

# du-k-every last

Both the du and df commands are used to get information about the file system size: df is used to report the total number of blocks and remaining blocks of the file system, and du-s / is used to report the number of blocks used by the file system. However, we can find that the value of the number of blocks used by the file system calculated from the df command is inconsistent with the value obtained by the du command. Such as the following example:

# du-s / tmp returns the following values:

-12920 / tmp

Df / tmp returns the following values:

Filesystem-512 Used-Free -% Used-- Iused--% Iused-- Mounted on

/ dev/hd3-57344-42208Murray-26%-391,4-- / tmp

From the above values, we can calculate-=: 57344-42208 = 15136. And 15136 is greater than 12920. The difference is due to the difference in the implementation of the du and df commands: the du-s command obtains the total number of blocks used by the specified file system by adding up the number of blocks used by all directories, symbolic links, and files in the specified file system, while the df command obtains the total number of blocks and the number of remaining blocks by looking at the disk block allocation map of the file system.

The file system allocates some of these blocks to record some of its own data, such as I nodes, disk distribution maps, indirect blocks, super blocks, and so on. This data is invisible to most user-level programs, often referred to as Meta Data.

The du command is a user-level program that does not consider Meta Data, while the df command looks at the disk allocation map of the file system and considers Meta Data. The df command gets the real file system data, while the du command only looks at part of the file system. For example, the allocation of Meta Data in a journaled file system with an frag=4096 and the empty size of nbpi=4096 is 4MB is as follows:

1 4k block for the LVM

2 4k super blocks

2 4k blocks for disk maps

2 4k blocks for inode maps

2 4k blocks for .indirect

32 4k blocks for inodes

-

41 4k blocks for meta data on an empty 4MB file system

For AIX version 4.X:

The result returned by executing du / foo is as follows:

-8-/ foo/lost+found

-16-/ foo

To match the output of the du command with the output of the df command, we must add Meta Data. First, convert 41 4k blocks to values in 512 bytes:

41 * 8 = 328

328 (meta data) + 16 (from du) = 344

So 344 blocks in 512 bytes are allocated to the empty file system.

Using the df / foo command, we can get the following results:

Filesystem-- 512-blocks-- Free--% Used-- Iused---%Iused-- Mounted on

/ dev/lv01-8192-7848-5%-16-2%-/ foo

From this we can get the number of blocks used by the file system: 8192 (total blocks)-7848 (free blocks) = 344. This value is consistent with the value obtained above.

The above conversion method is easy to implement for empty file systems, but for non-empty file systems, it is difficult to implement because of the variable size of indirect blocks of files in Meta Data. So instead of looking at the match between du and the value returned by df, we just need to know that the value returned by the du-s command reflects the number of disk blocks allocated to files and directories, while the df command reflects the actual allocation of the file system. The actual situation reflected by the df command includes user data (files and directories) and Meta Data.

Another example that shows the difference between the du and df commands is as follows:

If the user deletes a file in a directory opened by a running application, the value returned by the du command shows the size of the directory after subtracting the file. However, the df command does not show the size of the file minus it. The value returned by df does not show the usage of the file system after subtracting the file until the running application closes the open file.

List the space occupied by a directory

1. Du or du-s or du-k

Du-S | sort-n can quickly find out which directory is the largest.

2. With df, you can see the size of the installed file system and the amount of space left.

3. Quota-v to view the user's disk space information, if you use quota to limit the size of user space.

This is the end of the content of "how to check the directory size and hard disk size of linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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