Introduction to UNIX – Lesson II

| September 16, 2007 | Reply

As I touched upon in the previous lesson, UNIX is made up entirely of flat files. We learned how to move throughout the filesystem and list the files held therein.

Every single part of the unix machine is a simple, editable text file. This makes it extremely vital for us to understand how to view, edit, copy, and delete these files.

Now in this lesson, we’ll learn how to work with these files.

The first and most obvious reason for this, is that we’ll need to view log files.

System events and other important information are logged in plain readable text files.

There’s a lot of information held in those files, so the more we know about manipulating text files, the faster we can find relevant information.

We may also want to copy screen output to a file for later viewing. This is commonly done to monitor performance statistics, to create a point in time snapshot of what the system was doing when the file was created.

As we learned in the last lesson, the systems configuration files are stored in the /etc partition. Viewing and editing these files is a critical operation to the UNIX admin. To back up these files for protection against a mistake, we simply copy the file and give it a different name. /etc/hosts becomes /etc/hosts.bak, or /etc/hosts.bak.091607.

Making a backup of a file before you edit it is not only best practice, it helps to create a history of the machine. Unix Admins can view the old backed up files and see the settings the machine used to have.

All of the principals that you learn here will apply throughout your technological career. The most complicated and sophisticated operations make use of these very basic tools. Therefore it is wise to experiment, and become comfortable text editors, file manipulation, and safe habits.

Now, to be safe, we will contain our experiments as a non root user, and inside our home directory. You know now that when you log onto a box, by default you’ll be placed inside your home directory.

login as: luke’s password:

Linux cambridge 2.6.20-16-realtime #2 SMP PREEMPT Fri Jun 15 04:43:25 CEST 2007 i686

The programs included with the Ubuntu system are free software;

the exact distribution terms for each program are described in the

individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by

applicable law.

Last login: Sun Sep 16 08:39:11 2007 from


We can tell from this that I am in my home directory because of the ~ in the last line.


Just to be sure, and also for review I will verify this by issuing the “pwd” command.

luke@cambridge:~$ pwd



Now that I am sure of where I am on the filesystem, and I know I am running as a nonprivliged user, I can go ahead and start playing with files.

First, we need a file to work with. For this exercise I will name the file “file.txt”.

Unix has a very simple and very magical command called “touch”. “touch” will create a brand new empty file. See what happens when I type “touch file.txt”:

luke@cambridge:~$ touch file.txt

luke@cambridge:~$ ls

adrienne.mp3 Desktop1 file.txt Music song2 vmware

Audio Files Documents Images rsync tcpsyn

Backups Desktop Downloads Money Scripts Videos


Notice that a directory listing now shows my file there. To see more about the file I’m going to print out a listing in long format.

luke@cambridge:~$ ls -l

total 5212

-rw-r–r– 1 luke luke 5203238 2007-08-22 13:51 adrienne.mp3

drwxr-xr-x 21 luke users 4096 2007-09-13 16:04 Audio Files

drwxr-xr-x 5 luke users 4096 2007-06-01 05:09 Backups

-rwxr-xr-x 1 luke luke 550 2007-09-16 10:24

-rwxr-xr-x 1 luke users 16444 2007-09-05 14:22

drwx—— 2 luke users 4096 2007-09-05 13:27 Desktop

drwx—— 2 luke users 4096 2007-08-21 14:43 Desktop1

drwxr-xr-x 5 luke users 4096 2007-09-15 13:12 Documents

drwxr-xr-x 18 luke users 8192 2007-09-13 15:14 Downloads

-rw-r–r– 1 luke luke 0 2007-09-16 10:27 file.txt

drwxr-xr-x 9 luke users 4096 2007-07-24 14:36 Images

drwxr-xr-x 2 luke users 4096 2006-11-01 01:46 Money

drwxr-xr-x 146 luke users 4096 2007-08-20 11:07 Music

drwxr-xr-x 2 luke users 4096 2007-08-15 03:57 rsync

drwxr-xr-x 2 luke users 4096 2006-12-11 08:00 Scripts

drwxr-xr-x 6 luke luke 4096 2007-07-24 17:48 song2

-rw-r–r– 1 luke users 7509 2007-04-16 20:18 tcpsyn

drwxr-xr-x 3 luke users 28672 2007-08-13 20:03 Videos

drwxr-xr-x 3 luke users 4096 2007-06-01 04:59 vmware


Lets analyze this line piece by piece.

-rw-r–r– – The permissions on the file

1 – The amount of files (signifigant if this were a directory)

luke – The owner of the file

luke – The group of the file

0 – The size of the file

2007-09-16 – The date the file was last modified

10:27 – At what time the file was last modified

file.txt – Filename

We can tell from this information that it’s an empty file… the file size is 0.

For now, we’re going to put some text in this file so we can play with it. The simplest way to do this is with the unix “echo” command.

luke@cambridge:~$ echo “Hello World”

Hello World


See here that the echo command will print out the text on the screen. Now, we want to direct so instead of printing out on the screen, it prints into the file.

luke@cambridge:~$ echo “Hello World” >> file.txt


Now, ls shows us the file like this:

-rw-r–r– 1 luke luke 12 2007-09-16 10:44 file.txt

Instead of 0, we can see that the file is now 12k in size. It is no longer empty.

There are many ways that we can view the contents of the file. You can use the “man” command to find out more about each individual command. Specifically what options can be applied.

More, less, tail, cat, and vi.

“more” – displays the file one page at a time

“less” – displays the tail end of the page

“cat” – concatenate files and print to screen

“vi” – standard text editor

Try these four commands on files of varying sizes to get an idea for when each may be useful.

Since there are so many different types of UNIX operating systems, it’s difficult to find things that are uniform across them. But the one thing you can always bet on, is the good old text editor “vi” will every single machine, no matter how stripped down. That’s why when choosing a text editor, “vi” is a wise choice.

We’ll use our file.txt as a test file.

Open up a terminal session, and type “vi file.txt”

Vi isn’t your average text editor. It was developed before keyboards looked the way they do now, so there are lots of different ways to edit the text. There are piles of books written just on vi, and I will only touch it briefly. I highly suggest that you use google to learn as much as you can about vi, because effiecently editing text makes life much easier.

There are two modes in vi. The edit mode and the command mode. Think of the command mode as the toolbar in windows. Since this is entirely text driven, the menus and commands are key combinations instead of mouse clicks. The edit mode confidently named.

Instead of writing out how vi works, I will refer you to this well written tutorial.

Read, practice, and digest that, and then we will continue.


Category: Uncategorized

About the Author ()