I'm trying to compute the source code size of gcc by considering cpp files first:
# NOTE: the cpp loop finishes immediately
LOC=0
BYTES=0
FILES=$(find . -name "*.cpp")
for f in ${FILES};
do
BYTES_TAG=$(stat --printf="%d" $f)
LOC_TAG=$(cat $f | wc -l)
BYTES=$((BYTES+BYTES_TAG))
LOC=$((LOC+LOC_TAG))
done
echo "LOC = $LOC, SIZE = $BYTES"
Then I try to sum the *.c files, but the bash loop doesn't stop. here is my gcc version:
$ wget https://ftp.gnu.org/gnu/gcc/gcc-11.2.0/gcc-11.2.0.tar.gz
$ tar -xf gcc-11.2.0.tar.gz
This is weird because counting all the files is immediate with:
$ find . -type f | wc -l
Size of all *.c and *.cpp files in bytes:
Number of lines in all *.c and *.cpp files:
Explanation:
find ... -execexecutes a command on all files it finds, replacing the{}in the-execpart with the file name(s). If you end the-execpart with\;, it will be executed once for each file. In some cases, ending the-execpart with+is more efficient -- this will execute for as many files as will fit in one command line.wc -landwc -cwill print one line per file, with the number of lines / characters (bytes) followed by the file name. The `sed "s/ .*//"' will cut away everything after the number.paste -sd+will then concatenate all the lines (-s) and separate them with plus signs (-d+). Piping this tobcmakesbcexecute the addition and give you the total you are looking for.Meta answer: Learn about
find ... -exec. Don't loop overfindoutput, because you will get into trouble e.g. when file names contain spaces -- andxargsis in most cases unnecessary.