Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Computer and Network Organisation (COMP5213)

School of Information Technologies University of Sydney

Unix Lab
This lab covers the basic Unix user skills including navigation, file management,
editing with vi and script creation.

The Unix Shell


When logging into the labs at university, we use the Xwindows interface. Xwindows
provides a platform for GUI applications. Before the development of Xwindows unix
users used a command line interface program or “shell”. Shells can be run as
processes under X using the xterm program. It is called xterm because it is an
Xwindows terminal emulator and it used to run shells because shells were originally
written to work with terminals.

Terminals are devices for working interactively with a command line interface. You
may have seen them in old movies (or banks, warehouses and hospitals where they are
still in use). A terminal connects a text screen and a keyboards to a Unix process over
a serial port. Any time you use telnet, ssh or connect to your ADSL router via a serial
port from your PC then you are using a terminal.

So, the program which provides the operating system interface for terminals is called
a shell. Shells allows users to execute commands in the current process and, like X, to
spawn and manage other processes. Typically, a shell will present the user with a
command prompt (they character(s) at the start of a line which tell the user that the
shell is ready for the next command). Examples are:

# Is often used as the prompt for the root user so that it is


obvious to whoever is working that they are working as root
(and hence they should be careful not rm -fr /usr/bin as I did
once before I restarted the system from a backup)
$ Is often used as the ordinary user prompt.
: as is this.
> So is this.
/home/freja$ In some shells it is possible to have the prompt display other
useful information such as the path of your current process, or
the name of the machine you are working on.
C:\FREJA This is a prompt some of you may recognise if you have ever
used DOS. DOS uses a command line interface similar to
Unix shells.

There are many different shells, and each of these may be slightly different depending
on which version of Unix you are running on. Some examples are:

bash The GNU Bourne-Again Shell (we will use this for examples in future)
csh The Berkeley C Shell
ksh The Korn Shell

All the examples in this Tutorial are written for bash and ksh.

Lecturer: Decler Mendez Author: Craig Bennett


Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney
Directory Context and Navigation
As we have said, shells provide users with a way to tell Unix to do work. As well as
allowing users to execute commands, a shell provides users with a context and an
environment.

The context (which will usually be the user’s home directory when they log in) is the
position in the filesystem where they are currently working. This allows them to
specify files and directories relative to their current context rather than having to
specify the full path time (starting with the root directory /) every time.

For example, when freja logs in, her home directory is /home/freja. So her initial
context is /home/freja.

pwd
Freja could find this out by entering the pwd (print working directory) command.

pwd
/home/freja

cd
If Freja would like to move to a different context she can use the cd (change
directory) command. So if Freja want to change her context to /home/freja/progs she
would use cd, but there are two (obvious) ways she can do this. The first is:

cd /home/freja/progs

In this command, Freja specified the complete path to the progs directory starting with
/ (the root directory) then the home and freja parent directories and finally the
progs directory. This doesn’t take advantage of the context of Freja’s shell process
however. Instead of the command above, Freja could have entered:

cd ./progs

This takes advantage of one of the two directory references which are available in
every context (. and ..) they refer to the current directory and its parent.

In practice, the cd command implicitly prefaces every path with ./ so Freja could have
simply written:

cd progs

Once she is in the progs directory Freja could have used either of the following cd
commands to return to /home/freja

cd /home/freja
cd ..
Computer and Network Organisation (COMP5213)
School of Information Technologies University of Sydney

~
Actually there is a special shortcut for referring to your home directory (~) which
allows Freja to refer to her progs directory from whatever context she is with the
directory name:

~/progs

Common directories on many Unix systems are as follows:

/bin Contains executable program files for programs that are needed when the
system starts up.
/dev A directory containing device special files for various physical and
logical devices.
/etc Contains numerous configuration files.
/lib Contains various programming libraries.
/proc Contains files with dynamically updated information about the running
processes on the system.
/sbin Similar to ‘/bin’. Sometimes contains executable files that are only ever
intended for the administrator’s use and may be needed when the system
starts up.
/tmp A directory in which any user may create temporary files. These files are
typically deleted when the system reboots.
/usr Typically contains executables anddata for various applications that are
not needed when the system starts up.
/var Typically contains log files and the mail and printer spooling di-rectories.
In recent times there have been attempts to move all dynamic data from
the ‘/usr’ directory to ‘/var’. This makes it possible to make the ‘/usr’
directory read-only and enables it to be shared among multiple machines.

