Linux view directory size and hard disk size

  

linux view directory size and hard disk size command:

# du -ks ---in all

# du -k ---every Last

Both the du and df commands are used to obtain file system size information: df is used to report the total number of blocks in the file system and the number of remaining blocks, 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. The following example:

# du -s /tmp returns the following value:

---12920 /tmp

and df /tmp returns the following value:

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

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

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

File System Allocation Some of the disk blocks are used to record some of its own data, such as i-nodes, disk maps, indirect blocks, superblocks, and so on. This data is invisible to most user-level programs and is 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 some parts of the file system. For example, an allocation of Meta Data in a log file system with a frag=4096 and nbpi=4096 empty size of 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 4.X version:

The result of 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-byte units:

41 * 8 = 328

328(meta data) + 16(from du) = 344< Br>

So there are 344 blocks in 512-byte units allocated to this empty file system.

Using the df /foo command we can get the following result:

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

/dev/lv01 ------8192 -----7848 -----5% -----16 -----2% ----/foo< Br>

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 an empty file system, but for a non-empty file system, it is difficult to implement because the size of the indirect block of the file in Meta Data is variable. So we don't need to look at the matching relationship between du and the value returned by df, but only need to know that the value returned by the du -s command reflects the number of disk blocks allocated to files and directories, and 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 showing the difference between du and df commands is as follows:

If the user deletes a file in a directory opened by a running application, the du command returns The value shows the size of the directory after subtracting the file. However, the df command does not display the size after subtracting the file. Until the running application closes the open file, the value returned by df shows the usage of the file system after subtracting the 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. Use df to see the size and remaining space of the installed file system.

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

Copyright © Windows knowledge All Rights Reserved