[Linux] df, df command description and usage summary (check free disk space, check usage by folder)


Writing time : 2021-10-08 16:33:20

Let's look at the df and du commands, which can check the disk space when operating the server and check the usage for each folder with insufficient capacity.
You can check the disk space with the df command and check the disk usage by folder with the du command to see which folders are frequently used.

Description of du, df commands

df

Shows the free space on the disk.
df command document

Prints disk usage for all directories including subdirectories.
du command document

df options and sample output

If the -h option is added, disk free space is displayed in gigabyte units.

$ df -h  
Filesystem      Size  Used Avail Use% Mounted on  
/dev/xvda1       80G  4.9G   76G   7% /  
devtmpfs        1.9G     0  1.9G   0% /dev  
tmpfs           1.9G     0  1.9G   0% /dev/shm  
tmpfs           1.9G  183M  1.7G  10% /run  
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup  
tmpfs           379M     0  379M   0% /run/user/1000  

du option and sample output

If the -h option is added, the disk usage of the folder is displayed in gigabyte units.

$du -h  


Adding --max-depth limits subdirectories to the specified depth.
The command below processes the lower 1 depth directory as a target.

$du -h --max-depth=1   


Finally, you can specify the target directory.
If no directory is specified, the current directory is the target.

$du -h --max-depth=1 /var   


The sort -rh option can interfere with the output order in the order of the most used directories.
The head option specifies the number of outputs.

$du -h | sort -rh | head -20  


The following command outputs up to 20 directories in the order of the most used directories among the subfolders of the /var folder.

$du -h | sort -rh | head -20 /var  
4.9G    .  
3.3G    ./var  
2.9G    ./var/lib/jenkins  
2.9G    ./var/lib  
1.2G    ./var/lib/jenkins/jobs/todo/builds  
1.2G    ./var/lib/jenkins/jobs/todo  
1.2G    ./var/lib/jenkins/jobs  
1017M   ./usr  
648M    ./var/lib/jenkins/workspace  
615M    ./var/lib/jenkins/.gradle  
Other posts in the category