Вы находитесь на странице: 1из 11

UNIX

Objectives

 To understand the term ‘Operating System’


 To understand the basic architecture of UNIX
 To understand the system directories that get installed when UNIX is installed.
 To be able to execute basic commands in Unix relating to files and directories
 To be able to grant and revoke permissions
 To understand the importance of the .profile file
 To understand the concept of input, output and error redirection
 To be able to use the VI editor

1.1 Introduction

Software can be termed as an operating system when it is capable of

 Controlling the hardware and peripheral devices


 Managing the memory
 Scheduling, prioritizing, initiating and controlling jobs
 Managing file creation and
 Interpreting commands
Unix performs all the above and is one of the oldest, stable and a multi-user operating
systems.

2.1 Unix architecture

Unix being a multi-user operating system does not allow its users to directly interact
with the hardware unlike DOS. All user interactions in Unix are done via the shell and the
kernel. Shell is the program that acts as a bridge between the user and the kernel. It
interprets and also does input/output redirection. Kernel is the heart of the Unix operating
system. It is the kernel, which communicates with the hardware and sends back the required
data to the user via the shell.

USER

SHELL

KERNEL

HARDWARE

Figure 1.1 Unix Architecture

Every user, when logged on to Unix, works in a specific environment called the shell.

3.1 The System Directories

Temenos Training Publications 1


When Unix is installed, a number of system directories get installed. These directories
contain a number of system files that control the functioning of the operating system.

/(root)

bin dev etc usr lib user tmp

uv trguser1 trguser2

Figure 2.1 Unix System Directories

/(root)

The directory denoted by a ‘/’ is the main/parent directory of the Unix operating
system. It is usually called ‘root’. All the other directories of Unix branch out from here.

bin

Contains system executable files. All commands in Unix have an associated file
with the same name. When a command is issued by a user the shell searches for a file with the same
name as that of the command and executes the file if found, else flashes an error message. These
executable files are usually found in this bin directory.

dev

Contains device files. Every thing in Unix is treated as a file. A file, a directory a
device such as hard disk, tape, floppy disk, keyboard etc are all treated as files. Every device that is
present as apart of the system has an associated file in the ‘dev’ directory.

etc

Contains admin-related files. There are a number of files that are required for the
administration of Unix. Such files are found under this directory. Some of the admin related files are
discussed further in this session.

usr

There could be a number of applications/software that need to be installed on unix.


All such applications/software are usually under this directory.

lib

In order for Unix to function, it needs a number of library files. These are like the .dll
files in Windows, which are required for the functioning of the operating system.

user

User’s home directories are usually created under this directory

tmp

Temenos Training Publications 2


The temporary directory of Unix. Every operating system/application/database require
a ‘tmp’ directory in order to store temporary information. For instance, when a user executes a
program, details of that program, variables in that program etc get stored in this temporary area. As
the name implies, the contents of this directory are temporary and the values get flushed periodically.
When a user logs on to the system, his user information(profile) is loaded on to the temp area.

3.1 Logging Into Unix

In order to login into the Unix system, at the login prompt, type your user name and
the password. Unix validates the user name and password. Once validated, you would be taken to a
protected environment called the ‘shell’ where you could perform any operation.

login : trguser1 Supply the user name and password


password :

$ Prompt for normal users


(# - Prompt for administrator/root)

3.1 Basic Unix Commands

Following are some of the basic Unix commands. All these can be executed from the
Unix prompt.

pwd Print Working Directory. This tells you which directory you are in.
After ‘shelling’ to Unix, you will be in your home directory. In a
GLOBUS account, the home directory will be a run area e.g.
demo.run.

3.1.2 Listing files


ls List the contents of the current directory
ls -l List with full details
ls -l | more (or) ls –l | pg List with full details one page at a time
ls -al Lists all files along with hidden files (File names that begin with a ‘.’
are treated as hidden files in Unix)
ls –l tem* Lists all files that begin with tem followed by any character/any
number of characters. Use the wildcard character ‘?’, to restrict
search to 1 character

Temenos Training Publications 3


Guide To Detailed File List

Output of ‘ls –l’ command

drwxrwxrwx 2 uvtr97 uvtrain 8192 Aug 4 10:33 &SAVEDLISTS&


-rw-rw-rw- 1 uvtr97 uvtrain 49152 Aug 4 10:33 VOC
-rw-rw-rw- 1 uvtr97 uvtrain 16384 Aug 4 10:33 VOCLIB
drwxrwxrwx 2 uvtr97 uvtrain 8192 Aug 4 11:25 liz
drwxr-x--x 3 uvtr97 uvtrain 8192 Aug 4 10:29 uvtr97

1 2 3 4 5 6 7 8 9

1) ‘d’ indicates a directory, ‘-‘ is a file


