Skip main navigation

Starting Useful Commands: wc to Count Aspects of a File

Starting useful commands: wc to count aspects of a file
The UNIX wc command is a useful tool for counting the number of lines, words and characters in a file.

In order to demonstrate the wc command, let’s create a text file to give it something to count.

Create a file using the nano editor called fruit.txt

screenshot of command creating a file named fruit.txt

Then populate it with the following line:

Orange

Apple

Pear

Banana

Grape

screenshot of nano with fruits names typed in

Select control-x to exit and type y and press enter to save the changes and write the file to disk.

screenshot of exiting nano i.e. fruit.txt

Type ls to list the directory contents and you will see a file called fruit.txt

ls

screenshot of the ls command typed into the command line

The following terminal details various wc outputs:

wc fruit.txt gives a full output of the number of lines (5), words (5) and characters (31).
Separate outputs can be obtained by adding additional parameters:

wc -l outputs the number of lines in the fruit.txt file i.e. 5

wc -w outputs the number of words in the fruit.txt file i.e. 5

wc -c outputs the number of characters in the fruit.txt file i.e. 31

screenshot of examples uses of command wc

Further information on the wc command and its parameters can be found by accessing the following link.

https://linux.die.net/man/1/wc

Or by accessing the manual pages for wc within a terminal session, by typing:

man wc

© Wellcome Genome Campus Advanced Courses and Scientific Conferences
This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now