How to view the size of the space occupied by the directory in Linux

01-26-2023

This article mainly introduces the relevant knowledge about how to view the space occupied by directories in Linux. The editor will show you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article on how to view the space occupied by directories in Linux Can help everyone solve problems.


In linux, you can use the du command to view the space occupied by the directory, the syntax du [option] [directory or file name]; common options include -a to display the current The disk space occupied by directories and subdirectories/files, -s only displays the disk space occupied by the current directory, --apparent-size displays the size of the directory or file itself, -l counts the disk space occupied by hard links, -L counts The amount of disk space occupied by the file pointed to by the symbolic link.

In Linux, you can use the du command to view the space occupied by the directory.

linux du command

du is a command to count the disk space occupied by directories or files.

It should be noted that the size of the file can be seen by using the "ls -r" command. But you will find that when you use the "ls -r" command to check the size of the directory, the size of the directory is mostly 4KB. ), if there are not many subdirectories and subfiles in the parent directory, one block can be put down, then the parent directory only occupies one block size.

Think of it like a library catalog and actual books. If we use the "ls -l" command to check, we can only see the book catalog of these books occupying 1 page, but we can't see how many books are actually there, even if it fills up several rooms.

But when we count directories, we don’t want to see how much space the subdirectory names and subfile names in the parent directory take up, but we want to see the total disk usage of the subdirectories and subfiles in the parent directory size, then you need to use the du command to count the real disk usage of the directory.

The format of the du command is as follows:


du [options] [directory or filename]


Common options for du:

  • -h: Display in human readable form

  • -a: Display the disk space occupied by the directory, and also display the disk space occupied by the directories and files under it

  • -s: Display the disk space occupied by the directory, do not display the disk space occupied by its subdirectories and files

  • -c: Display several directories or The size of the disk space occupied by the file, and the sum of them

  • --apparent-size: Display the size of the directory or file itself

  • -l: Statistical disk space occupied by hard links

  • -L: Statistical disk space occupied by files pointed to by symbolic links

Example of du command

1、du -h

QQ截图20230126210600.jpg

2, du -a

When using this option, display the size of the disk space occupied by the directory and subdirectories and files under the directory.

QQ截图20230121200509_1.jpg

It can be seen that by default, du only displays the size of the disk space occupied by the ./ directory, and after using the -a option, it not only displays the directory (the last line), It also shows the size of the disk space occupied by each file in the directory.

3, du -s

When using this option, du only displays the size of the disk space occupied by the directory, not the subdirectories and files under it Information about disk space usage.


By default, du does not display the information about the disk space occupied by the files under the directory, but it will display the information about the disk space occupied by the subdirectories under it; after using the -s option, Only the size of the disk space occupied by the xx directory is displayed.

4. du --apparent-size

Displays the size of the file or directory itself, not the disk space they occupy. The size of the disk space occupied by files or directories is sometimes not exactly the same as their own size; this phenomenon is not unique to linux, and it is the same in windows.

Let's look at the explanation of the help documentation for this option:

The apparent size of a file is the number of bytes reported by `wc –c` regular files , or more Generally, `ls –l –block-size=1` or `stat –format=%s`. For example, a file containing the word `zoo` with no newline would, of course, have an apparent size of 3. Such a small file may require anywhere from 0 to 16 kib or more of disk space, depending on the type and configuration of the file system on which the file resides.

This paragraph gives the difference between the size of the file or directory itself and the size of the disk space occupied. He gave a more exaggerated example below, so I won't write it here. We know: wc or ls --block-size shows its own size, and du gives the size of the occupied disk space.

1.jpg

5, du -c

When using this option, it will not only display the disk space occupied by several files or directories

As shown in the figure, after adding the -c option, du not only displays the size of the disk space occupied by the two directories, but also the last line Count their sum

6, du -lh --max-depth=1

Check the disk occupied by sub-files and sub-directories under the current directory capacity.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us