• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

TinyGrab

Your Trusted Source for Tech, Finance & Brand Advice

  • Personal Finance
  • Tech & Social
  • Brands
  • Terms of Use
  • Privacy Policy
  • Get In Touch
  • About Us
Home » How to get a folder size in Linux?

How to get a folder size in Linux?

May 29, 2025 by TinyGrab Team Leave a Comment

Table of Contents

Toggle
  • How to Get a Folder Size in Linux: A Deep Dive for Power Users
    • Diving Deeper into du: Unveiling its Potential
      • Understanding the Options
      • Practical Examples: Putting it all Together
    • Beyond du: Alternative Approaches
    • Frequently Asked Questions (FAQs)

How to Get a Folder Size in Linux: A Deep Dive for Power Users

Want to know the precise size of a folder in Linux? You’ve come to the right place! This article will equip you with the knowledge and techniques to effortlessly determine folder sizes using the command line. Let’s get started.

How to get a folder size in Linux? The most reliable and commonly used command is du -sh <folder_path>. This command calculates the disk usage of the specified folder (<folder_path>) and presents the result in a human-readable format (-h) with a summary (-s). For example, du -sh /home/user/documents will display the total size of the “documents” folder.

Diving Deeper into du: Unveiling its Potential

The du command, short for “disk usage,” is a powerful utility in Linux for estimating file space usage. While the basic command du -sh is incredibly useful, it’s just the tip of the iceberg. Let’s explore some advanced options to unlock its full potential.

Understanding the Options

  • -s (summary): This option tells du to display only the total size of the specified folder(s), rather than listing the size of each individual file and subdirectory. It’s essential for getting a quick overview.

  • -h (human-readable): This option transforms the output into a more user-friendly format, displaying sizes in kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), etc. Without -h, the sizes are displayed in blocks, which can be less intuitive.

  • -k (kilobytes): Forces du to display sizes in kilobytes. Can be useful for scripts that require consistent output units.

  • -m (megabytes): Forces du to display sizes in megabytes. Similar to -k, but for megabytes.

  • -g (gigabytes): Forces du to display sizes in gigabytes. Useful for very large folders.

  • -b (bytes): Displays the size in bytes. The most accurate representation, but often the least practical for humans.

  • -c (total): Displays a grand total of all specified directories. For example, du -sch folder1 folder2 will show the sizes of folder1 and folder2, as well as the combined size of both.

  • --exclude=PATTERN: Excludes files or directories matching the specified pattern from the calculation. This is incredibly useful for ignoring specific types of files or large caches. For example, du -sh --exclude="*.tmp" /home/user will exclude all files ending with “.tmp”.

  • -d (depth): Limits the depth to which du descends into subdirectories. du -h -d 1 /home/user will display the size of /home/user and its immediate subdirectories, but not their children.

Practical Examples: Putting it all Together

Let’s look at some real-world examples to see how these options work in practice.

  1. Getting the size of a folder in megabytes:

    du -sm /var/log 
  2. Finding the size of a folder, excluding a specific subdirectory:

    du -sh --exclude="cache" /home/user 
  3. Calculating the total size of multiple folders:

    du -sch /home/user/documents /var/www/html 
  4. Displaying the size of a folder and its immediate subfolders:

    du -h -d 1 /opt 

Beyond du: Alternative Approaches

While du is the workhorse, there are alternative methods to obtain folder sizes in Linux, each with its own strengths and weaknesses.

  • ls -lhd <folder_path>: This command lists the contents of a directory (-l) in human-readable format (-h) and includes the size of the directory itself if you specify the -d option. However, it only shows the size of the directory entry, not the total size of all files and subdirectories within it. It’s useful for quickly checking the size of empty directories or for comparing directory entry sizes.

  • find <folder_path> -printf '%sn' | awk '{total += $1} END {print total}': This command uses find to list the size of each file within the specified folder, pipes the output to awk to sum the sizes, and then prints the total. It’s a more roundabout approach than du and can be slower for large directories.

  • Graphical File Managers: Most Linux desktop environments include graphical file managers (e.g., Nautilus in GNOME, Dolphin in KDE) that can display folder sizes. Simply right-click on the folder and select “Properties” (or a similar option). This is the most user-friendly option, but it’s not suitable for scripting or remote access.

Frequently Asked Questions (FAQs)

Here are some frequently asked questions about obtaining folder sizes in Linux, along with detailed answers.

1. Why is the size reported by du different from the size reported by my file manager?

This discrepancy often arises due to disk allocation units (blocks). du reports the actual disk space used, which may be larger than the sum of the file sizes if files occupy partial blocks. File managers sometimes provide more generalized or simplified sizes. Additionally, file managers might not always include hidden files and directories in their calculations by default.

2. How can I find the largest folders in my system?

You can use a combination of du and sort to achieve this. The command du -sh /*

sort -rh

3. How do I exclude multiple patterns when using du?

You can use multiple --exclude options: du -sh --exclude="*.tmp" --exclude="cache" --exclude=".git" /home/user. Each --exclude will filter out a separate pattern.

4. Can I use du to find empty directories?

Yes, you can combine du with find for this purpose. The command find . -type d -empty will locate all empty directories in the current directory.

5. How can I make du faster for very large directories?

Using the --one-file-system option can speed up du if your directories span multiple file systems. This prevents du from traversing into mounted file systems, which can significantly reduce the processing time. Additionally, using a specific unit like -m (megabytes) can be faster than -h.

6. How do I use du in a script?

When using du in a script, it's best to use a specific unit (e.g., -k, -m, -g) and avoid the -h option, as the human-readable output is not easily parsable. Then, you can use tools like awk or sed to extract the size from the output.

7. Does du account for hard links?

Yes, by default, du counts the space used by each hard link separately. This means that the total reported size might be larger than the actual disk space used. You can use the -l option to count hard links only once.

8. How do I use du to find folders larger than a specific size?

This requires a combination of find and du. For example: find . -type d -exec du -hs {} ;

grep 'G'

9. What is the difference between apparent size and disk usage?

Apparent size refers to the actual size of the file if you were to copy it byte-for-byte. Disk usage, on the other hand, considers the space occupied on the disk, including overhead like block allocation. Sparse files, for example, might have a large apparent size but a smaller disk usage.

10. Can I use wildcards with the folder path in du?

Yes, you can use wildcards. For example, du -sh /home/user/doc* will calculate the size of all directories and files starting with "doc" in /home/user.

11. How do I get the folder size in a remote Linux server via SSH?

Simply establish an SSH connection to the server and run the du command as you would locally. For example: ssh user@remote_server "du -sh /path/to/folder"

12. Is there a GUI alternative to du for disk usage analysis?

Yes, there are several graphical tools available, such as Baobab (Disk Usage Analyzer), which provides a visual representation of disk space usage. You can usually install it using your distribution's package manager (e.g., sudo apt install baobab on Debian/Ubuntu).

By mastering the du command and understanding its options, you'll have a powerful tool at your disposal for analyzing disk usage in Linux. Remember to experiment with the different options and adapt them to your specific needs. Happy exploring!

Filed Under: Tech & Social

Previous Post: « How long is Disneyland decorated for Christmas?
Next Post: Can I restore a deleted WhatsApp account? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

NICE TO MEET YOU!

Welcome to TinyGrab! We are your trusted source of information, providing frequently asked questions (FAQs), guides, and helpful tips about technology, finance, and popular US brands. Learn more.

Copyright © 2025 · Tiny Grab