How to Get the Size of a Directory in Linux?
A directory in Linux acts as a container for organizing and storing files and other directories. If you are trying to manage your disk space or are simply curious about the size of a specific directory, various commands can be used to find the Linux folder size. In this article, let's explore different options to get the size of a directory in Linux.
Prerequisites
Before we dive into the different methods, let's cover a few prerequisites that can help you have the best learning experience.
- A Linux system that can be used to experiment on, can help you to understand the commands explored in the article.
- You need to have access and a basic understanding of the usage of Linux terminal. You can refer to this linux terminal article to have a basic understanding of the Linux terminal.
- A very basic understanding of the permission for files and directories in Linux can also be good. Linux has different permissions based on users and depending on the directory permissions, you may need root or sudo access. Explore more about this on the Directory Permissions article.
Option - 1: Display the Size of a Directory Using the "du" Command
The du or disk usage command is used to display the disk space used by files and directories in the file system.
Syntax:
Explanation:
- The [options] can be specified to modify the behavior of the du command.
- The [directories/files] is the directories or files for which you want to display the disk usage. If no directories or files are provided, the du command will display the disk usage of the current directory by default.
Options:
Let's explore some commonly used options with the du command:
- The -h or --human-readable option displays sizes using units such as kilobytes (K), megabytes (M), gigabytes (G), etc.
- The -s or --summarize option displays only the total size of a specified directory, without listing the size of individual files and subdirectories within them.
- The -c or --total option, displays the total of disk usage at the end as total, including the disk usage of all the specified directories.
- The -a or --all option includes all files and hidden files in the calculation.
- The -L or --dereference option can be used to show the disk usage of the files and directories that symbolic links point to. In normal cases, only the size of the link is shown.
Example:
The following example shows the steps to check the Linux folder size of the /home/hari/example directory,
-
Navigate to the directory using the cd command.
-
Use the du command with the -h option to get the size of all files and directories in the example directory.
-
Use the du command with the -c option to get the total directory size.
-
Use the du command with the -a option to get the size of all files.
Option - 2: Get the Size of the Directory in Linux Using Tree Command
The tree command is another useful tool that displays the directory structure in a tree-like format. The command also provides Linux folder size and files contained within them.
Syntax:
Explanation:
- The [options] are various command-line options that are used to achieve specific requirements.
- The [directory] is the directory for which you want to display the tree structure. If no directory is provided, the tree command will display the tree structure of the current directory by default.
Options:
Let's explore some commonly used options with the tree command:
- The -a or --all option displays all files and directories, including hidden ones (those whose names start with a dot). By default, hidden files and directories are not shown.
- The -L level or --max-depth=level option limits the depth of the directory tree to the specified level. For instance, -L 2 will display only the top-level directories and their immediate subdirectories.
- The -d or --dirs-only option displays only directories without files in the tree structure.
- The -f or --full-path option displays the absolute path of each file and directory in the tree structure.
- The -I pattern or --ignore pattern option is used to exclude files and directories that match the specified pattern from the tree structure.
- There are also the -h and -s options with properties similar to the du command.
Example:
The following example shows the steps to check the Linux folder size of the /home/hari/example directory using the tree command,
-
Navigate to the directory using the cd command.
-
Run the tree command with -a and -h options to get the size of all files and folders in the directory.
-
Use the -I option to exclude all the text files from the output.
-
Use the -s option to get only the total size of the directory.
Option - 3: Find the Size of a Linux Directory Using "ncdu" Command
The ncdu or the NCurses Disk Usage is a disk usage analyzer utility that provides an interactive way to explore disk usage within directories and file systems.
Syntax:
Options:
Let's explore some commonly used options with the ncdu command:
- The -q or --quiet option enables quiet mode, suppressing the display of messages and prompts.
- The -r option enables the read-only mode, preventing ncdu from modifying or deleting files and directories.
- The --exclude pattern option excludes files and directories that match the specified pattern from the analysis. We can also give a file of patterns on a separate line instead of pattern for this option.
- The --sort option allows you to specify a sorting criterion for the output. You can sort by size, file count(count), last modified time(mtime), and name(name). This option will sort the data in descending order.
- The -N or --no-scanfs option prevents ncdu from scanning file systems. It can be useful to speed up the analysis and avoid unnecessary scanning.
Example:
The following example shows the steps to check the Linux folder size of the /home/hari/example directory using the ncdu command,
-
Install the ncdu utility using the package manager.
-
Navigate to the directory using the cd command.
-
Use the following command to see the size of the directory.
This will open a new window where the size of each file, folder, and total size will be displayed. You can press q to quit this window.
-
Use the following command to sort the list of directories and files based on their size in descending order.
-
Use the following command to exclude text files in the calculating size of the directory.
The excluded files will be marked by a red arrow and will not be considered for space calculation.
Conclusion
- Directories in Linux are used to store files and organize storage.
- Knowing the sizes of directories helps manage disk space efficiently.
- The du command is used to get the Linux folder size by calculating the total size of files and directories within it.
- The tree command provides a visual representation of the directory structure and sizes, allowing for a hierarchical view of disk usage.
- The ncdu command provides multiple features and an interactive way to analyze disk usage.