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

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.

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.

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply