Lesson 4: Viewing/Displaying/Searching the contents of a file.
In this lesson we will look at ways of viewing and displaying the contents of a file using unix commands.
Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.
At the prompt, type
% clear
This will clear all text and leave you with the % prompt at the top of the window.
The first command we will look at for the displaying the contents of a file is the more command. The more command display the contents of a file one screen full at a time.
Let’s pretend we have a file called resume.txt on root, to view this file we would type
% more resume.txt – the screen would then output the contents of this file.
Remember more only lets you seen on screen full at a time, so see the next screen full you just click on the space bar.
Keep clicking the space bar until you reach the end of the file.
Another command for displaying files is cat
The command cat can be used to display the contents of a file on the screen. Type:
% cat resume.txt
As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable.
Other commands for viewing files are the head and tail commands
head
The head command writes the first ten lines of a file to the screen.
First clear the screen then type
% head resume.txt
tail
The tail command writes the last ten lines of a file to the screen.
Clear the screen and type
% tail resume.txt
Searching for a word in a file
grep is one of many standard UNIX utilities. It searches files for specified words or patterns.
First clear the screen, then type
% grep address resume.txt
As you can see, grep has printed out each line containg the word address