#!/bin/bash while read x do y="$y $x" # build a string y that will contain all file names done for f in $y do n=$(cat $f | wc -l) if [ $n -lt 3 ] # if there are less than 3 lines in file then echo $f # print just file name continue fi line3=$(head -3 $f1 | tail -1) # take 3rd line of file s=0 for z in $line3 # calculate sum of numbers in 3rd line of file do s=$[$s + $z] done echo "$f $s" done