How to find the total size of all files in a directory in ubuntu

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

How to find the total size of all files in a directory in ubuntu 1

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”}’

How to find the total size of all files in a directory in ubuntu 2

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.

See also  Time to prepare for Ubuntu 18.04 LTS End of Standard Support on 31 May 2023

Use the ls command with awk:

Here’s the command:

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

How to find the total size of all files in a directory in ubuntu 3

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”}’

How to find the total size of all files in a directory in ubuntu 4

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.

See also  Setup SSH Keys on Ubuntu 18.04

Use the ls command with wc and awk:

Here’s the command:

Sponsored

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

How to find the total size of all files in a directory in ubuntu 5

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

How to find the total size of all files in a directory in ubuntu 6

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

How to find the total size of all files in a directory in ubuntu 7

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.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply