Use wc command to count number lines

by Daniel Pham
Published: Updated:
This entry is part 6 of 10 in the series Linux commands: Part 6 - Redirection

This article will introduce wc command. You can use wc command 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, number words, number bytes in a file
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.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Use uniq command to delete duplicate linesNext part: Use grep to print lines match a pattern »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.