Skip main navigation

Accessing File Content

Accessing file content with cat, tail, head and less

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
© 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