+2 votes
in Operating Systems by (3.7k points)

I want to compute the sum of size of all files present in a folder or directory. There may be several other folders in a folder, but I just want to exclude those recursive folders. I want to use awk/gawk instead of du.

1 Answer

0 votes
by (12.7k points)

Navigate to your desired folder and try to run one of the following awk/gawk commands:

$ ls -l | awk 'BEGIN{size=0}{if ($1 !~ /drw/) size+=$5} END {print size}'

$ ls -l | gawk 'BEGIN{size=0}{if ($1 !~ /drw/) size+=$5} END {print size}'

$ ls -l | gawk '{if ($1 !~ /drw/) size+=$5} END {print size}'

$ ls -l | awk '{if ($1 !~ /drw/) size+=$5} END {print size}'

If you do not get the correct sum, just run 'ls -l' and check if 5th position has size or not. If size is present in another position, change $5 to that position.

...