Jan_Cho Jan_Cho - 1 month ago 7
Bash Question

Get total size of a list of files in UNIX

I want to run a

find
command that will find a certain list of files and then iterate through that list of files to run some operations. I also want to find the total size of all the files in that list.

I'd like to make the list of files FIRST, then do the other operations. Is there an easy way I can report just the total size of all the files in the list?

In essence I am trying to find a one-liner for the 'total_size' variable in the code snippet below:

#!/bin/bash
loc_to_look='/foo/bar/location'

file_list=$(find $loc_to_look -type f -name "*.dat" -size +100M)

total_size=???

echo 'total size of all files is: '$total_size

for file in $file_list; do
# do a bunch of operations
done

Answer

You should simply be able to pass $file_list to du:

du -ch $file_list | tail -1 | cut -f 1

du options:

  • -c display a total
  • -h human readable (i.e. 17M)

du will print an entry for each file, followed by the total (with -c), so we use tail -1 to trim to only the last line and cut -f 1 to trim that line to only the first column.

Comments