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

Install Apache Cassandra on Ubuntu 18.04: NoSQL Setup Guide

Apache Cassandra is a free, open-source NoSQL database designed for high availability and linear scalability with…

3 hours ago

Install Rocket.Chat on Ubuntu 18.04 with Nginx and Let’s Encrypt

Rocket.Chat is a free, open-source team communication platform built with the Meteor framework. It is a…

3 hours ago

Install MySQL on Ubuntu 18.04: Setup, Security, and Root Access

MySQL is the most popular open-source relational database management system. It is fast, reliable, and scales…

3 hours ago

Install Apache on Ubuntu 18.04: Web Server Setup and Config

Apache is the most widely used web server in the world. It is free, open-source, and…

3 hours ago

Install NetBeans IDE on Ubuntu 18.04 with Snap and OpenJDK 8

NetBeans is a free, open-source, cross-platform IDE developed by the Apache Software Foundation. It was one…

3 hours ago

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

5 days ago