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
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.
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
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
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.
To display two lines at the top of the file, type
head -2 Diamonds.csv
Conversely, to display two lines at the bottom of the file, type