This command checks the size of each file in the list and outputs the total in human readable format.
EDIT 07-02-2023: See the commands below. This (original) suggestion of mine parses the output from ls, which is prone to cause problems.
while read line; do ls -l "$line"; done < filelist.txt | awk '{total+=$5} END {print total}' | awk '{ split( "bytes KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "Total: %.2f %s", $1, v[s] }'
It uses only ls, awk and printf, which are likely to exist on the most basic of systems. It avoids the need to use xargs as the read command takes its input one line at a time from the list of files.
It works as follows. It reads each line of the file containing the list (filelist.txt in my example here) and pipes the output into awk, which reads only field 5 ($5) and keeps adding to the total for each file. The ls command with the -l switch provides us with the file size in bytes, so this is fed into a second awk which is a function to repeatedly divide the bytes by 1024 to convert it to KB, MB, GB, TB and even PB if you have that much disk space! It will print whatever is appropriate, ie KB, GB etc to 2 decimal places. If you want 3 decimal places then change the 2 to a 3 in the printf command at the end, viz
printf "Total: %.3f %s", $1, v[s]...
Asked almost 11 years ago, last activity 6 years ago! Well, if anyone finds this, it does meet the OPs requirements and maybe it will help someone.
EDIT 07-02-2023:
As pointed out by @doneal24, we should not be parsing the output from ls
. So we can simply read each line of the list of files, filelist.txt, and run du -b
against each one. We then use awk
to count the total bytes, and then again to convert it into human readable format. We do not need to pipe through xargs
as the structure of the commands is that the filenames are presented singularly from the read
command, which takes its input from the list one at a time.
while read line; do du -b "$line"; done < filelist.txt | awk '{i+=$1} END {print i}' | awk '{ split( "bytes KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "Total: %.2f %s", $1, v[s] }'
I believe this is the answer the OP wanted - it provides the total size of the files in the list in human readable format.
Once again, thanks to @doneal24 for the input. It proves how, even answering a post over 10 years old, we continue to learn and help others. Teamwork!