Often you may want to use Bash to get the total size of a list of files.
You can use the following syntax to do so:
du -ch $my_files | tail -1 | cut -f 1
This particular example will return the total size of the files included in the list of files named $my_files.
Here is what the varous commands do:
- du -ch $my_files: Obtain disk usage information for total (c) size in human readable (h) format
- tail -1: Only return last line in output, which contains the total size
- cut -f 1: Only return the first field from the last line in output, which contains number of total size
The following example shows how to use this syntax in practice.
Example: How to Get Total Size of List of Files in Bash
Suppose we use the ls -l statement to first view all files in our current directory along with their size:

Now suppose that we would only like to find the total size of the files that have a .xlsx extension.
We can use the following syntax to first obtain a list of files that have a .xlsx extension:
my_files=$(find . -name "*.xlsx")
We can then use the following syntax to return the total size of the files in this list:
du -ch $my_files | tail -1 | cut -f 1
The following screenshot shows how to use this syntax in practice:

From the output we can see that the total size of files that have a .xlsx extension in the current directory is 24k.
Note: Feel free to replace “.xlsx” with any other pattern that you’d like to filter your files based on.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to List All Files Not Matching a Pattern
Bash: How to Count Number of Files in Each Directory
Bash: How to Find Files Newer than Specific Date or Time
