[Linux] How to check disk space using the du and df commands when you want to organize your disk

table of contents
Hello, good evening
This is Miyazaki from the System Solutions Department
In this article, I will explain how to check the disk capacity, how to organize the disk capacity,
I would like to write about the du and df commands that can be used in situations like these, with some examples
df command
df - Shows how much disk space is in use and how much is available
In actual work, the only option that is used is the -h option
the -h and --human-readable
options, a size character is added, such as M for megabytes
. Since we use powers of 2 instead of powers of 10, M
represents 1,048,576 bytes.
Here is the result of actually typing the command:
[root@localhost ~]# df -h Filesystem Size Used Remaining Used% Mounted on /dev/mapper/cl-root 6.2G 4.9G 1.4G 78% / devtmpfs 905M 0 905M 0% /dev tmpfs 920M 208K 920M 1% /dev/shm tmpfs 920M 8.7M 912M 1% /run tmpfs 920M 0 920M 0% /sys/fs/cgroup /dev/sda1 1014M 226M 789M 23% /boot tmpfs 184M 16K 184M 1% /run/user/1000
I was able to check the disk capacity for each file system.
It's a good idea to run this command first when checking disk capacity.
du command
du - command to display disk usage of files
The three options that are commonly used in actual work are -c, -h, and -s
-c, --total
Displays the total amount
-h, --human-readable
: Displays size in a human-readable format (e.g., 1K 234M 2G)
-s, --summarize:
Displays only the total capacity of each argument.
Here is the result of actually typing the command:
[root@localhost /]# du -sch ./* 0 ./1 0 ./bin 194M ./boot 156K ./dev 49M ./etc 4.6M ./home 0 ./lib 0 ./lib64 0 ./media 0 ./mnt 19M ./opt 0 ./proc 52K ./root 65M ./run 0 ./sbin 0 ./srv 0 ./sys 296K ./tmp 3.7G ./usr 1.1G ./var 5.0G Total
running the `du` command on a directory with a capacity of several tens of gigabytes
Please note that
By investigating and specifying the directory that is consuming the most space,
you can find out which files in which directory are taking up the most space.
Next, we will examine the large directories individually
If you want to delve deeper into the directories under /usr, you can move to /usr and then run the same commands.
(Note: You don't necessarily have to move; you can also specify directories like /usr/*.)
[root@localhost /]# cd /usr [root@localhost usr]# du -sch ./* 209M ./bin 0 ./etc 0 ./games 18M ./include 716M ./lib 1.2G ./lib64 102M ./libexec 4.0K ./local 57M ./sbin 1.3G ./share 109M ./src 0 ./tmp 3.7G total
This is just an example, but from this result we can see that the capacity under /usr/share is 1.3G
If you are having trouble searching because the data is too large or it takes too long to get the results,
--exclude=directories to exclude
This option allows you to exclude the target directory from the search.
Using the previous command as an example, the result was as follows:
[root@localhost usr]# du -sch ./* --exclude=share 209M ./bin 0 ./etc 0 ./games 18M ./include 716M ./lib 1.1G ./lib64 102M ./libexec 0 ./local 57M ./sbin 109M ./src 0 ./tmp 2.3G total
the /home/qmail directory is excluded from the search, and
the total size displayed after excluding /usr/share is 2.3G.
summary
How was it?
Disk space increases day by day, and before you know it, it's full.
I hope this will be helpful in such situations. That's all.
4
