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 with 52 Replies. Last Upated: Fri Apr 11, 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?


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
Ademola
Thanks for this quick info. Life saver for a custom ruby code relying on Unix shell to paginate.
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!!
Omar
Thank you. Is really helpme
Scott
The easy way to get rid of the filename: $wc -l < file.txt
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
motherfucker
LOVEIN AWESOME MAN!
A.E.S
Thanks! Very useful!!!
C.C
you can get rid of the file name also using cut: wc -l myfile |cut -d' ' -f1
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!
lala
I'm actually not seeing this as difficult..
Tomas
you can also use grep... grep -c "" file
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!
dude
thanks aus! this should really be an OPTION OF wc rather than having to fiddle around the problem. you saved people's days
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
Cori
YES!!!! Thanks
Mezeter
Thank uu very much.
Very usefull.
Thx again.
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
pooja
how can we count the number of lines charachter and number of vowels in a given paragraph
Ishan
Very useful Thanks!
Tom
Yes. Thanks
John
Perfect, thanks!
ndlinh
Thanks a lot. Very helpful.
WeatherARC
Thanks for this information, just what I needed for the last step in my code, well second to last step.
MarieWithHugeKnockers
Exactly the information I was hoping for!
Igor
Great, very helpful
garved
how can we find the line number of each c programme in linux
Paul
Thank you very much. Very helpful. God bless you!
James
Thanks very much - thought I'd need some sort of script but obviously not!
jonathan
Thanks a lot, saved me writing a shell script...
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
omsacras
Thanks for the useful tip! It was just the thing for which I was searching.
nice
It works very nice many thanks! And i have to had more because my comment is to short!!!
beparas
Thank you very much Thank you very much Thank you very much Thank you very much
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
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
matt
Perfect, exactly what i needed, could expand for other uses.
James
Just what I needed - thanks!
linux
i like the way you tell the solution of the problem of line counting.
nisha
04-1084-105405main-s100e5351-330x260.jpg
thanks it helped me a lot
Ankur
how to add something at the starting of each line using a shell script
Gavin
yes it did, short and sweet and exactly what i needed. i am a noob.
bass
cheers for short and good solution.
that hepled me a lot
Shaik
very nice...just looking for dis.......
Dave
nice! googled it and here it is!
col
cat file.txt > grep # > count.txt
wc -l count.txt
london
please show me how to count number of comment lines using shell script
lobner
needed just that info... thx
Tram
Thank you. It's helpful.
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