WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to Get the Size of a Directory from the Command Line in Ubuntu

Topic: Ubuntu / LinuxPrev|Next

Answer: Use the du Command

You can use the du command (short for "disk usage") to get the size of directory from the command line in Ubuntu. It basically summarize disk usage of the set of FILEs, recursively for directories.

du -hs /path/to/directory

For example, if your current working directory is /var/www and you want to determine the size of directory named assets inside of it, you can simply use the command:

du -hs assets

Or, if you prefer absolute path you can run the following command.

du -hs /var/www/assets

Both commands will have the same effect, because current working directory doesn't matter when you use absolute path. Any path that starts with a forward slash / is an absolute path.

The options have the following meanings:

  • -h : Human readable. du will print sizes in human readable format (e.g., 1K, 150M, 2G).
  • -s : Summarize. du will only display the total size of the specified directories.

Without -s option du will not only display the size of the specified directory, but also the size of the subdirectories inside of that directory separately.

Alternatively, you can use the -a option with the du command to get the disk space used by each file as well as subdirectories within the specified directory.

du -ha /var/www/assets

Moreover, if you want to display all files and subdirectories in a directory sorted by size you can use the sort command along with the du command like this:

du -h /var/www/assets | sort -rh

Here, the option -r for sort command will reverse the default sorting order (i.e., bigger files and directories will be listed first), whereas the option -h specify human numeric sort.

If you do not specify any file or directory, du will report the disk usage of current working directory.

Tip: If you run du command on a file or directory for which you don't have permissions, you will see something like this "du: cannot read directory". In this situation, you'll have to prepend the du command with sudo, for example, sudo du -hs /path/to/directory.


Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties