Categories: TutorialsUbuntu

How to Find the total size of all files in a directory in Ubuntu

Finding the total size of all files in a directory in Ubuntu can be done in several ways. Here are 6 methods, ranging from simple to more advanced.

Use the du command:

This is the simplest method, and the one most people are familiar with. To use du, open the terminal and type:

du -sh /path/to/directory

Sponsored

Replace /path/to/directory with the actual path to the directory you want to find the size of. The -sh option tells du to display the result in “human-readable” format, which means it’ll show you the size in MB or GB instead of bytes.

Read: How to display files sizes in MB in Linux/Ubuntu

Use the find command with awk:

Here’s how to use this method:

find /path/to/directory -type f -ls | awk ‘{sum+=$7} END {print sum/1024/1024 ” MB”}’

This method uses the find command to search for all files (-type f) in the specified directory (/path/to/directory), then pipes the output to awk to calculate the total size. awk adds up the size of each file (represented by $7) and stores the result in a variable called sum. The final output is in MB.

Use the ls command with awk:

Here’s the command:

ls -lR /path/to/directory | awk ‘{sum+=$5} END {print sum/1024/1024 ” MB”}’

This method uses ls -lR to list the details of all files in the specified directory (/path/to/directory), including their sizes. The output is then piped to awk, which adds up the size of each file (represented by $5) and stores the result in a variable called sum. The final output is in MB.

Read: How to find the largest files on Linux

Use the stat command with awk:

Here’s the command:

find /path/to/directory -type f -exec stat -c%s {} + | awk ‘{sum+=$1} END {print sum/1024/1024 ” MB”}’

This method uses find to search for all files (-type f) in the specified directory (/path/to/directory), then -exec to run the stat command on each file. The -c%s option tells stat to display only the size of each file in bytes. The output is then piped to awk, which adds up the size of each file (represented by $1) and stores the result in a variable called sum. The final output is in MB.

Sponsored

Use the ls command with wc and awk:

Here’s the command:

find /path/to/directory -type f -exec ls -l {} + | awk ‘{sum+=$5} END {print sum/1024/1024 ” MB”}’

This method uses find to search for all files (-type f) in the specified directory (/path/to/directory), then -exec to run the ls -l command on each file. The output is then piped to awk, which adds up the size of each file (represented by $5) and stores the result in a variable called sum. The final output is in MB.

Read: How to fix high memory usage in Linux

Use the ncdu command:

This method is the most user-friendly and interactive of all. To use ncdu, open the terminal and type:

ncdu /path/to/directory

Linux ncdu command

Read: How to find the size of a file or directory on Linux using du and ncdu commands

If it is not installed on your system, go ahead and install it using the command:

sudo apt install ncdu

Install ncdu

Replace /path/to/directory with the actual path to the directory you want to find the size of. The command ncdu will display a list of all files and directories in the specified directory, along with their sizes, in a convenient, easy-to-read format. You can navigate the list using the arrow keys and see the size of each sub-directory by pressing Enter. To exit ncdu, press q.

Read: How to display the contents of a text file on the terminal in Linux/Ubuntu

Conclusion

If you’re using Ubuntu and need to find the total size of all files in a directory, there are plenty of ways to do it! You can use the du command, which is pretty easy to use, or the find command with awk, which is a bit more advanced. The ls command with awk and the stat command with awk are also options. And if you’re looking for something really user-friendly, try the ncdu command. With all these choices, anyone can figure out the total size of their files in no time, no matter how tech-savvy they are.

 

The post How to Find the total size of all files in a directory in Ubuntu appeared first on net2.

Ubuntu Server Admin

Recent Posts

Cut data center energy costs with bare metal automation

Data centers are popping up everywhere. With the rapid growth of AI, cloud services, streaming…

22 hours ago

Build the future of *craft: announcing Starcraft Bounties!

Our commitment to building a thriving open source community is stronger than ever. We believe…

22 hours ago

NodeJS 18 LTS EOL extended from April 2025 to May 2032 on Ubuntu

The clock was ticking: Node.js 18’s upstream End of Life (EOL) The OpenJS Foundation is…

22 hours ago

Native integration now available for Pure Storage and Canonical LXD

June 25th, 2025 – Canonical, the company behind Ubuntu, and Pure Storage, the IT pioneer…

2 days ago

Revolutionizing Web Page Creation: How Structured Content is Slashing Design and Development Time

Co-authored with Julie Muzina A year ago, during our Madrid Engineering Sprint, we challenged ourselves…

3 days ago

Ubuntu Weekly Newsletter Issue 897

Welcome to the Ubuntu Weekly Newsletter, Issue 897 for the week of June 15 –…

4 days ago