Count files lines in a directory?

Hy!

I have some problem. Problem is that i don't now how to solve problem of average lines of files in a directory.

I have managed to get number of files in a directory, but i don't know the command to count average lines of these files.

I have one "for" loop that goes true whole directory...

Thanks in advance!

look into 'man wc'

Use wc command to get the number of lines (if I remember correctly, wc adds one or subtracts one from the actual count). Add the total number, divided by number of files for the average number of lines.

Yes but i don't know how to use wc command in a for loop on all files. I tried butt i have not managed to make it.

Example: I have 5 files in a directory. And i want to count their average lines.

@RTM - your idea is good but i'm newbie in linux. I will be very gratefoul if you guys can explain a little bit more. :o

to count all '.txt' files:

#!/bin/ksh

typeset -i sum=0
typeset -i cnt=0

for i in *.txt
do
   sum=$(( sum + $(wc -l < "${i}") ))
   cnt=$(( cnt + 1 ))
done

printf "sum->[%d] cnt->[%d] avgSZ->[%.2f]\n" ${sum} ${cnt} "$(echo "scale=2; $sum / $cnt" | bc)"
ruby -e 'c=ARGV.size;p ARGF.to_a.size/c.to_f' *

This won't work if the total number of lines is too large, since all of the lines will be put into an array.

Yes but i need that code in linux shell script. I'm only beginning shell scripting i don't now much about it...Thanx

well.... there's no better time than now to start learning

Thats true! :slight_smile:

I have tried your code Mr. vgersh99. It's working ok. But i must read a directory from a terminal and then count files of the given directory.

Ok here is my code:

echo "Directory: "
read directory

for file in $directory
do
cat $file | wc -l
done
echo
exit 0

Works fine with a file. But when i try to add directory then i get cat: vaje: Is a directory :confused:

#!/bin/ksh

for file in "${directory}"/*
do
   [ -f "${file}" ] && echo "file->[${file} lines->[$(wc -l < ${file})]"
done

man ksh
man test
man wc

vgersh99 you rock tha place... :cool:

And i hope last thing that i will ask you...If i don't input nothining in terminal for a directory, how can i make that this program will search current directory? I will post my code...

    
echo "Directory: "
read directory

typeset -i cnt=0
typeset -i sum=0

if [ $directory != "" ]; then
for file in "${directory}"/*
do
  # [ -f "${file}" ] && echo "file->[${file} lines->[$(wc -l < ${file})]"
	cnt=$(( cnt + 1 ))
	sum=$(( sum + $(wc -l < ${file})))
done
echo  ${sum} "/" ${cnt} "=" "$(echo "scale=2; $sum / $cnt " | bc)"


else
for file in "${directory}"/*  #here i will like to heave current directory
do
  # [ -f "${file}" ] && echo "file->[${file} lines->[$(wc -l < ${file})]"
	cnt=$(( cnt + 1 ))
	sum=$(( sum + $(wc -l < ${file})))
done
echo  ${sum} "/" ${cnt} "=" "$(echo "scale=2; $sum / $cnt " | bc)"
fi
#!/bin/ksh

#echo "Directory: "

read directory?'Directory: '

[ -z "${directory}" ] && directory='.'

typeset -i cnt=0
typeset -i sum=0

for file in ${directory}/*
do
  [ -f "${file}" ] && echo "file->[${file}] lines->[$(wc -l < ${file})]"
        cnt=$(( cnt + 1 ))
        sum=$(( sum + $(wc -l < ${file})))
done
echo  ${sum} "/" ${cnt} "=" "$(echo "scale=2; $sum / $cnt " | bc)"

Hi .. it was really informative.. Thanks for that ..
I have a similar situation here , I just need to get the counts (Number of files) in a directory ..

I can Place the directory names in some files .. the shell script should read the file and login to those directories and Print the values for each directories !!

My Output

unix/sed unix/awk
547 667

find /directory <see -type option for files>  | wc <see man page>