Linux – Disk Utilization using du

Background

End of Year, Beginning of a new year, and end of Month are often good times to stress a computer system.

We have had our systems stressed these last couple of months.

Often it is running low on disk space, a lot of higher than normal database backup and log backups.

 

MS Windows

Microsoft Tools

On Windows I will often use Microsoft’s own Computer Management to review disks logical size and utilization.

Or File Explorer and filter by size looking for huge ones.

Space Sniffer

Or better still SpaceSniffer.

I spoke about SpaceSniffer here.

Linux

I know I am avoiding talking about Linux.

And, so let us talk about Linux a bit.

du command

Introduction

I suppose du means disk utilization.

Help

First let us get a quick overview of what du does and the options it offers.

Syntax


du --help

Output

Explanation

What does it do?

Summarize disk usage of each FILE, recursively for directories.

Options Tabulated

Option Meaning Usage
-a List files as well, not just directories Usage is du -a
-B Block Size -b 1 is default
-b 1024 means to divide the actual size by 1 K
-h Human readable  usage is du -h
–exclude=PATTERN Exclude files based on certain filename or extension du -a -B 1 -h –exclude=*.gz /dbbackup
 –max-depth  Our many folders down to go  du –max-depth=4 /dbbackup

 

Sample

Block Size ( -B)
Sample ( Block Size at 1 )

du -a -B 1 /dbbackup | grep "sql"

Output

Explanation
  1. /dbbackup/gregory/export.sql is 4096

 

Sample ( Block Size at 1024 )

du -a -B 1024 /dbbackup | grep "sql"

Output

Explanation
  1. /dbbackup/gregory/export.sql
    • was at 4096
    • But, now is 4
      • 4096/1024 => 4

 

Human Readable ( -h)
Sample ( Block Size at 1024 )

du -a -h *.sql

Output

Explanation
  1. /dbbackup/gregory/export.sql
    • was at 4096
    • But, now a more readable 4.0 K

 

exclude ( -exclude=filter)
Sample ( Exclude files already compressed via gzip )

du -a --exclude=*.gz

max-depth=N
Depth to show = 1

du --max-depth=1 -h /dbbackup

Output
 

Depth to show = 4

du --max-depth=4 -h /dbbackup

Output

Explanation
  1. In our first sample:
    • We progressed down only one folder tree from our asking node ( /dbbackup )
  2. In our second example:
    • We went down 4 levels from our root from /dbbackup

 

Use case scenario

Use case #1

Get Folder Size for our backup folder ( /dbbackup)

Baseline

Sample

du --max-depth=4 /dbbackup

Output

 

Silence Errors

There is an error for “permission denied“.

Let us silence it for now.

Sample

du --max-depth=4 /dbbackup 2>1 | grep -v 'permission denied'

Output

Explanation
  1. Using 2>1
    • We redirect errors such as permission denied to standard output
  2. Using grep -v
    • We filter out ‘permission denied’

 

Ordered Results

Let us order by size.

Sample

du --max-depth=4 /dbbackup 2>1 | grep -v 'permission denied' | sort -nr

Output

 

References

  1. StackOverflow
    • Exclude all permission denied messages from “du”
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s