How to Get the Size of a Directory in Linux?


Introduction

In the Linux operating system, a directory is similar to a folder in other operating systems. It's a way to organize files and other directories, which helps the user easily locate and access them.

Directories can be nested within each other, creating a hierarchical structure that can be several levels deep. One important feature of directories in Linux is that they are treated as files themselves.

This means that they have attributes and permissions, just like any other file. Directories also contain metadata such as creation time and last modified time.

Method 1: Using the "du" Command

Explanation of the "du" command and its options

In Linux, the "du" command stands for "disk usage". It is a powerful tool that allows users to determine the size of a directory or file on their system.

The command works by recursively scanning a directory and all of its subdirectories, calculating and displaying the sizes of each item. There are several options that can be used with the "du" command to customize its output.

One useful option is "-h", which displays sizes in a human-readable format, such as kilobytes, megabytes, or gigabytes. Another useful option is "-s", which displays only a summary of the total size of a directory rather than individual sizes for each item.

Step-by-step guide on how to use the "du" command to get directory size

To use the "du" command to get the size of a directory in Linux, follow these steps −

  • Open your terminal and navigate to the parent directory containing your target directory.

  • Type "du -h target_directory_name/" (without quotes), where "target_directory_name" is replaced with the name of your desired directory.

  • Press enter. This will display a list of all files and directories within your target directory, along with their corresponding sizes in human-readable form.

Examples and explanations of different options that can be used with the "du" command

The following are some examples of options that can be used with the "du" command −

  • "-s" −Displays only a summary of total disk usage for specified directories

  • "-a" −Displays disk usage for all files including hidden ones

  • "-c" −Shows total disk usage at end

  • "--exclude=[directory]" −Excludes specified directories from the calculation

  • "-x" −Excludes files on other file systems

For example, if you wanted to exclude a specific directory from the calculation, you could use the "--exclude" option.

For example,

du -h --exclude=/home/user/dir_to_exclude/ target_directory_name/

would display the size of your target directory while excluding "dir_to_exclude". Knowing how to use these options with the "du" command can make it easier to get more accurate or desired results when calculating directory sizes.

Method 2: Using the "ls" Command with Filters

Explanation of the "ls" command and its filters

The 'ls' command is a popular utility tool in Linux that stands for "list directory contents." The primary purpose of this command is to display a list of files and directories within a directory. It provides various options to customize the output, including filters that allow users to sort or limit the results based on specific criteria. One crucial aspect of filtering in 'ls' is its ability to filter by size.

Users can specify file/directory sizes, which assists them in getting a clear idea of their directory's size. The size filter option in 'ls' is an essential tool for system administrators who need to monitor disk usage regularly.

Step-by-step guide on how to use filters with the "ls" command to get directory size

To use filters with the 'ls' command for getting directory size, follow these steps −

  • Open your terminal

  • Navigate to the parent directory you want to check

  • Run the following command: `ls -lhS`

  • This will list all files and directories within that folder sorted by size, with human-readable formatting.

The above command lists all files and directories within that folder, sorted by their sizes (human-readable format). You may also specify a particular file type extension with ls so that only those types are returned.

Examples and explanations of different filters that can be used with the "ls" command

There are many filters available in 'ls' commands; some are used more frequently than others while specifying file/directory sizes as per user specifications. Here are some examples −

  • `find . -type f -size +50M` −This filter option lists all files in your current working directory greater than 50 MB in size.

  • `ls --sort=size -lh` − This filter option lists all files and directories within that folder, sorted by their sizes (human-readable format).

  • `ls | grep .zip$` −This filter option lists all files with the ".zip" extension.

The above examples demonstrate some of the many ways you can use 'ls' command filters for getting directory size. By using these filters, users can easily identify large files and directories and take appropriate actions according to their needs.

Method 3: Using GUI Tools

Introduction to GUI tools for getting directory sizes in Linux

For users who prefer a graphical user interface (GUI) over the command line, Linux provides several tools for getting the size of a directory. One of the most popular tools is Disk Usage Analyzer, also known as Baobab. This tool provides an interactive and intuitive representation of disk usage that is easy to understand.

Disk Usage Analyzer scans directories and displays a graphical representation of the disk usage using a tree map. The larger boxes represent bigger files or directories, while smaller ones represent smaller files or directories.

Each box is color-coded based on file type, making it easy to identify different types of files. Users can navigate through the tree map by clicking on boxes and zooming in or out as needed.

Explanation and demonstration of popular GUI tools like Disk Usage Analyzer, Baobab, etc.

In addition to Disk Usage Analyzer, there are several other GUI tools available for getting directory sizes in Linux. These include Filelight, KDirStat, and QDirStat. Each tool has its own unique features and advantages.

Filelight is a lightweight tool that offers similar functionality to Disk Usage Analyzer but with fewer options. It displays disk usage using concentric circles rather than tree maps.

KDirStat is another powerful tool that offers more advanced features than some other options. It allows users to view file statistics by extension or age, sort results by name or size, and even delete files within the application.

QDirStat is another option that offers similar functionality to KDirStat but with a simpler user interface. It displays disk usage using a treemap like Disk Usage Analyzer but allows users to drill down into individual folders for more detailed information.

Linux provides several GUI-based tools for getting directory sizes that can be used instead of command-line methods. Users can choose the tool that best suits their needs based on their personal preferences and the type of information they need to retrieve.

Conclusion

In this article, we have explored several methods for getting the size of a directory in Linux. We started by introducing the concept of directories in Linux and emphasizing their importance. We then discussed three different methods for determining the size of a directory: using the "du" command, using the "ls" command with filters, and utilizing GUI tools.

The first method we covered was using the "du" command, which is one of the most commonly used commands for getting directory sizes in Linux. We provided a detailed step-by-step guide on how to use different options with this command to achieve specific results.

Updated on: 06-Jun-2023

27K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements