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

How we used Flask and 12-factor charms to simplify Canonical.com development

Our latest Canonical website rebrand did not just bring the new Vanilla-based frontend, it also…

5 hours ago

Web Engineering: Hack Week 2024

At Canonical, the work of our teams is strongly embedded in the open source principles…

1 day ago

Ubuntu Weekly Newsletter Issue 873

Welcome to the Ubuntu Weekly Newsletter, Issue 873 for the week of December 29, 2024…

3 days ago

How to resolve WiFi Issues on Ubuntu 24.04

Have WiFi troubles on your Ubuntu 24.04 system? Don’t worry, you’re not alone. WiFi problems…

3 days ago

Remembering and thanking Steve Langasek

The following is a post from Mark Shuttleworth on the Ubuntu Discourse instance. For more…

3 days ago

How to Change Your Prompt in Bash Shell in Ubuntu

I don’t like my prompt, i want to change it. it has my username and…

3 days ago