Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I find the number of lines of a text file?

Status
Not open for further replies.

beginer

Programmer
Aug 23, 2001
3
JP
Hi, there,<br>I am new to UNIX and I just got an assignment to find the number of lines of all text files in my current and subsequent directories by using FIND command and command substitution.I know I can use<br>tail -n 1 $(find . -type f)<br>to display the last line of each of the files fund.Could anyone there tell me the command to find just the number of lines of each text file?<br>Thanks in advance.
 
Try the &quot;wc&quot; (word count) command with the -l (line count) option:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;wc -l &lt;filename&gt;<br><br>jfk
 
hi beginner,<br><br>look at the man page for the find command &quot;man find&quot; and you'll see that there's an option for running a command on each file that gets found.<br><br>you could get find to run a shell script for you on each file in a directory tree.<br><br>the shell script would examine the file (using the command &quot;file &lt;filename&gt;&quot;) and, if it's reported as containing text of some sort (look at 'man file') would run 'wc -l' on the file.<br><br>as you can imagine - as this is an assignment we're not keen on just writing the answer for you -- but have a go and get back to us with your script, someone will help you over the lumpy bits. <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
You won't need a script, use simply<br><FONT FACE=monospace><br>cd /your/directory<br>find . -type f -exec wc -l {} \;<br></font><br><br>That will search from your directory and down, selecting only <b>f</b>iles (not directories) and <b>exec</b>ute <b>wc</b> (word count) command showing only the number of <b>l</b>ines in each file. That {} is replace by each name, and \; is the final of command to exec with find.<br><br>I hope it works...
 
Thank you all for the valuable suggestion!!! Now I finally have made it work by using the following command:<br><br>wc -l `find . -type f` <br>or<br>wc -l $(find . -type f)<br><br>find. -type f -exec wc -l {} \; did the same job, but it does not use the command substitution which is required by my assignment. I have not learned script yet so far.<br><br>Again, I am vary grateful to all of you who have replied.<br><br>Shukui
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top