User Tools

Site Tools


unix:bash:simple_arithmetic

Add file sizes together from a list returned via Find

IFS=$'\n';set j=0;for i in $(du -s $(find ./ -name \*.MOV)|cut -f 1);do j=$(( $i + j ));echo $j;done;IFS=$' \t\n'

This seems to keep growing after multiple runs, so something is odd.

Better:

IFS=$'\n';set j=0;for i in $(du -s $(find ./ -name \*.MOV)|cut -f 1);do j=$(( $i + j ));echo $j;done;unset i;unset j;IFS=$' \t\n'

Need to divide number by 2 on a Mac for human readable size in Megabytes:

IFS=$'\n';set j=0;for i in $(du -s $(find ./ -name \*.MOV)|cut -f 1);do j=$(( $i + j ));echo $(( j / 2 ));done;unset i;unset j;IFS=$' \t\n'

Better:

IFS=$'\n';set j=0;for i in $(du -s $(find ./ -name \*.MOV)|cut -f 1);do echo $i;j=$(( $i + j ));echo $j;done;echo $(( $j / 2));unset i;unset j;IFS=$' \t\n'

Now need to change so the file names are one var, the size is another var, so the output can be nice and neat:

IFS=$'\n';set j=0;for f in $(find ./ -name \*.MOV);do i=$(du -s $f|cut -f 1);k=$j;j=$(( $i + j ));echo $f is $(( $i / 2)) Mbytes;echo Added to $(( $k / 2 )) Mbytes equals $(( $j / 2 ));done;unset f;unset i;unset j;unset k;IFS=$' \t\n'

This seems to be the working but quite verbose code (important to note “k / 2” instead of “$k / 2”, apparently caused by the set of k=$j, creates a pointer or something?):

IFS=$'\n';set j=0;set k=0;set i=0;set f=;for f in $(find ./ -name \*.MOV);do i=$(du -s $f|cut -f 1);k=$j;j=$(( $i + j ));echo $f is $(( $i / 2)) Mbytes;echo Added to $(( k / 2 )) Mbytes equals $(( $j / 2 )) Mbytes;done;echo The total size is $(( $j /2 )) Mbytes;unset f;unset i;unset j;unset k;IFS=$' \t\n'

Now, can we get it sorted?

unix/bash/simple_arithmetic.txt · Last modified: 2010/02/22 08:30 by ben