du command is a command line utility for reporting file system disk space usage. It can be used to find out disk usage for files and folders and to show what is taking up space. It supports showing just directories or all files, showing a grand total, outputting in human readable format and can be combined with other UNIX tools to output a sorted list of the largest files of folders on a system.
To view a disk usage summary of a directory pass the directory to the
du command. This will print a summary of the files and folders in a directory.
du ~/go 8 /home/george/go/src/gopkg.in/yaml.v2/.git/info 48 /home/george/go/src/gopkg.in/yaml.v2/.git/hooks 8 /home/george/go/src/gopkg.in/yaml.v2/.git/refs/heads 8 /home/george/go/src/gopkg.in/yaml.v2/.git/refs/remotes/origin 12 /home/george/go/src/gopkg.in/yaml.v2/.git/refs/remotes 1712 /home/george/go/src/gopkg.in/yaml.v2 1716 /home/george/go/src/gopkg.in ...
The output show the disk usage in kilobytes in the first column followed by the full path to the file or folder. Folders are summaries so include a sum of files and folders within them.
To view a grand total for a directory pass the
-c option. This will show the full output and append a total line.
du -c ~/go ... 14420 /home/george/go/pkg 23088 /home/george/go/bin 170436 /home/george/go 170436 total
To view disk usage in human readable format pass the
-h option. Instead of showing file size in kilobytes for all files and folders the output is modified to into a human readable format.
du -h ~/go 8.0K /home/george/go/src/gopkg.in/yaml.v2/.git/info 48K /home/george/go/src/gopkg.in/yaml.v2/.git/hooks 1.2M /home/george/go/src/gopkg.in/yaml.v2/.git/objects/pack 1.2M /home/george/go/src/gopkg.in/yaml.v2/.git/objects
To view the file size of a directory pass the
-s option to the du command followed by the folder. This will print a grand total size for the folder to standard output.
du -s /home/george 2142628 /home/george
Along with the
-h option a human readable format is possible.
du -sh /home/george 2.1G /home/george
To sort by file size pass the output of
sort and use the
-n (numeric) and
-r (reverse) options.
du ~/go | sort -n -r | less 170440 /home/george/go 132816 /home/george/go/src 74024 /home/george/go/src/github.com 57072 /home/george/go/src/golang.org
To find the largest folders on a file system pass the
-a option. This will change the behaviour of
du to write size counts for files as well as folders. Run the following as root to see the ten largest files or folders on a system. This can be useful if you are dealing with out of disk space issues on a system.
du -a / | sort -n -r | head -n 10 5351116 / 2462616 /usr 2153492 /home 2153472 /home/george 1571924 /usr/lib
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Using HashiCorp Vault with LDAP
How to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users
Linux and Unix xargs command tutorial with examples
Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.
Copy a file in Go
How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.