2) permissions
3) link count
4) owner of file
5) group of owner
6) size of file
7) date file last modified
8) time file last modified

3.1.2 Directories
Unix files are stored in Directories that are like folders in a filing cabinet. The highest
directory is called the root directory (symbolized by /). The directory structure is then like a tree up
from this central point. Unlike a filing cabinet, a directory can have any number of subdirectories in it,
and they in turn can contain subdirectories.

When listing the contents of a directory, you can identify a directory by the ‘d’ at the
far left of the screen, when you run the ‘ls –l’ command.

After each command enter ‘pwd’ to see which directory you are now in.

$ cd Change Directory. On its own this goes to the user’s home directory.
(This should be the run account you log in to).
$ cd / Goes to the root directory.

$ cd dirname Change to the specified directory.


$ cd /dir1/dir2 Goes to an absolute path name regardless of location.
Absolute path names are the full address from root (/)
$ cd .. Goes up one directory level. Do this until you arrive back at the root
directory.
$ mkdir dirname Make a directory. Give it your family name.

$ rmdir dirname Delete an empty directory . Delete yours.

$ rm –r dirname This removes a directory and all its contents. This command can be
very dangerous, so use it with extreme care.

Temenos Training Publications 4


3.1.3 Create, Copy, Move and Remove files

$ touch filename Creates a file under the present working directory.


$ ls –l filename List the details of this newly created file.
$ cp filename filename1 This copies the file to another file. (Similar to Save As).
$ cp –R temenos / Copies the directory temenos(recursively) to root(/)
$ rm filename Remove/Delete this file
$ mv filename filename1 Move. When moving a file in the same directory you are effectively
giving it a new name. Therefore the move command can be used to
move as well as rename files
$ cat filename Displays the contents of the file.
$cat filename1 filename2 Displays the contents of the file ‘filename1’ and ‘filename2’ one after
the other
$ cat filename | more Displays the contents of the file ‘filename’ , 1 page at a time
q Quit the display

3.1.4 Permissions

When you execute the command ls –l, you would let a detailed list of all the files and
directories under the current working directory. On the left hand side of the detailed list you can see
the permissions on each file and directory. This set of permissions is know as the FAP(File Access
Permissions).

There are only 3 permissions that can be granted/revoked to/from a file or directory.
They are

r Read
w Write
x Execute
The Read permission is required for any file or directory that requires a Write or an
Execute permission.
Unix has 3 different set of users namely,
Owner/User Owner of the file/Person who created a file
Group Every user in Unix needs to belong to a group(Just like employees in
an organisation belong to a department). Group signifies the other
members of the group to which the owner belongs to.
Others All the other users in the system

r w x r w x r w x
4 2 1 4 2 1 4 2 1
A B C
Figure 3.1 FAP of a file

A Permissions for the owner of the file (u)


B Permissions for users in the same group as that of the owner (g)

Temenos Training Publications 5


C Permissions for all users (o)

One of the most common causes of errors in Unix is insufficient permissions. As a


first step with any problem, always check the permissions on files

Following are the various ways to grand and revoke permissions to files and directories.

Example 1

Grand Read permission to the owner of the file

chmod u+r filename

In the above command, ‘u’ denotes the user/owner of the file, ‘+’, denotes that a permission is to be
granted and ‘r’ denotes the read permission.

Example 2

Revoke Write permission from the group for a specific file

chmod g-w filename

In the above command, ‘g’ denotes the group, ‘-’, denotes that a permission is to be revoked and ‘w’
denotes the write permission.

Example 3

Grant Read and Execute permissions to the Others for a specific file

