Managing disk space is essential for system administrators and everyday Linux users. Knowing how to check the size of a directory in Linux allows you to identify large folders, clean up unnecessary files, and optimize storage. In this guide, we will explore various commands to check the size of directories and subdirectories in Linux, covering different distributions like Ubuntu and CentOS.

 

Understanding Directory Sizes in Linux

Linux does not display directory sizes directly because directories function as containers for files rather than storing data themselves. Instead, Linux calculates a directory’s size based on the total size of all files within it, including subdirectories.

Using commands like du, ls, and find, users can analyze directory sizes efficiently. The du command provides a complete breakdown of disk usage, while ls helps check individual file sizes. For those using Ubuntu or CentOS, tools like Disk Usage Analyzer (baobab) and ncdu offer visual and interactive ways to inspect disk usage.

If you’re working with Linux regularly, learning how to navigate directories and manage disk space efficiently is essential. If you're new to Linux, check out our guide on Basic Linux Commands to improve your command-line skills.

 


 

1. How to Check the Size of a Directory in Linux Using the du Command

The du (Disk Usage) command is the most common way to check the size of a directory in Linux CentOS. It calculates the disk space used by a folder and its subdirectories.

Basic Command to Check Directory Size

du -sh /path/to/directory

Explanation:

  • -s (summary): Shows only the total size of the directory.

  • -h (human-readable): Displays sizes in KB, MB, or GB instead of bytes.

For example, to check the size of the /home/user/Documents folder:

du -sh /home/user/Documents

 

Check the Linux Size of Directory and Subdirectories

To get a breakdown of all subdirectories within a folder:

du -h --max-depth=1 /home/user/Documents

  • --max-depth=1: Limits the depth of directory scanning to one level.

This will list the sizes of all folders inside /home/user/Documents without going deeper.

 

Check the Size of All Directories and Files Recursively

To get a detailed recursive listing of all directories and files inside a folder:

du -ah /home/user/Documents

This command includes individual file sizes along with directory sizes.

 

Sort Directories by Size

If you want to find the largest directories inside a folder, sort them by size:

du -ah /home/user/Documents | sort -rh | head -10

This lists the top 10 largest files and directories within the specified folder.

 


 

2. Check the Size of a Directory in Linux Command Line Using ls

While du is preferred for directory size calculations, the ls command can help analyze individual file sizes.

Check the Size of All Files in a Directory

ls -lh /home/user/Documents

  • -l: Displays detailed file information.

  • -h: Shows human-readable sizes (KB, MB, GB).

However, ls does not show directory sizes, only individual file sizes.

 

Sort Files by Size

To display files from largest to smallest:

ls -lhS /home/user/Documents

  • -S: Sorts files by size.

This helps identify which files take up the most space in a directory.

 


 

3. Find the Total Size of All Directories in Linux

If you want to check the size of all directories in Linux, you can use du along with the grep command to filter results.

du -h --max-depth=1 /home/user | grep -E 'G|M'

This will list only directories that are at least 1MB or larger, making it easier to identify large folders.

 


 

4. Checking the Size of a Directory in Ubuntu and CentOS

Ubuntu

The same du and ls commands work on Ubuntu, but if you want a GUI-based solution, use Disk Usage Analyzer (baobab).

Install it with:

sudo apt install baobab

Run:

baobab

This will open a graphical interface to analyze disk usage visually.

 

CentOS

On CentOS, du and ls work as usual, but you may also install ncdu, a text-based disk usage analyzer:

sudo yum install ncdu -y

Run:

ncdu

This provides an interactive way to navigate large directories.For advanced directory searches, refer to our guide on How to find a file in Linux systems.

 


 

5. Checking Directory Sizes in GB

To check a directory's size in gigabytes:

du -sh --block-size=G /home/user/Documents

Or, use:

du -BG /home/user/Documents

This forces the output to be in GB, making it easier to read.

 


 

6. Unix Command to Check Folder Size in GB

For Unix-based systems (including macOS), the du command works similarly:

du -sh /Users/username/Documents

To ensure output in gigabytes:

du -sh --block-size=G /Users/username/Documents

 


 

7. Linux Check Disk Space by Folder

To get a summary of disk space usage per directory:

df -h

If you want to check disk usage within a specific folder:

du -sh *

This lists the sizes of all files and folders in the current directory.

 


 

8. Check the Size of a Directory Using find

If you need to calculate directory sizes for specific file types, use find:

find /home/user/Documents -type f -name "*.log" -exec du -ch {} + | grep total$

This command calculates the total size of all .log files inside a directory.


 

9. Automate Directory Size Checking with a Script

If you frequently check folder sizes, automate the process with a script:

#!/bin/bash

echo "Enter directory path:"

read dir

du -sh "$dir"

Save the script as check_size.sh, then make it executable:

chmod +x check_size.sh

Run it:

./check_size.sh

This prompts the user to enter a directory path and displays its size.


 

Conclusion

Learning how to check the size of a directory in Linux Ubuntu is crucial for managing disk space and maintaining system performance. Whether you need to check the total size of a directory, analyze individual files, or sort directories by size, Linux provides powerful tools to get the job done.

The du command is the most effective way to check folder sizes, while ls helps list file sizes within a directory. For advanced users, combining find with du enables filtering file types and calculating specific storage usage.

For those managing multiple files and directories, consider using Linux VPS Hosting for optimized performance and better storage management. Learn more about Linux VPS Hosting and how it can enhance your workflow.