With GNU find
, you can do:
find a.txt b.txt c.txt -prune -printf '%s\n' | paste -sd + - | bc
That gives the size as reported by ls -l
or the stat()
system call. For non-regular file types (like fifo, device, symlink), depending on the system, that may not necessarily give you the number of bytes that would be read from them if they were. See there for more options for those.
You could do:
cat a.txt b.txt c.txt | wc -c
for that, but that's not something you'd want to do for fifos or some device files like /dev/zero
or /dev/random
.
You can add the -L
option to the find
command to resolve symlinks and get the size of the target instead.
POSIXly, the only command that can get you the file size as returned by the lstat()
system call is ls
unfortunately.
ls -l
doesn't return the size for block devices. It is very difficult to parse its output reliably, and can only be done in a foolproof way (for compliant implementations and for non-device files) for one file at a time:
getsize() {
LC_ALL=C ls -nd -- "$1" | awk '
{
if (/^[cb]/) print 0
else print $5
exit
}
END {exit (!NR)}'
}
(here assuming a size of 0 for device files which is always true on Linux, but not on all systems).
Then you can do:
sum=0
for file in a b c; do
sum=$((sum + $(getsize "$file")))
done
echo "$sum"
cat
works on binary data, no problem. Utilities that interpret the data as text won't work though. – Kusalananda Oct 02 '17 at 11:58