Home  »  ArticlesGuidesHow ToTechnologyTools   »   How to Get the Directory Size from the Command Line in Ubuntu

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

There are no shortage of reasons to get the directory size in any Linux or any Operating system. This guide will show you how to do it with commands that work in Ubuntu or Debian based systems.

Using the du Command to get the Directory Size

Linux users can use the du command which is short for “disk usage” to get the directory size from the command line in Ubuntu. With this command you can get disk usage of the files and folders recursively.

The du command allows users to check by relative path or absolute path using the syntax as shown below.

The du command will return an error “du: cannot read directory” if the user has no permissions to on the directory or files. To fix that you should run the commands with sudo privileges.

$ du -hs /absolute/path/to/directory

or

$ du -hs relative/path/to/directory

Any path that starts with a forward slash / is an absolute path as show in the first example.

The du Command Options Explained

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

You can remove the -s option to display the size of the specified directory as well as the size of the subdirectories inside of that directory separately.

The du command -a option allows users to get the disk space used by each file as well as subdirectories within the specified directory. The syntax is as follows:

$ du -ha /path/to/directory

You can sort the output by size by using the following command syntax:

$ du -h /path/to/directory | sort -rh

You can use the -r option to sort in reverse order from largest to smallest. Leaving out the -r option will sort from smallest to largest file.

If you do not specify any file or directory in your command, du will return the disk usage of current working directory.

Conclusion

In this short guide you have seen how to get directory size in Ubuntu Linux systems with options on summarizing, showing human readable forms, and sorting as well as getting individual file sizes.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Available under:
Articles, Guides, How To, Technology, Tools