Use wc command to count number lines

Share this post to your friends !

This entry is part 6 of 10 in the series Linux commands: Part 6 - Redirection
Use wc command to count number lines
5 (100%) 1 vote[s]

WriteBash - This article will introduce wc command. The wc command is used to count the number of lines, words and bytes in the file.

Wc command syntax

There are 2 ways to use the wc command as below.

We use wc to count the number of lines in a file, not the data in the pipeline.

$ wc [options] filename

Use wc in the pipeline, processing the output data from another command.

$ command-1 | command-2 | wc [options]

Use wc command to count number lines

Okey, now we will do an example. The first example is to use the non-pipeline command.

We have a text file with the following content. Now we will count the lines, count the words, count the number of bytes of that text file.

$ wc demo-wc.txt
use-wc-command-to-count-number-lines Use wc command to count number lines
Use wc command to count number lines, number words, number bytes in a file.

For the second example, we will reuse the example included in the article about sort command. We will list the content in /bin and /usr/bin and sort it. Now we will count the number files in both folders after sorting.

# ls /bin /usr/bin | sort | wc -l
2748
  • Use -c to print the byte counts.
  • Use -l to print the newline counts.
  • And use -w to print the word counts.

Conclusion

The wc command is often used in conjunction with the sort and uniq command. This set of 3 commands creates very powerful data filtering pipelines. You should combine it.

Continue reading the series«« Previous part: Use uniq command to delete duplicate linesNext part: Use grep to print lines match a pattern »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.