As a public environment, the server development environment often encounters disk space bursts (I don't know if you've ever encountered this, but I often do). Since many of our development classmates put files on it, in this case we can only delete our useless files first. However, I have encountered a situation before, I deleted the 10 G files previously stored, free up less than two minutes, disk space is full again!!!
So it's very important to know the command of viewing server disk space. Otherwise, how can you find out what is causing the disk space to grow so fast? Let's go on and see how to use the command du accurately so that we can make fewer detours in normal development.
Say, the following commands are all the results of root user execution in centos7.6 environment.
du
Displays a summary of disk usage for the catalog
[root@hadoop3 /]# du /tmp 4 /tmp/.font-unix 4 /tmp/.ICE-unix 4 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 8 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 4 /tmp/.XIM-unix 260 /tmp/hsperfdata_root 4 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 8 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 4 /tmp/.Test-unix 4 /tmp/.X11-unix 300 /tmp
Command Details: The'du'command without any options lists all files and folders in a given directory or current working directory. In addition, it will display as a block along their path, and at the bottom of the page, it will display the total file size as a block. As you can see in the example above, the file path is displayed with the block size of an existing file. However, the output above is not in a readable format, which is its main disadvantage.
du -h
Display a summary of disk usage for the catalog as readable
[root@hadoop3 /]# du -h /tmp 4.0K /tmp/.font-unix 4.0K /tmp/.ICE-unix 4.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 8.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 4.0K /tmp/.XIM-unix 260K /tmp/hsperfdata_root 4.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 8.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 4.0K /tmp/.Test-unix 4.0K /tmp/.X11-unix 300K /tmp
Command Details: Using the'du-h'option will list all outputs in a'readable format'. This'-h'option converts the block size to a readable format, such as Bytes, Kilobytes, Megabytes, or Gigabytes. In the example above, you can see that all file sizes are printed as output K.
du -sh
View total disk usage for a specific directory
[root@hadoop3 /]# du -sh /tmp 300K /tmp
Command details:'du-sh'option will show the exact size of the directory used.'- The s'flag will show the total number of directories with block sizes, but a combination of the'-h' flags will convert the output to a readable format. This command is used most frequently in practice.
du -a
List disk usage for all files/subfiles in this directory
[root@hadoop3 /]# du -a /tmp 4 /tmp/.font-unix 4 /tmp/.ICE-unix 4 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 8 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 4 /tmp/.XIM-unix 32 /tmp/hsperfdata_root/5057 32 /tmp/hsperfdata_root/3645 32 /tmp/hsperfdata_root/963 32 /tmp/hsperfdata_root/4034 32 /tmp/hsperfdata_root/11619 32 /tmp/hsperfdata_root/18122 32 /tmp/hsperfdata_root/12567 32 /tmp/hsperfdata_root/3820 260 /tmp/hsperfdata_root 4 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 8 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 4 /tmp/.Test-unix 0 /tmp/wrapper-680-1-in 4 /tmp/.X11-unix 0 /tmp/wrapper-680-1-out 300 /tmp
Command Details: With the'-a'option, you can list and print disk usage for each file, including directories and subdirectories. This command identifies the largest file/folder in a given path and helps you delete/clear unused or largest files to provide sufficient free space for the server. In the example above, you can see the difference from the previous example, where each file, including the directory, is listed. If you add the'-h'flag to the command above, such as'du-ah', all output is in a human readable format.
du -c
List total disk space occupied by files
[root@hadoop3 /]# du -ch /tmp 4.0K /tmp/.font-unix 4.0K /tmp/.ICE-unix 4.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 8.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 4.0K /tmp/.XIM-unix 260K /tmp/hsperfdata_root 4.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 8.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 4.0K /tmp/.Test-unix 4.0K /tmp/.X11-unix 300K /tmp 300K total
Command details: Using the'-c'option will list the total disk space used at the bottom of the output. If you add the'-h'flag to the command above, such as'du-ch', all output is in a readable format.
du -BK/-BM/-BG
Output file/folder disk occupancy size in specified capacity units
[root@hadoop3 /]# du -BK /tmp 4K /tmp/.font-unix 4K /tmp/.ICE-unix 4K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 8K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 4K /tmp/.XIM-unix 260K /tmp/hsperfdata_root 4K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 8K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 4K /tmp/.Test-unix 4K /tmp/.X11-unix 300K /tmp [root@hadoop3 /]# du -BM /tmp 1M /tmp/.font-unix 1M /tmp/.ICE-unix 1M /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 1M /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 1M /tmp/.XIM-unix 1M /tmp/hsperfdata_root 1M /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 1M /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 1M /tmp/.Test-unix 1M /tmp/.X11-unix 1M /tmp [root@hadoop3 /]# du -BG /tmp 1G /tmp/.font-unix 1G /tmp/.ICE-unix 1G /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 1G /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 1G /tmp/.XIM-unix 1G /tmp/hsperfdata_root 1G /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 1G /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 1G /tmp/.Test-unix 1G /tmp/.X11-unix 1G /tmp
Command details: Combining the'-B'tag with'K','M' or'G', you can divide the total disk usage of files and directories into kilobytes (Kilobytes), megabytes (Megabytes), or gigabytes (Gigabytes). However, please note that when using capacity units, use them appropriately, otherwise this will happen, there is actually no 1M, using-BM will result in files rounding capacity.
du —max-depth=x
Check the size of all subdirectories in the current location
[root@hadoop3 /]# du -h --max-depth=1 /tmp 4.0K /tmp/.font-unix 4.0K /tmp/.ICE-unix 8.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 4.0K /tmp/.XIM-unix 260K /tmp/hsperfdata_root 8.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 4.0K /tmp/.Test-unix 4.0K /tmp/.X11-unix 300K /tmp
Command Details: Add the'-max-depth=x'parameter to check the disk capacity size of the x-depth subdirectory in the current directory.
du —exclude="xxx"
Exclude specific types of files when calculating disk size
[root@hadoop3 /]# du -h --exclude="*.font-unix" /tmp 4.0K /tmp/.ICE-unix 4.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 8.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 4.0K /tmp/.XIM-unix 260K /tmp/hsperfdata_root 4.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 8.0K /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 4.0K /tmp/.Test-unix 4.0K /tmp/.X11-unix 296K /tmp
Command Details: Using the'-exclude'option in the'du' command, we can remove certain modes (such as php, txt, png extensions) when calculating disk usage for all files and directories.
du —time
View disk usage at last modification time
[root@hadoop3 /]# du -ha --time /tmp 4.0K 2021-02-26 12:19 /tmp/.font-unix 4.0K 2021-02-26 12:19 /tmp/.ICE-unix 4.0K 2021-11-20 23:29 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ/tmp 8.0K 2021-11-20 23:29 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-nginx.service-pt0CTJ 4.0K 2021-02-26 12:19 /tmp/.XIM-unix 32K 2021-12-06 00:17 /tmp/hsperfdata_root/5057 32K 2021-12-06 00:18 /tmp/hsperfdata_root/3645 32K 2021-12-06 00:17 /tmp/hsperfdata_root/963 32K 2021-12-06 00:18 /tmp/hsperfdata_root/4034 32K 2021-12-06 00:18 /tmp/hsperfdata_root/11619 32K 2021-12-06 00:18 /tmp/hsperfdata_root/18122 32K 2021-12-06 00:18 /tmp/hsperfdata_root/12567 32K 2021-12-06 00:18 /tmp/hsperfdata_root/3820 260K 2021-12-06 00:18 /tmp/hsperfdata_root 4.0K 2021-11-20 18:18 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD/tmp 8.0K 2021-11-20 18:18 /tmp/systemd-private-31de4ddb230847ecb9b7d67391665ec2-chronyd.service-rVhZDD 4.0K 2021-02-26 12:19 /tmp/.Test-unix 0 2021-12-06 00:18 /tmp/wrapper-680-1-in 4.0K 2021-02-26 12:19 /tmp/.X11-unix 0 2021-12-06 00:18 /tmp/wrapper-680-1-out 300K 2021-12-06 00:18 /tmp
: Use the'time'option in the'du' command, which lists the date and time of the last modified file and directory.