Understanding how to check directory size in Linux is critical for managing storage space efficiently. Understanding this process is essential whether youâre assessing specific folder space or preventing storage issues.
This comprehensive guide covers commands and tools so you can easily calculate and analyze directory sizes in a Linux environment. We will guide you step-by-step through three methods: du
, ncdu
, and ls -la
. Theyâre all effective and each offers different benefits.
What is a Linux directory?
A Linux directory is a special type of file that functions as a container for storing files and subdirectories. It plays a key role in organizing the Linux file system by creating a hierarchical structure. This arrangement simplifies file management, making it easier to locate, access, and organize related files. Directories are fundamental components that help ensure smooth system operations by maintaining order and facilitating seamless file access in Linux environments.
#1 Get Linux directory size using the du command
Using the du command, you can easily determine a directoryâs size by displaying the disk space used by files and directories. The output can be customized to be presented in human-readable formats like kilobytes (KB), megabytes (MB), or gigabytes (GB).
Check the size of a specific directory in Linux
To get the size of a specific directory, open your terminal and type the following command:
du -sh /path/to/directory
In this command, replace /path/to/directory
with the actual path of the directory you want to assess. The -s
flag stands for âsummaryâ and will only display the total size of the specified directory. The -h
flag makes the output human-readable, showing sizes in a more understandable format.
Example: Here, we used the path /home/ubuntu/
, where ubuntu
is the name of our username directory. We used the du
command to retrieve an output of 32K for this directory, indicating a size of 32 KB.
Check the size of all directories in Linux
To get the size of all files and directories within the current directory, use the following command:
sudo du -h /path/to/directory
Example: In this instance, we again used the path /home/ubuntu/
, with ubuntu
representing our username directory. Using the command du -h
, we obtained an output listing all files and directories within that particular path.
#2 Get Linux directory size using ncdu
If youâre looking for a more interactive and feature-rich approach to exploring directory sizes, consider using the ncdu
(NCurses Disk Usage) tool. ncdu
provides a visual representation of disk usage and allows you to navigate through directories, view size details, and identify large files with ease.
For Debian or Ubuntu, use this command:
sudo apt-get install ncdu
Once installed, run ncdu
followed by the path to the directory you want to analyze:
ncdu /path/to/directory
This will launch the ncdu
interface, which shows a breakdown of file and subdirectory sizes. Use the arrow keys to navigate and explore various folders, and press q
to exit the tool.
Example: Hereâs a sample output of using the ncdu
command to analyze the home directory. Simply enter the ncdu
command and press Enter. The displayed output will look something like this:
#3 Get Linux directory size using 1s -1a
You can alternatively opt to use the ls
command to list the files and directories within a directory. The options -l
and -a
modify the default behavior of ls
as follows:
-l
(long listing format)- Displays the detailed information for each file and directory
- Shows file permissions, the number of links, owner, group, file size, the timestamp of the last modification, and the file/directory name
-a
(all files)- Instructs
ls
to include all files, including hidden files and directories - Includes hidden files on Linux that typically have names beginning with a
.
(dot)
- Instructs
ls -la
lists all files (including hidden ones) in long format, providing detailed information such as permissions, owner, group, size, and last modification time. This command is especially useful when you want to inspect file attributes or see hidden files and directories.
Example: When you enter ls -la
command and press Enter, you will see an output similar to this:
Each line includes:
- File type and permissions (e.g.,
drwxr-xr-x
):- The first character indicates the file type
-
for a regular filed
for a directoryl
for a symbolic link
- The next nine characters are permissions in groups of three (rwx):
r
= readw
= writex
= execute- Permissions are shown for three classes of users: owner, group, and others.
- The first character indicates the file type
- Number of links (e.g., 2):
- For regular files, this usually indicates the number of hard links
- For directories, it often reflects subdirectory links (e.g., the
.
and..
entries)
- Owner and group (e.g., user group)
- File size (e.g., 4096 or 1045 bytes)
- Modification date and time (e.g., Jan 7 09:34)
- File name (e.g.,
.bashrc
,notes.txt
,Documents
):- Files or directories that begin with a dot (
.
) are hidden (e.g.,.bashrc
)
- Files or directories that begin with a dot (
Conclusion
Thatâs it! You can now determine the size of a directory in Linux. Measuring directory sizes is a crucial skill for efficient storage management. Whether you choose the straightforward du
command, use the visual advantages of the ncdu
tool, or opt for the versatility of ls -la
, this expertise enhances your ability to uphold an organized and efficient Linux environment.
Looking to deploy Linux in the cloud? With Gcore Edge Cloud, you can choose from a wide range of pre-configured virtual machines suitable for Linux:
- Affordable shared compute resources starting from âŹ3.2 per month
- Deploy across 50+ cloud regions with dedicated servers for low-latency applications
- Secure apps and data with DDoS protection, WAF, and encryption at no additional cost