Imagine your Linux system as a huge storage area divided into sections called directories. As you use your system and add more files, these directories get bigger. In extreme cases, huge directory sizes can slow down system operations because of the time and resources required to parse these directories.
Knowing how to find the size of directories is essential to keeping your Linux system running smoothly and ensuring you have enough disk space.
In this comprehensive tutorial, we’ll cover the basic commands for determining how much space directories take up on the system storage.
Table Of Contents
- 3 Easy Methods to Get Linux Directory Size
3 Easy Methods to Get Linux Directory Size
Let’s explore the 3 easy methods you can use to get the directory size in Linux. But before that, make sure you fulfill the following prerequisites:
- A system running a mainstream Linux distribution
- Access to a command line or terminal
- A user account with root privileges.
Method #1: Use the du Command
The du (short for disk usage) command tells you how much space directories consume. It is already included in the core Linux utilities and available in all mainstream distributions.
Run the following command in the terminal to check the size of the current directory.
# sudo du
The output of the command shows all files in the current directory with their size in kilobytes (KB). Usually, these numbers are too huge to comprehend. You can include the -h flag to improve the readability of file sizes.
# sudo du -h
You can pass the path of a directory to the du command to see the size details.
# du -h /var
Note that if your account name doesn’t have access permission to a file, you’ll see an error in the results:
To resolve this error, simply use sudo with the du command to get access to the directory and its contents:
# sudo du -h /var
You can use the -c flag to view the total disk usage of a specific directory:
#sudo du -c /var
Now, let’s combine the -c and the -h flags in the following format:
# sudo du -hc /var
You’ll get the same result here as above.
By default, the du command traverses the entire directory. You can imagine the time required to process a multi-level directory. You can restrict the scan to a specific subdirectory level using the max-depth option. For instance, use –max-depth=0 to scan only the top directory’s level:
# sudo du -hc --max-depth=0 /var
Change –max-depth=1 to display only the top directory and the first level of subdirectories:
# sudo du -hc --max-depth=1 /var
You can view all available flags and options by entering the following command in the terminal:
# man du
You can use this command without sudo, as it doesn’t require root privileges to display manual documentation.
Method #2: Use the tree command
The tree command shows a graphical representation of the directories. It indicates directories and files using colors and shows the connection between the subdirectories and files with lines.
The tree is very versatile, and you can use several flags to modify the output of the command. For instance, use the following command to see the subdirectories’ sizes in the current directory in a human-readable format:
# tree -d -h
You can also pass a directory path to the tree command to view the structure of the directory:
# tree /var
You can access the details and available options for the tree command by entering:
# man tree
Important: In some Linux distributions, the tree command is unavailable by default. You can install it with the package manager of your distribution.
For instance, you can install it on Ubuntu (and other Debian-based distributions) by running the following command:
# sudo apt-get install tree
Method #3: Use the ncdu Command
The ncdu (NCurses Disk Usage) tool is similar to the tree command in the level of details of the command’s output.
The utility displays the disk usage in detail. The typical usage is:
You’ll see the files and directories, their sizes in KB (by default), and a column of # indicating their relative sizes.
You can easily navigate the output with the Up and Down arrows to select a line. Press the Right arrow to move forward in the directory and the Left arrow to return.
Like the previous commands, you can pass the path to a directory to the ncdu command to view its contents and sizes.
# ncdu /var
Since ncdu is not available in all distributions, you might need to install it if it’s not available on your system. If you’re using a Debian-based distro, run the following command:
# sudo apt-get install ncdu
Knowing how to determine the size of files and folders in Linux is essential for keeping your server running well. In this guide, we discussed du, tree, and ncdu as three simple ways of getting the size of a directory.
Understanding things like readable sizes, sorting, and how folders work helps you manage your computer better. This info lets you keep your computer organized, find big folders, and make smart choices regarding space usage.
If you’re looking for a robust server for your Linux projects, RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Q. What command can I use to find the size of a directory in Linux?
The du command is used to get the size of a directory in Linux. For example, du -sh directory_name provides the size in a human-readable format.
Q. Can I find the size of a directory and its subdirectories?
The du command is recursive by default, so running du -sh directory_name includes the sizes of the directory and its subdirectories.
Q. How can I list the sizes of all directories in a particular location?
You can use du -h –max-depth=1 /path/to/parent_directory to list the sizes of all immediate subdirectories in the specified location.
Q. Are there options to sort the directory sizes?
You can use the –max-depth option with the sort command to sort the directory sizes. For example, du -h –max-depth=1 | sort -h.
Q. Can I find the sizes of directories in a remote Linux server?
You can use SSH to access a remote server and run the du command. For instance, ssh user@remote_server ‘du -h directory_name’.
Q. What does the size output in human-readable format mean?
The human-readable format (e.g., KB, MB, GB) in the du command output makes it easier for users to understand the sizes of directories without interpreting raw byte counts.
Q. Can specific directories be excluded from the size calculation?
You can use the –exclude option with du to exclude specific directories from the size calculation. For example, du -h –exclude=exclude_directory.
Q. How often should I check the size of directories on my Linux system?
Checking directories’ sizes is beneficial for managing disk space efficiently. Consider incorporating it into routine system maintenance tasks.
Q. Are there graphical tools available for visualizing directory sizes in Linux?
Yes, graphical tools like Baobab and QDirStat provide visual representations of directory sizes on Linux.
Q. Can the du command be used in scripts for automation?
The du command is script-friendly and can be integrated into scripts for automating directory size-related tasks.