Skip main navigation

Starting Useful Commands: wc to Count Aspects of a File

Starting useful commands: wc to count aspects of a file
© Wellcome Genome Campus Advanced Courses and Scientific Conferences

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

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education