Count words, characters, and lines using the WC command in Linux

Home > Search > Linux commands
  by

The wc (word count) command can be used to count the number of words, characters, or lines in a file.

The wc command without any options will display the number of lines, words, and bytes in the file. In this example, file1 has 2 lines, 6 words, and 31 characters.

[user1@server1 ]# wc file1
2 6 31 file1

 


Only count lines

The -l option can be used to only count the number of lines in the file.

~]# wc -l file1
2

 


Only count words

The -w option can be used to only count the number of words in the file.

~]# wc -w file1
6

 


Only count characters

The -c option can be used to only count the number of characters in the file.

~]# wc -c file1
31

 


Count the number of characters in the longest line

The -L option  will display the number of characters in the longest line in the file.

~]# wc -L file1
18

 


Grep

The cat, grep, and wc commands can be combined to only count the number of lines in a file that contain a certain string of text. In this example, there is 1 line in file1 that contains the phrase Hello World.

~]# cat file1 | grep "Hello World" | wc -l
1

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments