Jan_Cho Jan_Cho - 2 months ago 14
Bash Question

Get total size of a list of files in UNIX

I want to run a

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:


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


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

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


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.