Linux

How to Check Directory Size in Linux

Knowing how to check directory sizes in Linux is essential for managing disk space and keeping your system organized. Linux provides several built-in tools and commands to analyze storage usage and identify which directories or files are consuming the most space. Whether you are managing a small workstation or a large server, understanding these commands can help optimize performance and prevent storage issues.

1. Using the du Command

The du (Disk Usage) command is the most common and powerful tool to check directory sizes. It reports the disk space used by files and directories.

To check the size of a specific directory, run:

du -sh /path/to/directory

Options explained:

  • -s: Displays only the total size.
  • -h: Shows human-readable output (KB, MB, GB).

Example:

du -sh /home/user/Documents

This command displays the total size of the “Documents” folder in an easy-to-read format.

To list the sizes of all subdirectories:

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

This displays the size of each folder within /home/user, helping you locate large directories quickly.

2. Checking Total Disk Usage

If you want to see how much space is being used on the entire system, use:

df -h

The df (Disk Filesystem) command shows mounted filesystems, their total size, used space, and available space. It’s ideal for an overview of disk capacity and helps prevent running out of storage on critical partitions.

3. Sorting Directories by Size

To find the largest directories on your system, combine commands:

du -h /path | sort -hr | head -10

This lists the top 10 largest directories in descending order. It’s a quick way to pinpoint which folders are consuming the most disk space.

4. Using the ncdu Command

For a more interactive way to analyze directory sizes, install ncdu (NCurses Disk Usage):

sudo apt install ncdu

Then run:

ncdu /path/to/directory

ncdu opens a graphical, text-based interface in the terminal, allowing you to navigate through directories and delete unnecessary files directly from the interface.

5. Checking Directory Size with ls and du Combination

You can combine ls and du to analyze directories more effectively:

ls -lh

This lists files with their sizes in human-readable format. To combine both commands for more detailed output:

du -ch /path/to/directory/*

This shows the size of each file and a cumulative total at the end.

6. Using find for Targeted Directory Analysis

If you want to find all files over a certain size, use:

find /path -type f -size +100M -exec ls -lh {} \;

This locates files larger than 100 MB. It’s useful for cleaning up old backups or large media files that consume space unnecessarily.

7. Automating Directory Size Checks

System administrators often automate disk checks using cron jobs. For example, create a cron task that logs directory sizes daily:

0 2 * * * du -sh /var/www >> /var/log/disk_usage.log

This command runs every night at 2 a.m. and saves size data to a log file for tracking.

8. Common Use Cases

  • Monitor web servers: Identify growing logs or cache directories in /var/www.
  • Check user directories: Manage space in /home for multiple users.
  • Analyze system directories: Find large backups or temporary files in /var and /tmp.

10. Conclusion

Efficient disk management is crucial for keeping your Linux system stable and fast. Commands like du, df, and ncdu help you analyze storage, identify large files, and clean up space when needed. By regularly checking directory sizes, you ensure that your system runs smoothly and storage usage stays under control.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

WhatsMyName App – Find Anyone Across 640+ Platforms

Overview WhatsMyName is a free, community-driven OSINT tool designed to identify where a username exists…

13 minutes ago

Analyzing Directory Size Linux Tools Explained

Managing disk usage is a crucial task for Linux users and administrators alike. Understanding which…

59 minutes ago

Understanding Disk Usage with du Command

Efficient disk space management is vital in Linux, especially for system administrators who manage servers…

1 hour ago

Essential Commands for Linux User Listing

Managing user accounts is a core responsibility for any Linux administrator. Whether you’re securing a…

1 hour ago

Command-Line Techniques for Listing Linux Users

Linux offers powerful command-line tools for system administrators to view and manage user accounts. Knowing…

23 hours ago

Exploring User Management in Linux Systems

User management is a critical aspect of Linux administration. Each user in a Linux system…

23 hours ago