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.
Listening to BBC Radio with mpv
The BBC publishes high quality 320 kbps HLS AAC streams that can be used to listen to radio from the command-line using mpv. Here are the URLs and some aliases to start listening quickly.
Vim: So long Pathogen, hello native package loading
Vim 8 added a native way to load third-party packages. This means, for me at least, there is no longer any need to use a third party plugin helper like Pathogen.
Linux and Unix more command tutorial with examples
Tutorial on using more, a UNIX and Linux command for viewing the contents of a file or files one screen at a time. Examples of viewing a file, viewing multiple files, searching using regular expressions and opening the file in a text editor.