Using ls to list directories and their total sizes

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Using ls to list directories and their total sizes

šŸ“Title: "How to Use ls to List Directories and Their Total Sizes in Unix"

šŸ’”Introduction: Hey there, fellow techies! šŸ–ļø Have you ever wondered if it's possible to use the ls command in Unix to list the total size of a sub-directory and all its contents? šŸ¤” Well, you've come to the right place! In this blog post, we'll tackle this intriguing question and provide you with some easy and effective solutions. So, let's dive in, shall we? šŸ’»šŸš€

🧠Understanding the Problem: The default output of the ls command typically displays the size of directories as 4.0K. However, this only refers to the size of the directory file itself, not the total size of its contents. Many developers and sysadmins crave a way to overcome this limitation and display the comprehensive size of a sub-directory, including all the files and sub-directories it contains. Thankfully, there are a few simple tricks to achieve this!

šŸ”§Solution 1: du and ls: One way to obtain the total size of a directory and its contents is by combining the power of du and ls commands. Here's how you can do it:

  1. Open your terminal.

  2. Navigate to the parent directory where the sub-directory you want to analyze is located.

  3. Type the following command: du -shc sub-directory/* | tail -1.

  4. Voila! The total size, including all the files and sub-directories within the specified sub-directory, will be displayed in human-readable format. šŸŒ

āž”ļøExample: Let's say you want to find the total size of the "documents" sub-directory. Here's what the command would look like: du -shc documents/* | tail -1. Running this will give you an output like: 2.1G total. Now you have the complete size of the "documents" directory and all its contents. Amazing, right? šŸŽ‰

šŸ”§Solution 2: du Only: If you prefer to use only the du command and avoid combining it with ls, fret not! There's another way to achieve the same result:

  1. Open your terminal.

  2. Navigate to the parent directory where the sub-directory you want to analyze is located.

  3. Type the following command: du -sh sub-directory.

  4. You're done! The total size of the sub-directory and its contents will be displayed in a human-readable format. Easy peasy! 🌟

āž”ļøExample: Let's imagine you want to check the total size of the "images" sub-directory. Simply use the command: du -sh images. This will provide you with an output like: 1.5G images. Boom! You've successfully obtained the total size without any complications. Awesome, isn't it? šŸ’„

šŸ“£Call-to-Action: Now that you know how to utilize the ls command to list directories and their total sizes like a pro, it's time to put your newfound knowledge to the test! Go ahead and try it out on your own directories. Feel free to share your experiences or any other cool tricks you've discovered in the comments section below. Let's geek out together! šŸ¤“šŸ’¬

That's a wrap, my tech-savvy friends! We've explored how to conquer the challenge of using ls to list directories and display their total sizes accurately. It's all about unleashing the power of du and a bit of creativity! Now you can impress your colleagues and save time when investigating disk space usage. Until next time, happy coding! šŸŽ‰šŸ‘©ā€šŸ’»šŸ‘Øā€šŸ’»

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my