How To Count The Number Of Lines In A File In Linux Shell Command

Forums » Linux Systems » How To Count The Number Of Lines In A File In Linux Shell Command
This Page Contains information about How To Count The Number Of Lines In A File In Linux Shell Command By tomas in category Linux Systems with 52 Replies. Last Upated: Thu May 15, 2014
ok, you have a file and you want to know how many lines it has right?

if you are like me, sometimes we need to know this infromation for some reason or another. well today you are in luck because you've found a cool little tutorial i wrote just to show people like you and me who are learning linux how to send or used linux shell command.

ok the command you need to use is wc

for example, if i have a file called my_text.txt and i want to know how many lines this file has, all if would have to do is send this command:

wc -l my_text.txt


COUNTING ONLY LINES
Like mos UNIX commands, the wc command offers options that instruc the wc to run in different ways, you can tell wc to limit the count just lines, words or characters or any combination of each.

did this short tutorial helped you?


lobner
london
please show me how to count number of comment lines using shell script
col
cat file.txt > grep # > count.txt
wc -l count.txt
Dave
nice! googled it and here it is!
Shaik
very nice...just looking for dis.......
bass
cheers for short and good solution.
that hepled me a lot
Gavin
yes it did, short and sweet and exactly what i needed. i am a noob.
Ankur
how to add something at the starting of each line using a shell script
nisha
thanks it helped me a lot
linux
i like the way you tell the solution of the problem of line counting.
James
Just what I needed - thanks!
matt
Perfect, exactly what i needed, could expand for other uses.
randomguy
well I needed something to count the running processes or the amount of elements in a directory. But your tutorial reminded me of the WC-command and i set up THIS solution: ls | wc -l ps -e | wc -l etc... thx
aarne
this was fine. thanks.
Do you know a way to list lines from file, were there is something in specific position f.ex. I would like to know how many products have EAN-code and EAN code starts from position 238 (15char.) in xxxfile.dat
beparas
Thank you very much Thank you very much Thank you very much Thank you very much
nice
It works very nice many thanks! And i have to had more because my comment is to short!!!
omsacras
Thanks for the useful tip! It was just the thing for which I was searching.
rajiv
Hi thanks. I used the command and i just want to make things sure the command is wc -l l as in London first i used wc -1 i thought it is number 1 ..... cheers
jonathan
Thanks a lot, saved me writing a shell script...
James
Thanks very much - thought I'd need some sort of script but obviously not!
Paul
Thank you very much. Very helpful. God bless you!
garved
how can we find the line number of each c programme in linux
Igor
Great, very helpful
MarieWithHugeKnockers
Exactly the information I was hoping for!
WeatherARC
Thanks for this information, just what I needed for the last step in my code, well second to last step.
ndlinh
Thanks a lot. Very helpful.
John
Perfect, thanks!
Tom
Yes. Thanks
Ishan
Very useful Thanks!
pooja
how can we count the number of lines charachter and number of vowels in a given paragraph
Arun
what is the shell script for find the lines numbers of a text file are having words which are 5 to 10 characters long and having first letter as a capital letter
Mezeter
Thank uu very much.
Very usefull.
Thx again.
Cori
YES!!!! Thanks
Aus
Great thanks ! the results alway come back with file name, eg: $wc -l text.txt 10 text.txt I have to get rid of the file name. $wc -l text.txt | awk -F" " '{ print $1 }'
Michael
Examples that strip additional spaces off:
wc -l text.txt | awk '{print $1}'
or
echo `wc -l < text.txt`
or
awk 'END {print NR}' text.txt
or
sed -n '$=' text.txt
or, not counting empty lines:
grep -c . text.txt
dude
thanks aus! this should really be an OPTION OF wc rather than having to fiddle around the problem. you saved people's days
Rui
Another cool way to do this without the filename and without having to use awk is to do : cat filename.txt | wc -l
Ling
Yes, this is really cool!
Tomas
you can also use grep... grep -c "" file
lala
I'm actually not seeing this as difficult..
achacon
form me It's very nice to find very fast, how I can count the numbers of lines in file from linux. Thank you very much!
C.C
you can get rid of the file name also using cut: wc -l myfile |cut -d' ' -f1
A.E.S
Thanks! Very useful!!!
motherfucker
LOVEIN AWESOME MAN!
Mikey
What about using wc -l with grep where you want to count the number of lines that start with a specific pattern...?
lkench
you could do it that way, but grep -c is much less cumbersome if you want a count of matching lines
Raewe
Thanks a lot. Very helpful.
Ahmed
Some one said they don't see as difficult. Selfish. There was a day you knew nothing about this. Thanks to the Author of this tutorial. I am Java developer, using linux all the time, and I rely these kind of tutorials for quick answer. thanks again
Scott
The easy way to get rid of the filename: $wc -l < file.txt
Omar
Thank you. Is really helpme
Venkat
--------------- text file. Click on Edit>Go To. Give a huge number than expected number of lines. say if you expect to have 2000 lines, give 5000 or just give 200000. It gives an eror "The Line number is beyond the total number of lines" and shows up the the immediately available line. If you have 2379 lines it gives 2380. So whatever it shows up minus 1 is the actual number of lines in the text file. Cheers!!
Ademola
Thanks for this quick info. Life saver for a custom ruby code relying on Unix shell to paginate.
Tram
Thank you. It's helpful.
harish
Thanks for sharing this was useful
Mike
echo $(less | wc -l <file>)
Title: How To Count The Number Of Lines In A File In Linux Shell Command [2890]
Tags: how to count the number of lines in a file in linux shell command