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
show available space on the mounted file system

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 and directory one level

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 each file and directory first five result

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 each file and directory sorted by size

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
show the size of each file and directory filter by the unit of size

References