“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
Introduction to the
In Linux, check the size of the file space should be a very common command. Today, I will introduce you to the Linux system check the size of the file space in two ways and in use may encounter strange problems.
Why are there two ways? Because we can use du to see how much space is occupied, we can also use df to see how much space is left, just like the two sides of a coin.
So without further ado, let’s get started on today’s highlights.
Du command
The most direct command to check the size of the space is du, which stands for disk Usage. Disk space usage.
Let’s 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 ...]
Copy the code
The du command is used to display the usage of the file system. By default, the information about the current directory is displayed. You can also specify a specific directory.
There are a lot of arguments for du, and I’m not going to go through them all, but let’s talk about some of the most common ones.
-h is a human-readable pronunciation, so we always use -h.
For example, to check the space usage of the current directory, you can use:
# du -ah
Copy the code
The command above shows all the files in the directory. If you want to add up all files, you can use:
# du -hs
Copy the code
If you want to specify a specific directory, simply add the directory name.
If you want to see the size of specific files in a directory, and also want to count the total size, you can use:
du -ch
Copy the code
The command above adds the total size later.
Sometimes we may find that the directory takes up too much space, but we do not know which directory, what to do?
Du provides a Max -depth=1 parameter to specify the level of the statistics directory. You can adjust it according to your needs.
The df command
The df command is similar to the du command, but it counts the remaining directory space.
The df command is as follows:
df [-b | -h | -H | -k | -m | -g | -P] [-ailn] [-t] [-T type] [file | filesystem ...]
Copy the code
So are df and DU the same thing?
In most cases it is the same, but in some cases there is a large error in the statistics of both.
You’ve probably had the experience of having a big log file that keeps writing, and if you delete that log file, what happens?
Du is the sum of the statistics file sizes, while DF is the statistics block usage.
In the above example, although the file was deleted, the file handle was not released, so du data shows that the file was deleted, but df shows that the file is still there. Until the process that opens the large file is killed.
To check the open status of the file, run the following command:
fuser -u
Copy the code
conclusion
Of course, there is the simplest ls command, also can easily check the size of the file. I hope you enjoy it.
This article is available at www.flydean.com/
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!