chmod o+rx filename

In the above command, ‘o’ denotes the ‘Others’, ‘+’, denotes that a permission is to be granted and ‘r’
and ‘x’ denote the read and execute permissions respectively.

Example 4

Grant Read permission for the Group for a directory ‘Training’.

chmod g+r Training

If permissions need to be granted/revoked recursively then use the following


command

chmod –r g+r Training

The –r option used along with chmod enables recursive granting/revoking of permissions.

Permissions can be granted/revoked using the following commands as well.

$ chmod 777 directoryname Change permissions to rwxrwxrwx


$ chmod 666 directoryname Change permissions to rw-rw-rw-
$ chmod 761 directoryname Change permissions to rwxrw---x
$ chmod 555 directoryname Change permissions to r-xr-xr-x
$ chmod –r 777 directoryname Open all the permissions to all the files in this directory
and all files in all subdirectories

3.1.5 Finding Files And Directories

Temenos Training Publications 6


‘find’ is the Unix command that is used to find files and directories in the system.
Example 1
Find out the location of the file ‘training’
find . -name training -print
. Denotes the starting directory. It is this directory from which the
search will begin. Please note that the ‘find’ command always do a
recursive search.
-name Used to specify the name (in other words it will not only look for the
file it will display its full name). It is a recognised switch to the find
command.
Training Name of the file that we are looking for
-print Displays the full pathname when found
The above command will recursively search starting from the current directory a file
called training and will display the path and the name of the file once found.
If the search needs to start from the root(Could be time consuming) then the above
can be modified in the following manner
find / -name training –print
Note that starting directory is the ‘root’.

Other options with the find command


find . -name “ab*” -print Find if only know part of name (NB Quotations)
Go to your own directory and run this command for some
of the files you have created
find / -name “abc1” -type d -print Search for a directory
Starting from the root directory
Can use –type f along with the find command to find
‘only’ files
Killing processes can be done with varying degrees of
severity. Low severity will kill a process ‘gracefully’
releasing any locks or waiting for any related processes
to finish. This prevents serious damage to the system,
such as unreleased locks.
However it may be necessary to kill a command with
greater severity
kill –15 [pid.no] Least severe
kill –9 Most severe
Kill one of your processes. List your processes again, to
see that they have gone. If they haven’t kill them again
with more severity

4.1 .profile FILE

Every user in Unix has a profile file called the “.profile” file. As the name denotes, it

Temenos Training Publications 7


contains the profile settings of a specific user.

When a user is created in Unix, the operating system prompts for the following
information

- The user name


- Password
- Home directory

Once we supply the above information, Unix makes a copy of the .profile file under
the ‘root’(main) directory of Unix and places it under the home directory of the user.

The home directory of the user is the path where the user will be taken to when he
logs in. This is the protected environment where the user would work in.

The .profile file copied under the home directory of the user will contains the following
environmental variables

PS1 : Contains the user’s prompt that has to appear when he logs on to Unix.
HOME : Contains the path of the home directory that the use has to be taken to as
soon as he logs in
PATH : Contains the various paths to be searched for executable files in Unix.
LOGNAME : Contains the user’s login name.
Sample data that would be present in a .profile file.

PS1 : $
HOME : /globus1/train1/demo/demo.run
PATH : .:/bin:/usr/bin:/etc:/u1/uv/bin
LOGNAME : train1

Note :

Type any command at the Unix prompt , like


$ pwd
Unix will list the present working directory. Every command in Unix has an
associated file. As soon as you enter a command in Unix, Unix will automatically search for an
associated file with the same name in order to execute it and give you the desired output. In case
a file with the same name is not found, Unix would display an error message. The PATH variable
contains the various search paths separated by a ‘:’ that UNIX would search for executables. The
‘.’ in the beginning denotes that UNIX has to start the search from the current directory.

Use the following command to display the contents of variables in Unix.


echo $variablename

Temenos Training Publications 8


