Lab: Unix Introduction and Tutorial
UNIX INTRODUCTION
Setting up the system
- Email system (gmail, web or configuration)
What is UNIX?
UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops.
UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. However, knowledge of UNIX is required for operations which aren’t covered by a graphical program, or for when there is no windows interface available, for example, in a telnet session.
Types of UNIX:
There are many different versions of UNIX, although they share common similarities. The most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X.
The UNIX operating system
The UNIX operating system is made up of three parts; the kernel, the shell and the programs.
The kernel
The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.
As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfile (which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.
The shell
The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).
The adept user can customise his/her own shell, and users can use different shells on the same machine. Staff and students in the school have the tcsh shell by default.
The tcsh shell has certain features to help the user inputting commands.
Filename Completion – By typing part of the name of a command, filename or directory and pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again.
History – The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.
Files and processes
Everything in UNIX is either a file or a process.
A process is an executing program identified by a unique PID (process identifier).
A file is a collection of data. They are created by users using text editors, running compilers etc.
The Directory Structure
All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / )
UNIX TUTORIAL
1.1 Listing files and directories (ls)
When you first login, your current working directory is your home directory. Your home directory has the same name as your user-name, for example, myname@mydesktop, and it is where your personal files and subdirectories are saved.
To find out what is in your home directory, type:
ls
The ls command ( lowercase L and lowercase S ) lists the contents of your current working directory.
“ls” does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are very familiar with UNIX!
To list all files in your home directory including those whose names begin with a dot, type:
ls -a
ls is an example of a command which can take options: -a is an example of an option. The options change the behaviour of the command. There are online manual pages that tell you which options a particular command can take, and how each option modifies the behaviour of the command.
1.2 Making Directories (mkdir)
We will now make a subdirectory in your home directory to hold the files you will be creating and using in the course of this tutorial. To make a subdirectory called exercise1 in your current working directory type
mkdir exercise1
1.3 Changing to a different directory (cd)
The command cd directory means change the current working directory to ‘directory’. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.
To change to the directory you have just made, type:
cd exercise1
Type ls to see the contents (which should be empty)
Try to make another directory inside the exercise1 directory called “your_name_folder”
1.4 The directories . and ..
Still in the exercise1 directory, type:
ls -a
As you can see, in the exercise1 directory (and in all other directories), there are two special directories called (.) and (..)
The current directory (.) and the parent directory (..)
In UNIX, (.) means the current directory, so typing
cd .
In UNIX, (..) means the parent of the current directory, so typing
cd ..
will take you one directory up the hierarchy (back to your home directory). Try it now.
1.5 pwd (print working directory)
Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type
pwd
Hint : use the commands cd, ls and pwd to explore the file system.
1.6 More about home directories and pathnames
Understanding pathnames
First type cd to get back to your home-directory, then type
ls exercise1
to list the contents of your exercise1 directory.
Now type
ls your_name_folder
Hint : What message did you get? And why?
1.7 ~ (your home directory)
Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing
ls ~/exercise1
will list the contents of your exercise1 directory, no matter where you currently are in the file system.
2.1 Copying Files (cp)
cp file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2
2.2 Moving files (mv)
mv file1 file2 moves (or renames) file1 to file2
To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.
It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.
2.3 Removing files (rm) and directories (rmdir/rm -r )
To delete (remove) a file, use the rm command.
To delete (remove) an empty folder, use the rmdir command. To delete (remove) a non-empty folder, use the rm -r command.
Hint: Create a directory called tempstuff using mkdir , then remove it using the rm -r command.
2.4 Some useful commands
clear
This will clear all text and leave you with the % prompt at the top of the window.
cat (concatenate)
The command cat can be used to display the contents of a file on the screen.
less
The command less writes the contents of a file onto the screen a page at a time.
head
The head command writes the first ten lines of a file to the screen.
tail
The tail command writes the last ten lines of a file to the screen.
Exercises 1a:
What we are going to do now is to take the file science.txt, and use the cp command to copy it to your exercise1 directory.
Start by downloading the file “science.txt” from the bottom of this page and place it on your desktop.
Second, cd to your exercise1 directory.
cd ~/exercise1
Then at the UNIX prompt, type:
cp /home/<login-name>/Desktop/science.txt .
cat science.txt
less science.txt
Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading. As you can see, less is used in preference to cat for long files.First clear the screen then type
head science.txt
Then type
head -5 science.txt
Q. What difference did the -5 do to the head command?
Clear the screen and type
tail science.txt
Q. How can you view the last 15 lines of the file?
Exercise 2a:
Create a backup of your science.txt file by copying it to a file called science.bak
We are now going to move the file science.bak to your backup directory.
First, change directories to your exercise1 directory (can you remember how?). Then, inside the exercise1 directory, type
mv science.bak your_name_folder/
Type ls and ls your_name_folder to see if it has worked.
As an example, we are going to create a copy of the science.txt file then delete it.
Inside your exercise1 directory, type
cp science.txt tempfile.txt
ls
rm tempfile.txt
ls
2.5 Searching the contents of a file
Simple searching using less
Using less, you can search though a text file for a keyword (pattern). For example, to search through science.txt for the word ‘science’, type
less science.txt
then, still in less, type a forward slash [/] followed by the word to search
/science
As you can see, less finds and highlights the keyword. Type [n] to search for the next occurrence of the word.
2.6 grep
grep is one of many standard UNIX utilities. It searches files for specified words or patterns. First clear the screen, then type
grep science science.txt
As you can see, grep has printed out each line containg the word science.
Or has it ????
Try typing
grep Science science.txt
The grep command is case sensitive; it distinguishes between Science and science.
To ignore upper/lower case distinctions, use the -i option, i.e. type:
grep -i science science.txt
To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type
grep -i 'spinning top' science.txt
Some of the other options of grep are:
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines
Try some of them and see the different results. Don’t forget, you can use more than one option at a time. For example, the number of lines without the words science or Science is
grep -ivc science science.txt
2.7 word count (wc)
A handy little utility is the wc command, short for word count. To do a word count on science.txt, type
wc -w science.txt
To find out how many lines the file has, type
wc -l science.txt
3 Redirection
Most processes initiated by UNIX commands write to the standard output (that is, they write to the terminal screen), and many take their input from the standard input (that is, they read it from the keyboard). There is also the standard error, where processes write their error messages, by default, to the terminal screen.
We have already seen one use of the cat command to write the contents of a file to the screen.
Now type cat without specifing a file to read
cat
Then type a few words on the keyboard and press the [Return] key.
Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to end the input.
Hint: What has happened?
If you run the cat command without specifing a file to read, it reads the standard input (the keyboard), and on receiving the ‘end of file’ (^D), copies it to the standard output (the screen).
In UNIX, we can redirect both the input and the output of commands.
3.1 Redirecting the Output
We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type
cat > list1
Then type in the names of some fruit. Press [Return] after each one.
pear
banana
apple
^D {this means press [Ctrl] and [d] to stop}
What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1
To read the contents of the file, type
cat list1
Exercise 3a
Using the above method, create another file called list2 containing the following fruit: orange, plum, mango, grapefruit. Read the contents of list2
3.2 Appending to a file
The form >> appends standard output to a file. So to add more items to the file list1, type
cat >> list1
Then type in the names of more fruit
peach
grape
orange
^D (Control D to stop)
To read the contents of the file, type
cat list1
You should now have two files. One contains six fruit, the other contains four fruit.
We will now use the cat command to join (concatenate) list1 and list2 into a new file called biglist. Type
cat list1 list2 > biglist
What this is doing is reading the contents of list1 and list2 in turn, then outputing the text to the file biglist
To read the contents of the new file, type
cat biglist
3.3 Redirecting the Input
We use the (<) symbol to redirect the input of a command.
The command sort alphabetically or numerically sorts a list. Type:
sort
Then type in the names of some animals. Press [Return] after each one.
dog
cat
bird
ape
^D (control d to stop)
Hint: What is the output?
Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type:
sort < biglist
and the sorted list will be output to the screen. To output the sorted list to a file, type:
sort < biglist > slist
Use cat to read the contents of the file slist
Make a PDF file
Use any of your favourite editors installed on the machine and create a PDF file, so that you can submit the answer to the assistants.