Recently I need to occasionally check disk usage on the Linux server to know how much disk space before the disk is full. The server is used for caching so it is critical that this server not run out of disk space.
Below are some useful df
and du
snippets that useful for daily operation.
df: Space available on the mounted file system
df
is to show a summary of the amount of available space on the mounted file system. The command below will show the available space in a human-readable way.
df -h
du: How many sizes is a file or directory
These are collections of useful snippets that I frequently used.
Show the size of each files or directory on one level only.
du . -h -d 1
Show the size of each file or directory one level and the first 5 results only. You can pipe the result to head
on all the commands below to limit the result.
du . -h -d 1 | head -5
Show the size of a file or directory sorted by size reversed (biggest at the top). Remove the r
flag to sort by smallest first.
# Sort biggest to smallest
du . -h -d 1 | sort -rh
# Sort smallest to biggest
du . -h -d 1 | sort -h
Show the size of files or directories, filter by the unit of size, and sort by biggest to smallest.
# Only show size gigabytes
du . -h -d 1 | grep '^\s*[0-9\.]\+G' | sort -rh
# Only show size megabytes
du . -h -d 1 | grep '^\s*[0-9\.]\+M' | sort -rh
# Only show size milobytes
du . -h -d 1 | grep '^\s*[0-9\.]\+K' | sort -rh
References
Description of
du
options used above, from du(1) - Linux manual page-d, --max-depth=N print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
How to Check Disk Space Usage in Linux Using df and du Commands