Skip main navigation

Accessing File Content

Accessing file content with cat, tail, head and less
© Wellcome Genome Campus Advanced Courses and Scientific Conferences

Here are some commands you can use to display the file contents

This section is focussed on displaying the contents of a file within a terminal window, and in order to run the commands you will need to download the Diamonds.csv file. This file is part of the data that you downloaded in Step 1.6 of this course.

Navigate to your download folder using the cd (change directory) command. Check the file is in the directory

ls
Diamonds.csv

cat

cat is short for catenate and displays the contents of a file. If this is a very long file then the output will take up more room than the terminal can display

To display the contents of Diamonds.csv, type

cat Diamonds.csv

This will output more content than the screen can display.

screenshot of the cat command display of the diamonds.csv file

less

If you want to look at a very long file then use the less command, as it will display one page at a time. You can then scroll up and down through the file.
Type

less Diamonds.csv

This will produce output that fills the display

screenshot of the result of the less command

As you can see, Diamonds.csv is highlighted at the bottom of the page, and the output can be controlled with a number of options. A few of the basics are shown below.

   
space advances by one page
down and up arrows or j and k scroll by one line
/ searches for tex
q quits to the shell
h displays help screen

head and tail

Sometimes you just want a quick look at the beginning or end of a file. This is useful for getting a sense of the contents of very large files, and for seeing the latest additions to things like logs that routinely have new data appended to them.

As an example, type

head diamonds.csv

This will display the first 10 lines of the Diamonds.csv file

screenshot of the result of the head command

To display the bottom of the Diamonds.csv file, type

tail Diamonds.csv

This will display the bottom 10 lines of the Diamonds.csv file.

screenshot of the result of the tail command

To display two lines at the top of the file, type

head -2 Diamonds.csv

screenshot of the result of the head -2 command

Conversely, to display two lines at the bottom of the file, type

tail -2 Diamonds.csv

screenshot of the result of the tail -2 command

© 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