0

Im very new to bash, and I have been given the task to write a bash script incorporating a loop that counts the byte size of all files in the current directory.

I want to use du but Ive been told he wants to see wc as the main command used.

As to how to make a loop accomplish these things, I'm unsure. Can anyone give me an example or point me in the right direction?

2 Answers2

0
for fname in *; do
    your-way-to-display-size-of-file "$fname"
done
StenSoft
  • 9,369
  • 25
  • 30
0

You need to gather several bits of bash know-how

to loop over files

for filename in * .[^.]*
do
    ...
done

to use the output of one command in another

command $(other_command ...)

to sum numbers in bash

count=$(($count + 1))

to count the number of bytes in a file with wc

wc -c $filename
Andras
  • 2,995
  • 11
  • 17
  • It is not the first time I see the `* .[^.]*` trick mentioned. Even if I understand it, I don't like it because who knows if there is or not any file starting with 2 (or more) dots ? The for-loop should be over `*`, `.[^.]*` and `..?*` ;-). (Also, be careful because any subdirectory would be listed.) – syme Feb 09 '15 at 18:37