I have a directory called 'directory1' with files: file1, file2, file3, that have been passed by in a variable 'files'. Echo $files has the following output: file1 file2 file3. I want to have the sum of the sizes but the code below only works for only one file. How can I make it work for all the files?

ls -l ./$dir/$files | awk '{t+=$5}END{print t}'

  • ls -l ./$dir/$files itself is only going to give the file size of directory1/file1. Try it.
    – steve
    Dec 23, 2019 at 22:30
  • Can you provide more details on exactly what are the contents of the files variable? You also mention a dir variable that is not described before. Does it mean that files are not on the same folder? Also is it required to use awk to sum the sizes or can other tools be used too?
    – Zip
    Dec 23, 2019 at 22:38
  • @Zip thank you for answering. The contents of the files are simple texts. Yes dir is an argument. Yes all the files are in the same folder. It is preferred to use awk but other tools could be used to (however, for quite some reason the du command is not recognized by my linux distro). The ultimate goal of this project is to compare 2 files on their contents and sum up the sizes of the files that are only included in the first directory.
    – user25
    Dec 23, 2019 at 22:48
  • Thanks! But I did ask actually about the contents of the files variable, not the files themselves, like if they are full paths, separated by lines or just filenames separated by spaces, as examples. Usually we provide an example input as well as a desired output on such questions, so that the answer can be clear and straightforward. Could you add that to the question?
    – Zip
    Dec 23, 2019 at 22:57
  • yes of course so the files variable has the following content: file1 file2 file3 which are the names of the files separated by spaces
    – user25
    Dec 23, 2019 at 22:59

2 Answers 2


You could print the files sizes in bytes using a loop with stat and sum the output with awk:

for i in $files; do
  stat -c '%s' "$dir/$i"
done | awk '{t+=$1}END{print t}'
  • Thank you so much it's working!!
    – user25
    Dec 23, 2019 at 23:12
  • Why change at all? Why not just for i in "$files"; do stat -c '%s' "$dir/$i"; done?
    – terdon
    Dec 24, 2019 at 12:39
  • Correct, stat -c '%s' "$dir/$i" is easier. But $files should be unquoted if the filenames are space separated. Thanks @terdon.
    – Freddy
    Dec 24, 2019 at 12:49
  • Ah, yes indeed. Force of habit :)
    – terdon
    Dec 24, 2019 at 13:12
  • Use set -f to turn off filename globbing and set IFS=' ' to avoid splitting the filenames on anything other than spaces. How you would handle filenames with spaces, I don't know.
    – Kusalananda
    Dec 25, 2019 at 7:21

You can do it various ways :

First change into the directory and from there run the commands + turnoff glob expansion since the $file is uncovered.

cd "$dir" ;set -f

stat -c '%s' $files | paste -sd+ | bc -l

du  --apparent-size -abc $files | tail -n 1

perl -E '$s += -s for @ARGV;say $s' $files

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .