Software & AppsOperating SystemLinux

How To Determine Directory Size from Command Line

Ubuntu 21

In the world of system administration, it’s crucial to keep track of your directories’ sizes to manage storage efficiently. This article will guide you through various methods to determine directory size from the command line.

Quick Answer

To determine the size of a directory from the command line, you can use the du command with the -s option for a summary and the -h option for a human-readable format. Additionally, you can sort the output by size using the sort command and limit the depth of the listing with the --max-depth parameter. Alternatively, you can use the ncdu command for an interactive overview of directory sizes or the dust command for a graphical representation.

The du Command

The primary tool for this task is the du command, short for “disk usage”. This command summarizes the disk usage of a set of files and directories.

Basic Usage

The basic syntax of the du command is as follows:

du [options] [directories and/or files]

To get the total size of a directory, use the -s option for a summary, and the -h option for human-readable format:

du -sh /path/to/directory

In this command, -s stands for “summary” and -h stands for “human-readable”. The summary option provides the total size of the directory, and the human-readable option converts the size to a format easier to understand (KB, MB, GB, etc.).

Sorting by Size

If you want to sort the output by size, you can pipe the du command to the sort command:

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

Here, the -h option with the sort command allows it to understand the human-readable sizes (KB, MB, GB, etc.).

Limiting Depth

To avoid a recursive listing of all files and directories, use the --max-depth parameter:

du -h --max-depth=1 /path/to/directory

The --max-depth parameter limits the depth of the directory listing. In the above example, it will only display the size of the directory and its immediate contents.

The ncdu Command

Another useful tool for visualizing directory sizes is ncdu, short for “NCurses Disk Usage”. It provides a fast and easy-to-use interface through the ncurses programming library.

Installation and Usage

Install ncdu using the following command:

apt-get install ncdu

Then, run ncdu with the -x option to get an interactive overview of directory sizes:

ncdu -x /

The -x option restricts the search to files in the same filesystem.

The dust Command

If you prefer a graphical representation, the dust tool is an excellent choice. It provides an overview of directory sizes with a bar chart.

Installation and Usage

Install dust with the following command:

apt-get install dust

Then, run dust in the desired directory:

dust /path/to/directory

Conclusion

In this article, we’ve explored several methods to determine directory size from the command line. Whether you prefer the traditional du command, the interactive ncdu, or the graphical dust, you now have the tools to manage your disk usage effectively. Remember to check the respective man pages (man du, man ncdu, man dust) for more details and options.

How can I determine the size of a specific directory?

You can use the du command with the -sh options followed by the path to the directory. For example, du -sh /path/to/directory will provide the total size of the directory in a human-readable format.

How can I sort the directory sizes in ascending order?

You can pipe the du command to the sort command using the -h option. For example, du -h /path/to/directory | sort -h will sort the output by size in ascending order.

How can I limit the depth of the directory listing?

To limit the depth of the directory listing, you can use the --max-depth parameter followed by the desired depth level. For example, du -h --max-depth=1 /path/to/directory will display the size of the directory and its immediate contents.

What is the `ncdu` command used for?

The ncdu command, short for "NCurses Disk Usage", is used for visualizing directory sizes in an interactive interface. It provides a fast and easy-to-use overview of directory sizes.

How can I install `ncdu`?

You can install ncdu using the command apt-get install ncdu. This command will install the ncdu package on your system.

What is the `dust` command used for?

The dust command is used for providing a graphical representation of directory sizes with a bar chart. It offers an overview of directory sizes in a visually appealing format.

How can I install `dust`?

You can install dust using the command apt-get install dust. This command will install the dust tool on your system.

Leave a Comment

Your email address will not be published. Required fields are marked *