Lecturer: Decler Mendez Author: Craig Bennett


Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Shell Variables: your Environment

One of the other facilities which the shells provide are environment variables.
Environment variables can be used to change the way your shell works, to store
configuration information or to pass information between programs. Environment
variables are accessible to shell scripts and programs. To refer to or use a shell
variable you add $ to the front of its name.

set
To see the variables which are currently set type:
set
HOME=/home/freja
MAIL=/var/spool/mail/freja
MAILCHECK=60
PATH=/usr/local/bin:/bin:/usr/bin:/home/freja/bin
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/home/freja
SHELL=/bin/bash
TERM=ansi
UID=502
USER=freja

.profile
When a shell process starts, it inherits the shell variables of its parent process (or in
the case of a shell spawned at login it has its shell variables set by settings in (the
global profile or environment files or the .profile file in your home
directory). You can edit your .profile if you wish to have things set or commands
run when you login.

echo
To see an individual variable by name you can echo it to the screen. For example to
examine your path you would enter:

echo $PATH

set and export


To set a shell variable called x to be equal to the word (depending on your flavour of
Unix – the second should work on black or white) you use one of the following sets of
commands:

set x=yahoo
or
x=yahoo
export x

Refer back to the listing of shell variables we got from the set command several of
the variables apply to the commands we have discussed already.
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

HOME The full path to your home directory


PWD The directory you are currently in
TERM The type of terminal you are using
SHELL The program which is your shell
PS1 The string which defines your ordinary shell prompt. '[\u@\h \W]\$
' means: username@machinename currentdirectory $
PATH The list of directories which your shell will search for programs to run
when you type them at the command prompt.

$PATH
The PATH variable is of particular interest as it allows you to run a standard set of
programs or your own. In particular, if your path doesn’t include the . path your
shell will not run executables in the current directory you are in (including the . path
in your PATH is often considered a security risk, can you think why?).

To add a directory to your path you would append the new directory to the path. For
example, if Freja wished to have her progs directory included in her shell search
path she might execute the following command:

PATH=$PATH:~/progs
export PATH

which
Since your shell may search many directories to find a command to run, how do you
know which one was run? You use the which command for example:

which ls

To tell you where the shell found the ls command (if it found it at all)
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Command History
fc
Most shells maintains a list of the commands that have been executed. With some it is
possible to use the up-arrow key to access the last command executed. Alternately, the
fc command can be used to display and edit the list of previously executed
commands. For example:

fc –l
1000 pwd
1001 x=yahoo
1002 export x

Lists the last 16 commands executed and their line numbers. To execute a command
from this list use the ! command followed by the line number. For example:

fc –s 1002

Will rerun the pwd command at line 1002. The bash shell uses the following variables
to determine where and how much command history it stores:

HISTFILE=/home/craig/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000

fc is only available in ksh.

Other Unix Commands


File Manipulation Commands
Some of the most frequently used shell commands are used to list the files in a
directory, create and maintain files.

ls
The ls command has been described as the most commonly used Unix commands. It
displays a list of the files which match certain criteria. For example:

ls Lists all the files in the current directory.


ls progs Lists all the files in the progs subdirectory
ls /usr/bin Lists all the files in the /usr/bin directory
ls ~/*.txt Lists all the files in your home directory which end in .txt

Note the last command, it uses the * character which tells the shell to perform pattern
matching expansion on the expression (by default this pattern matching occurs against
the list of directories in the current directory).
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney
Perhaps the most useful argument to the ls command is -l which causes ls to
display information about each of the files it lists. For example

ls -l
total 3
-rw-r--r-- 1 freja comp5213 13801 May 8 15:20 somefile
-rw-r--r-- 1 freja comp5213 10603 May 8 15:20 aFile.txt
drwxr-sr-x 3 freja comp5213 4096 Jun 21 18:38 progs

Try to determine what each column means (columns 1, 3 and 4 have been described
for you under the heading ‘Permissions’ below.)

Other commonly used argument to the ls command are a and s. The first tells ls to
show filenames which begin with a period (.) character. Starting a filename with (.)
is the Unix equivalent to the “hidden” attribute you may have seen under other
operating systems. The second tells ls to print the size of the file in blocks.

To combine all three options you would use the command


ls -lsa
total 6
4 -rw-r--r-- 1 freja comp5213 4096 May 8 15:20 .
4 -rw-r--r-- 1 freja comp5213 4096 May 8 15:20 ..
4 -rw-r--r-- 1 freja comp5213 130 May 8 15:20 .profile
4 -rw-r--r-- 1 freja comp5213 1060 May 8 15:20 aFile.txt
4 drwxr-sr-x 3 freja comp5213 4096 Jun 2 18:38 progs

Note that this now shows the special . and .. directory references as will as
.profile.

mv
The mv command is used to rename a file, move it from one directory to another or to
move an entire directory.

For example, to rename the file hamburger to sandwich the following command
would be used:

mv hamburger sandwich

Or to move the file fries from the directory stable to the directory unstable:

mv stable/fries unstable/
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

rm
The rm command is used to remove files. For example:

rm flobble

will remove the file called flobble from the current directory. The rm command
cannot be used to remove directories unless you use the -r argument. This will
remove the directory and all its contents including subdirectories.

cp
The cp command creates copies of files. For example

cp wibble wobble

creates a file called wobble which is a copy of the file wibble. The cp command
also has a -r option which recursively copies the contents of a directory, including
subdirectories, from one location to another.

mkdir and rmdir


The mkdir command creates directories. The rmdir command removes them, but
only if they are empty.

find
The find command is used to locate files with certain properties. For example,

find /home -name unix.ps

will search for a file called unix.ps in the directory ‘/home’ and any subdirectories.

find /home -name “*spot*”

will find any file which name contains the string spot.
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Permissions and File Security


Every file in a Unix system has a set of permissions. We encountered these earlier
when discussing the ls -l command. For example, the ls -l command might
produce something like this:

-rwxr-x--x 1 freja comp5213 92288 Jul 19 14:55 script

The important bits for security are as follows:

Permissions User who owns the file Group who own the file
-rwxr-x—x freja comp5213

The permissions block contains 10 fields for information:

The first shows the type of the file, it is explained as follows:

- Normal File
d Directory
b Block Device File
c Character Device file
l Symbolic Link
p Fifo (“Named Pipe”)
s socket

The next three characters are the read, write and execute permissions for the user who
owns the file. In our example, freja can read (r) and write (w) and execute (x) the
script file.

After that come the read, write and execute permissions for the group which owns the
file. In our example the comp5213 group can read (r) and write (w) but not execute
(-) the script file.

Lastly are the permissions for all the other users. In our example the cannot read (-)
or write (-) but can execute (x) the file.

You should note that a user can belong to many groups, but a file can only belong to
one and the user who owns the file need not be a member of the group which owns
the file.

For directories, the execute (x) permission has the special meaning – it determines
whether a user can access the directory. Similarly, the read (r) permission determines
whether a user can list the contents of a directory.
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

chmod
The chmod command can be used by the owner of a file to change its permissions.
Permissions to be set can be specified numerically or symbolically. When you set
permissions numerically, you add the numbers from the table below together to
achieve the setting you want and then run:

chmod <number> file(s)

Read Write Execute


Owner 400 200 100
Group 40 20 10
Others 4 2 1

So to make a file a file to be readable by everyone, but writable only by the owner we
would add 400 + 40 + 4 + 200 which is 644 and execute

chmod 644 afile

You can also use chmod in symbolic mode

chmod <users><operation><permission> file(s)

Users
u File Owner
g File Group
o Other Users
a All Users

Operation
+ Add specified permissions
- Remove specified permissions
= Set permissions removing existing permissions

Operation
r Read permissions
w Write permissions
x Execute Permissions

So the equivalent command to the one used above would be:

chmod u=rw, go=w afile

You can use the –R option if you want chmod to work recursively.
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney
Special Permissions
There are also some other permissions characters you may see listed for a file in place
of the executable field.

Setuid and setgid allow binary or script executables (some variants do not allow
scripts to do this) to run with the permissions of the file owner rather than the user
running the file. For example, if a user needed to backup the system, but they did not
have permission to read all the files in the system, they might use a setuid script to do
the backup.

The sticky bit causes an image of the executable to be stored in swap so that it will be
cached and hence load more quickly when run.

When applied to directories, some systems allow these bits to have special meaning.
The setgid bit forces all files created in a directory to be in the directory owners
group, bypassing the group of the creating user. The sticky bit is used on some
systems as a restricted deletion flag, which prevents a user from removing or
renaming a file from a directory unless they own that file.

These permissions appear as follows:

s Setuid or setgid and executable are set.


S Setuid or setgid are set but executable is
not.
t The sticky bit and executable are set.
T The sticky bit is set, but the executable
bit is not.

You can set these special permissions using the following symbols or numerical
values:

1000 o+t Set the sticky bit


2000 g+s Set User Id on execution
4000 g+s Set Group Id on execution
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

umask
The permissions which are assigned to a file when it is created is determined by the
users file mode creation mask or umask. The users umask is set using the umask
command using the same format as the octal numeric parameter to chmod. When a
file is created, its permissions will be determined by XORing the umask with access
mode 666 for files and 777 for directories. This means that files are created with the
OPPOSITE of what the digits would mean if they were permissions. Some common
examples are in the table below:

Umask File Permissions Directory Permissions


077 -rw------- drwx------
027 -rw-r----- drwxr-x---
007 -rw-rw---- drwxrwx---
022 -rw-r--r-- drwxr-xr-x
002 -rw-rw-r-- drwxrwxr-x

chown
The chown command is used to set the ownership of files and directories. The format
for the command is:

chown <user>:<group> <files>

For example of Freja wanted the progs directory to be owned by freja and the group
friends she would run:

chown freja:friends progs

You can use the –R option if you want chown to work recursively. By default files
are owned by the user who creates them and that users primary group.

Wildcards and Regular Expressions


In a GUI interface, users can select multiple files using the mouse or filter and sort
files using the elements of the GUI. In a command line interface, operations are
applied to groups of files by matching them with regular expressions.

The simplest regular expression token is the asterisk or star character (*) so if Freja
wished to delete all the files in her current directory (except . and .. which you
cannot delete) she would use the command:

rm *

The shell replaces the * with all the file-names in the current directory, separated by
spaces before executing the rm command.
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney
You can also use * to do partial matches so:

*xyz Matches all files ending in xyz.


xyz* Matches all files starting with xyz.
*xyz* Matches all files with xyz anywhere in the name.
x*y*z Matches all files starting with x and ending with z with y in the middle.

The ? character is similar to * except it only matches a single character. For example
the command

cp file?.txt backup/

will copy files with names like file1.txt, file2.txt and fileA.txt to the
directory backup. It will not copy a file called file.txt, however, since the ?
wildcard must match exactly one character.

Greater control can be obtained using character sets. For example, the command

mv file[123].doc backup/

will move files with names like file1.doc or file3.doc to the directory
backup. It will not, however, move file.doc or file7.doc.

The reverse effect can be achieved in the following way

mv file[^123].doc backup/

which will move file5.doc and file9.doc but not file2.doc or


file.doc.

You can also do things like:

ls m*n/?.txt

Which will list all the files which are one character followed by .txt in any
directory which starts with m and ends with n.
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Other Special Characters


In bash and ksh the following characters have special meaning:

; Command separator
& Run command as a separate process in the background
( ) Group commands
| Pipe
> < & Redirect input and output
* ? [ ] ~ + - @ ! Filename metacharacters (the last four are for ksh only)
" ' \ Quoting and escaping other characters
` Command substitution (output of the command here).
$ Variable substitution

When using the quoting and escaping characters they work as follows:

"" Everything in quotes is used as is except that shell


variables may be used with $ and commands between
`` will have their output inserted into the command.
'' Everything in single quotes is used 'as is' with no
substitution.
\ A character following a \ is used literally (eg: \$ inserts
a literal $ character rather than using it for variable
substitution)

Some examples:

echo 'Double quotes " have no special meaning inside


singles quotes.'
Double quotes " have no special meaning inside singles
quotes.

echo "The price is \$2.00\!"


The price is $2.00!

echo "There are `ls c* | grep –c c` files starting with c


here"
There are 3 files starting with c here

echo "The value of \$PS1 is $PS1"


The value of $PS1 is [\u@\h \W]\$
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Text Processing Commands


Apart from managing files, manipulating text is probably the next most common task
for the users of a Unix system. In this section we consider commands that manipulate
text in some way.

Display the contents of a file


Some different ways to display the contents of a file:

cat unix.txt.

will display the contents of the file unix.txt on the screen. If the contents of this
file cannot fit on a single screen then some of it will scroll off the top. The solution to
this problem is the more or less commands (they work in the same way just
different flavours of Unix), e.g.

more unix.txt

will display the contents of the file unix.txt on the screen, pausing after each
screenful until a key is pressed.

Redirection and Pipes


Displaying the contents of a file on the screen is useful, but even more useful is the
ability to process the contents of a file with other commands. One way of doing this is
with pipes. A pipe takes the output of a command on its left and passes it as input to
the command on its right. For example, the command

cat names.txt | sort

will take the output of the cat names.txt command and make it the input to the
sort command. The output will be the lines of the names.txt file in alphabetical
order.

While it may be useful to view names.txt in alphabetical order on the screen, it


would be even more useful to store this information in a file on its own. This can be
done with output redirection. For example, the command

cat names.txt | sort > sorted.txt

will place the lines of names.txt in alphabetical order in the file sorted.txt.

Input redirection can also be used, at the same time as output redirection if necessary.
So the above command could be replaced by the command:

sort < names.txt > sorted.txt


It is also possible to append output to a file, instead of creating a new one. This effect
is done by using >> instead of > for output redirection.

Another form of input redirection is illustrated in the following example:


Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

sort <<WOBBLE
Steven
Greg
Leanna
WOBBLE

In this case, instead of taking its input from a file, the sort command takes its input
from the keyboard, until it comes across a line containing the string WOBBLE and
nothing else.

Redirection works because there is a convention under Unix which by default assigns
every running process an input file handle or stream called stdin and two output file
handles called stdout and stderr. All the redirection we have done so far has
redirected stdout, but sometimes what we want to redirect is the errors output on
stderr. In this case we need to tell the shell which of the output streams from a
process we want to redirect.

File Descriptor Name Usually Opens


0 stdin Keyboard
1 stdout Terminal
2 stderr Terminal

Examples:
Send standard error to a file:

someprogram 2> fileoferrors

Send standard output and standard error to a file

someprogram >file 2>&1

This command works by sending stdout to file, and sending stderr to file
descriptor 1 (which is stdout)

Text Manipulation Commands


tail
The tail command is used to display the last lines of a file. For example, the
command

tail names.txt

will display the last ten lines of the file names.txt on the screen. The command

tail -15 names.txt

will display the last fifteen lines.


Computer and Network Organisation (COMP5213)
School of Information Technologies University of Sydney
tee
The tee command will display its input on the screen and in a file (it will, in fact
redirect output to two streams). For example, the command:

sort < names.txt | tee sorted.txt

will display the lines of the names.txt file in alphabetical order on the screen, and
place them in the file sorted.txt.

The GREP Command and Friends


The grep command displays all the lines of a file which match a basic pattern, (see
the manual for regexp). For example, the command

grep joe names.txt

will display on the screen all the lines of the names.txt file which contain the
string joe.

Some useful flags for grep are:

-c Display just the number of matching lines


-i Ignore letter case during comparisons
-l Print the names of files where matches
are found
-n Precede matches with the line numbers
on which they occur

The egrep command is a more complex version of grep which performs full
regular expression pattern matching.

sed
The sed command is used to edit text ‘on the fly’. For example, the command

cat officers.txt | sed ’s/Lieutenant/General/g’

will display the contents of the officers.txt on the screen with all occurrences
of Lieutenant replaced with General.

Lecturer: Decler Mendez Author: Craig Bennett


Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Printing
lpr
The lpr command is used to spool a file for printing. A typical example would be

lpr important.txt

which will spool the file important.txt for printing on the default printer.

The -P option can be used to specify the printer to use, if there is more than one
available. For example

lpr –Pps document.ps

will print the file document.ps to the printer called ps.

lpq
The lpq command can be used to view the contents of a printer queue (i.e. those files
in the spool waiting to be printed.) for example:

lpq
lp is ready and printing
Rank Owner Job Files Total Size
Active comp5213 5 unixlab.txt 1234 bytes
1st freja 16 myfile.doc 5678 bytes

The lpq command also accepts the ‘-P’ option to specify the printer.

lprm
The lprm command is used to remove a job from a printer queue. For example, given
the print queue above, if the user freja executes the command

lprm 16

the myfile.doc job would be removed from the queue. Again, lprm accepts the
-P option to specify a printer. In addition, the command

lprm –Pps –

will remove all print jobs owned by the person who executes the command (or just all
print jobs if the root user executes this command.)
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Getting Help
Many (but not all) commands will give you a usage summary if you give them a -? or
–help switch. If this isn't enough, you need to use the online manual.

man
In addition to all this paper documentation, a large amount of documentation is
available online on a Unix system. This documentation can be accessed with the man
command. For example, the command

man ls

will display on the screen a description of the ls command along with its various
options and output.

The online documentation is also broken into manual sections so that you will
sometimes see references to command(5) or file(8) this is telling you that the
documentation is in section 5 or section 8 of the manual (this is used to separate
implementation details from user guides etc). To access the man page for a command
from a particular section, you use the –s flag. For example to look at the manual for
environ in section 5 of the manuals you would enter.

man –s5 environ

There is a shell variable similar to PATH which determines where the man command
will look for man pages this is called MANPATH.
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

The vi Text Editor


The vi text editor and its derivative viMproved (vim) are available on many variations
of Unix and in the case of vim almost every operating system. Whole books can and
have been written on using the vi text editor, so we cannot hope to cover all of its
features here. However, the basic features are not difficult to understand.

Modes
Vi has three modes.

Command mode
Command mode is the default mode for vi. In command mode various commands can
be executed by typing various key combinations. Some of these commands are shown
below.

Key Effect
h or left arrow Move left one character
j or down arrow Move down one line
k or up arrow Move up one line
l or right arrow Move right one character
ZZ Save and quit
dd Delete the line under the cursor
x Delete the character under the curser
i Enter insert mode just before the cursor
I Enter insert mode at the start if the line
a Enter insert mode just after the cursor
A Enter insert mode at the end if the line
o Enter insert mode on a new line just below the current line
O Enter insert mode on a new line just above the current line
/pattern Find the next occurrence of the regular expression pattern
n Repeat the last search
numberG Go to line number, if number is omitted go to the end of
the file
^ Move to the start of the current line
$ Move to the end of the current line

Insert mode
In insert mode, text is placed in the file as it is entered from the keyboard. To return to
command mode press Esc.
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Last Line mode


Last line mode is entered using the : command. This mode allows for advanced
editing and commands

Command Effect
w Save the file
w name Save the file as name
wq Save and exit
q Quit
w! Save the file even if it is read only (you must own the file)
q! Quit discarding changes
n Move to the next file if multiple files are open

More Advanced vi
Vi also lets you change the appearance of the editor and other options using the set
command in last line mode. To set an option you enter

:set option

to unset it

:set nooption.

To see the current option settings (and a list of all the options you can set) enter

:set all

Two useful options are:

nu Display line numbers


tabstop=x Sets the tab character to be x spaces wide

Just as you could set startup variables for your shell using a .profile file, if you would
like vi to start in a particular way, then you edit a .exrc file in your home directory and
enter the option settings you would like as defaults.

Search and Replace

To search and replace in vi, you use the following kinds of commands
Command Meaning
:s/x/y/ replace the first x with a y on the current line
:s/AA/bb/g replace every occurrence of AA with bb on the current line
:12,14s/red/ remove the first occurrence of the word red on lines 12 to 14
:%s/12/33/g replace every occurrence of 12 with 33 in the whole file
:.,.+5s/^/#/ for the next 6 lines put a # at the start of the line
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

File Archives and File Compression


You may be familiar with the zip commands and archive files commonly used with
PCs. There are several similar utilities on Unix (including zip, compress and
gzip) but by far the most common combination are gzipped tar files (.tar.gz or
.tgz files).

tar
The tar program is used to store multiple files and directories in a single file. To
create a tar file you run:

tar –cf <tarfilename> <list of files to tar>

tar will automatically recurse directories. When creating archives tar will also
remove the leading / from file names if you specify an absolute path to archive so
that when the tar file is untarred it does so into the hierarchy of the current directory.

To untar a file you run:

tar –xf <tarfilename>

Which will extract all the files in the tar file into the current directory.

Useful flags are –v which gives verbose details about what tar is doing and –z which
automatically applies gzip compression to the archive.

Freja regularly saves a copy of her work directory using the command

tar –cvf progs`date '+%d%m%y`.tar work

Can you think what the `` quotes in Freja’s command might mean in shell?

gzip and gunzip


The gzip command is used to compress files and the gunzip command to
uncompress them. To compress a file you use a command like:

gzip filetocompress

This will delete filetocompress and create a compressed file called


filetocompress.gz.

To reverse this process you enter

gunzip filetocompress.gz
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

cron jobs
If Freja would like to have her backup done automatically, she could do so using the
cron daemon. The cron deamon is a system process which searches users crontabs
and creates sets of jobs to perform at particular intervals.

To add a job to your crontab execute

crontab –e

Which will give you a vi session for your crontab. Each line in the file is a separate
job which is made up of six fields separated by spaces and tabs:

Field Meaning Values


1 minute 0-59
2 hour 0-23
3 day of month 1-31
4 month of year 1-12
5 day of week 0-6 (0 = Sunday)
6 command to execute

Time/date fields which are not specified contain *

Examples:

Run my backup.sh script at midnight every Monday

0 0 * * 1 ~/progs/backup.sh

Download my email every 30 minutes

0,30 * * * * checkmail.sh

Say “hello” at 9am every work day

* 9 * * 1-5 echo “Hello”

Where do you think the output from the last command will go?
Computer and Network Organisation (COMP5213) Session II - 2002
School of Information Technologies University of Sydney

Writing Shell Scripts


All the commands we have seen so far are shell scripts, but all our scripts have been
one line long. Often it useful to create batches of commands and place them in scripts
to run. The commands in these scripts are just like the commands the user enters in
the shell so, congratulations you already know how to write scripts.

Let us consider an example script helloworld.sh which contains:

echo "Hello World"

When we want to run a script there are two ways we can do it either we can ask a
shell to run it for us eg:

sh helloworld.sh

Or we can use chmod to make the file executable and execute it

chmod 700 helloworld.sh


./helloworld.sh

If we do this though, how do we tell Unix that we want a different shell flavour to
execute our script?

The convention is to put a comment at the start of the file (comments in script files are
on lines starting with #) which contains a ! character followed by the name of the
executable we want to run the script. To have our script run by ksh we would need
to add

#!/usr/bin/ksh

Similarly to have our script run by perl (which will print an error since it is not valid
perl) we would add

#!/usr/bin/perl

As you learned at the start of the tute, you can use the which command to find the
path to a program if it is in your PATH.

When running scripts with ksh or bash there are some special variables which you
may find useful:

$# Number of command line arguments


$? Exit value of last executed command
$0 The command name
$n $1 to $9 the arguments given to the script
Computer and Network Organisation (COMP5213)
School of Information Technologies University of Sydney
Some longer shell examples

Copy each file starting with A to originalname.txt

for i in A*
do
cp $i $i.txt
done

Copy all the files ending in c to the same file but with a bak extension. The
basename program returns all of a file name up until the suffix specified.
for i in *.c
do
j=$(basename $i .c)
cp $i $j.bak
done

Copy all files called myprog.* to newprog.*. The cut command outputs a portion of
its input (in this case we are telling it that we want . as the delimiter (using the –d
switch), we want the second field (using the –f switch) and that the input to the
command is from stdin (using the – at the end of the command)
for i in myprog.*
do
j=$(echo $i | cut –d. -f2-)
cp $i newprog.$j
done

I want to do this at home!


There are two easy options to get your self a Unix like operating system to run on a
PC. Firstly, you could obtain one of the many x86 compatible Unix operating systems
available on the internet (or from www.hotline.com.au which sells them on CD). You
can then install this in your PC (be careful not to wipe Windows off (unless you want
to)). Secondly, you could download cygwin (www.cygwin.com) which lets you run
many of the GNU utilities and a version Xwindows on top of Microsoft Windows.

References
Gilly, Daniel, "Unix in a Nutshell", O'Reilly, United States, 1994

OpenBSD, Solaris and Redhat Linux man pages

www.gnu.org

www.openbsd.org

www.redhat.com

www.cygwin.com

Lecturer: Decler Mendez Author: Craig Bennett

You might also like