I'm new to bash and I need to do a little script to sum all file sizes, excluding subdirectories. My first idea was to keep the columns when you do ls -l
. I cannot use grep, du or other advanced commands I've seen around here.
$9 corresponds to the 9th column where the name is shown.
$5 is the size of the file.
ls -l | awk '{if(-f $9) { total +=$5 } }; END { print total }
ls
?ls
is a very bad idea. Is this homework? Can you usestat
?du
? is this homework? (homework is OK here, but it's best to mention that it is because most people here prefer to give hints and point you in the right direction rather than give a complete answer if it's for homework).ls
, tests like-f name
work in the standard-and-traditional test-aka-[ utility, the similar ksh-bash-etc-only [[ builtin, and perl, but not awk.