Introduction
It should be a very common command to view the file space size in the Linux system.Today, I will introduce two methods for viewing the file space in the Linux system and the strange problems that may be encountered in use.
Why are there two methods? Because we can use the du command to see the space occupancy, or use df to see the remaining space, just like the front and back of a coin, how to use it comfortably.
Not much to say, let's start our exciting content today.
du command
The most direct command to check the size of the space is du, the full name of this command is disk usage.It indicates the occupancy of disk space.
First look at the basic syntax of the du command:
du [-Aclnx] [-H | -L | -P] [-g | -h | -k | -m] [-a | -s | -d depth] [-B blocksize]
[-I mask] [-t threshold] [file ...]
The du command is mainly used to display the usage of the file system. The default is to display the information of the current directory. Of course, you can also specify a specific directory.
There are many parameters for du, so I won't list them all here. Here we will talk about some of the most commonly used usages.
Among them, -h means that it is a human-recognizable pronunciation, so we usually bring -h.
For example, to view the space usage of the current directory, you can use:
# du -ah
The above command shows all the files in the directory. If you want to add up all the files, you can use:
# du -hs
If you want to specify a specific directory, just add the name of the directory directly.
If you want to view the size of specific files in the directory, and want to count the total size, you can use:
du -ch
The above command will add the total size to the end.
Sometimes we may find that the directory takes up too much space, but we don't know which directory it is, what should we do?
du provides a parameter of --max-depth=1, which can specify the level of the statistical directory, and you can adjust it according to your needs, which is very convenient.
df command
The df command is similar to the du command, but it counts the remaining space of the directory.
The command of df is as follows:
df [-b | -h | -H | -k | -m | -g | -P] [-ailn] [-t] [-T type] [file | filesystem ...]
So are the statistics of df and du consistent?
In most cases, it is the same, but in some cases the statistics of the two will have large errors.
You may have experienced such a large log file, which is still being written continuously. If this log file is deleted at this time, what will happen?
For du, it is the sum of statistical file sizes, while df is the statistical data block usage.
In the above example, although the file is deleted, the file handle is not released, so the data of du shows that the file has been deleted, but df shows that the file is still there. Until the process of opening a large file is Killed.
You can check the open status of the file with the following command:
fuser -u
Summarize
Of course, there is also the simplest ls command, you can also simply check the size of the file. I hope everyone will like it.
This article has been included in http://www.flydean.com/02-du-and-df/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", know technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。