5.1 Input – Output Redirection
Shell, an integral part of the Unix operating system, which apart from acting as an
interface between the user and the kernel also does input output redirection. Input to the system
usually comes from the keyboard and therefore the keyboard is termed as the ‘Standard Input
Device’. Output of commands, shell scripts etc. usually go to the monitor and therefore the monitor is
termed as the ‘Standard Output Device’. All error messages usually go to the monitor and therefore
the monitor is also termed as the ‘Standard Error Device’. All these standard input/output.error
devices have pre-defined codes attached to them. They are as follows –

Devices Codes

Keyboard (Standard input device) : 0


Monitor (Standard Output Device) : 1
Monitor (Standard Error Device) : 2

5.1.1 Input Redirection:

When the input comes from a device other than the standard input device then it is
termed input redirection.

Example 1

cat filename.

This command cat takes the input from a file and displays the contents of the file.
Here the input is from a file and not from the standard input device. The symbol ‘<’ is associated with
input redirection and it is optional.

cat < filename or


cat filename
cat 0< filename

will result in the same output.

5.1.2 Output redirection :

When the output of a command, shell script etc go to any device other than the
standard output device, then it is termed as output redirection.

Example 1

ls –l > filename

In the above example, the output of ls –l is redirected to a file instead of being


displayed on the monitor. The symbol associated with output redirection is the ‘>’ symbol which is
compulsory.

ls –l > filename and

ls –l 1> filename

will result in the same output.

Temenos Training Publications 9


5.1.3 Error redirection:

Let us understand error redirection with an example.

Example 1

cat file1 2> error.msg

The above command will display the contents of the file with name ‘file1’ if found, else
redirect the error message to the file error.msg. The symbol associated with error redirection is also ‘>’
like output redirection. Therefore whenever error redirection has to be accomplished the code
associated with error redirection ‘2’ has to be attached else Unix will assume it as output redirection.

Note :
During output and error redirection, the output or the error gets written on to the file specified. If
the file mentioned does not exist, Unix will automatically create it and write onto it. In case the file
exists, the file will be overwritten .To prevent overwriting, ‘>>’ symbol can be used. This will
append the contents to the file.

6.1 Editing and Executing Files

Vi is the standard Unix text editor. It has 2 basic modes - Command mode and Insert
mode. It can be quite difficult to get used to at first. When you first enter Vi you are in command
mode. When you type a letter you will perform the command associated with that letter

To go into Insert mode select ‘i’. Now when you type letter they appear on screen as
text. Note that some functions like ‘Home’ and ‘Delete’ do not work in the conventional way

Remember if in doubt press Escape a couple of times to go back to command mode.


Also make sure your Caps Lock key is not on. Commands in capital letters are often different from
those in lower case.

It is usually best to type in all you can in insert mode, then go back in Command
mode to delete any errors. See ‘General Guide to using Vi’ document for further information

$ vi filename Enter this command to create a new file or edit an


existing file. This brings up the vi editor in command
mode
i This takes you into ‘Insert mode’ . Characters typed in
now, will appear as text in your document.
When you have finished typing press ‘Escape’ 2 times to return to command mode. Your system
should beep
:x This saves and exits the file
:wq To write and quit (as above)

Temenos Training Publications 10


Summary

 An operating system is a software that controls the hardware and peripheral devices,
manages the memory, schedules, prioritizes, initiates and controls jobs, manages file creation
and interprets commands
 Unix comprises of the shell and the kernel
 Shell is an interface between the user and the kernel. It is a protected environment for every
user to work in
 Kernel is the heart of the Unix operating system. It directly communicates to the hardware.
 Bin, lib, etc, dev, user, usr, tmp are some of the system directories that get installed when
Unix is installed
 pwd, mkdir, cd, cd .., rmdir, rm, ls etc are some of the basic commands in Unix
 chmod is the command that is used to grant and revoke permissions to files and directories
 .profile is a hidden file that holds the profile settings of a Unix user
 HOME, PS1, PATH, LOGNAME are some of the environmental variables that are set in the
.profile file
 The symbol for input redirection is ‘<’ and the id of the standard input device(Keyboard) is 0
 The symbol for output redirection is ‘>’ and the id of the standard output device(Monitor) is 1
 The symbol for error redirection is ‘>’ and the id of the standard error device(Monitor) is 2
 vi is the Unix editor.

Temenos Training Publications 11

Вам также может понравиться