Count files lines in a directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count files lines in a directory?
# 1  
Old 03-10-2006
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!
# 2  
Old 03-10-2006
look into 'man wc'
# 3  
Old 03-10-2006
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.
# 4  
Old 03-10-2006
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. Smilie
# 5  
Old 03-11-2006
to count all '.txt' files:
Code:
#!/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)"

# 6  
Old 03-12-2006
Code:
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.
# 7  
Old 03-12-2006
Yes but i need that code in linux shell script. I'm only beginning shell scripting i don't now much about it...Thanx
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count lines from multiple files (3)

Hey everyone, I've to count lines from string of files names then to show sum output of lines. for example: read x = F1 F2 F3 F1 = 12 lines F2 = 14 lines F3 = 10 lines = 36 what I did is: read x echo $x >|temp for x in $(cat temp) do wc -l < $x (3 Replies)
Discussion started by: Aviv
3 Replies

2. Shell Programming and Scripting

Count files in every directory

Hi all, I'm looking over all the internet for finding how to make a count of the files for every directory apart.. not a total result for the xx directories. For example: -dir1 file1 file2 -subdir1 -subdir2 file1 -subdir3 file1 -dir2 -dir3 file4 ... (9 Replies)
Discussion started by: CODIII
9 Replies

3. UNIX for Dummies Questions & Answers

Count number of files in directory excluding existing files

Hi, Please let me know how to find out number of files in a directory excluding existing files..The existing file format will be unknown..each time.. Thanks (3 Replies)
Discussion started by: ammu
3 Replies

4. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

5. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

6. Shell Programming and Scripting

count number of files in a directory

what's the script to do that? i want to only count the number of files in that directory, not including any sub directories at all (5 Replies)
Discussion started by: finalight
5 Replies

7. Shell Programming and Scripting

script to count files in a directory

Hello, I was wondering if anyone had an answer for this? thanks, KW (3 Replies)
Discussion started by: kwa71
3 Replies

8. UNIX for Dummies Questions & Answers

trying to count lines in multiple files

Hi there, I need help. I want to run the command: less filename | wc -l But on multiple files in a directory So to get those files I would run ls -ltr | grep filename_2000123 or of course ls -ltr *filename_2000123* But I am having a problem running a loop to get a count of each... (1 Reply)
Discussion started by: llsmr777
1 Replies

9. Shell Programming and Scripting

Count the number of files in a directory

Hi All, How do i find out the number of files in a directory using unix command ? (14 Replies)
Discussion started by: Raynon
14 Replies

10. UNIX for Dummies Questions & Answers

How to find the count of files in a directory

Hi Gurus WHat would be the command to check whether there is a file in particular path or not.. for ex: my file name is ExRate_20071501.csv I can have many files with same pattern but diffrentiated by date.. i have a process where i have to check if files exist in tht folder i have to... (5 Replies)
Discussion started by: sish78
5 Replies
Login or Register to Ask a Question