Bootstrap FreeKB - Linux Commands - wc (count words, characters, and lines)
Linux Commands - wc (count words, characters, and lines)

Updated:   |  Linux Commands articles

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

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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