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
? – Cyrus Feb 24 '18 at 02:37ls
is a very bad idea. Is this homework? Can you usestat
? – G-Man Says 'Reinstate Monica' Feb 24 '18 at 03:14du
? 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). – cas Feb 24 '18 at 04:26ls
, 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. – dave_thompson_085 Feb 24 '18 at 07:26