Showing posts with label Unix Fundamentals. Show all posts
Showing posts with label Unix Fundamentals. Show all posts

THE VI EDITOR

A combination of << and cat can be used to add lines to a file, and sed and file redirection can be used to modify the contents of a file. These tools are rough and awkward, and when it's time to either create new files or modify existing ones, a screen-oriented editor is needed. In UNIX, the screen editor of choice is called vi. vi is Visual Editor, in short.
There are a number of editors that may be included with the UNIX system, including ed, ex, vi, and EMACS. The latter two use the entire screen, a big advantage, and both are powerful editors.
The vi is a modal editor. A mode is like an environment. Different modes in vi interpret the same key differently. For example, if the user is in insert mode, pressing the A key adds an ‘a’ to the text, whereas in command mode, pressing the A key enters a, a single key abbreviation for the append command. Pressing Esc always returns the user to the command mode and if the system is already in command mode, it beeps to remind the user of that fact.
The second important characteristic of vi is that it's a screen-oriented program. It must know what kind of terminal, computer, or system that the user is using to work with UNIX. This probably won't be a problem because most systems are set up so that the default terminal type matches the terminal or communications program that the user is using.
The vi command by itself starts the editor, ready for the user to create a new file. The vi command with a filename starts vi with the specified file, so that the user can modify that file immediately.
There are primarily two types of editors available under UNIX namely,
 The ed line editor comparable to EDLIN IN DOS.
 The vi editor, a full screen editor.
CAUTIONS ABOUT VI
 The vi editor is very user unfriendly and offers no help facility.
 The same keystrokes can have more than one meaning depending on current mode and the key combinations are in no way mnemonic.
 The editor is fanatically case-sensitive
 The vi command does not lock a file while editing it. So it is possible that more than one user can edit it at the same time. The version of the file saved last is the one that is retained.
SALIENT FEATURES OF VI
 vi is omnipresent and is available in all UNIX systems.
 vi is fast.
 vi has got powerful UNDO features.
 vi has got a lot of support.
THE BASICS OF VI
vi and ex commands are compatible with each other but ex is a line editor only. All editing in vi is done in a buffer on a copy of the original until it is saved.
vi works in 3 different modes:
 Command mode
When in this mode, all keys are interpreted as commands.
 Insert mode
When in this mode, the keys are echoed in the edit buffer.
 ex Escape or Esc colon (:) mode
When in this mode, the keys are interpreted as commands and are echoed at the command line at the bottom of vi screen.
The bottom most line in vi is the command line where all commands in Esc: mode and messages are displayed. There are no error messages in vi - only beeps indicate the errors. Control or special keys cannot be used and the arrow keys cannot be used for cursor movement on most terminals.
IMPORTANT KEYS AND COMMANDS
KEY MEANING
The Esc key
Returns vi to the command mode and cancels partially formed commands.
The Return or Enter key Executes commands entered in ex Esc mode and starts a new line in Ins mode. In command mode, it simply takes the cursor to next line.
Forward Slash
“/” Specifies a string to be searched for in the existing file – the string appears in the status line following the forward slash “/” and is echoed at the beginning of the command line.
Question Mark
“?” Same as the forward slash key but the search is done backwards.
Colon
“:” To enter “ex Esc” mode - the command appears at the command line preceded by “:”.
Tilde
“~” Appears at the beginning of every line in vi.

INVOKING VI
COMMAND FUNCTION
vi Starts an empty edit buffer.
vi Edits the file or creates the file if it does not exist.
vi +3 Opens the file and goes to the third line.
vi + /bye Searches for the first occurrence of “bye” in the file .

CURSOR MOVEMENTS - NAVIGATING IN A FILE
The arrow keys should not be used to move, in vi though some terms do support them.









All vi commands involve the use of alphabetical keys, alone or in combination with Shift and Control Keys.
MORE CURSOR CONTROL IN VI
Commands marked with # can be invoked with a number preceding them indicating repeat count.

COMMAND FUNCTION
#w Moves forward by a word
#W Moves forward by a word till next space
#e Goes to the last character in the word
#E Same as #e but ignores punctuation
f[character] Upto specified character in current line – right
F[character] Same as above but - left
t[character] Same as f[c] but one character before
T[character] Same as F[c] but one character before
#b Go back by a word
#B Same as #b , but ignores punctuation
0 or ^ Goes to the beginning of a line
#$ Goes to the end of the line
#G Go to the line number # of the file
L Go to the last line
SCREEN COMMANDS
COMMAND FUNCTION
Ctrl-F Scrolls a screen forward - except last two lines
Ctrl-B Scrolls a screen backwards
Ctrl U Scrolls forward by half a screen
Ctrl-D Scrolls backwards by half a screen
Ctrl R or Ctrl L Re-draw and clear messages
Ctrl-G Status of edit

DELETING DATA
COMMAND FUNCTION
#dd Delete current line where cursor is present
#dw Delete words from the cursor position
#dW Same as #dw but includes punctuation
#x Delete character at the cursor position
#X Delete character before the cursor
d$ or D Delete current line from the cursor position to the end of the line
d0 Delete current line from the cursor position up to start of line

TEXT INSERTION
COMMAND FUNCTION
i Invokes insert mode and inserts before character
I Same as i but insertion at beginning of line
O Opens a blank line above current line
o Opens a blank line below the current line
a Same as i but inserts after cursor
A Same as a but appends at end of line

JOINING
The J command causes the lines of text below the current line to get joined with the current line.
LINE NUMBERING
In ex Esc mode the command: set nu sets line numbers and: set nonu removes them.
Block moves - Colon Commands
Copy 2nd line after 5th :2co5
Multiple lines copy :1,3co$
$ for last line, 1,3 is range
Move to line 6 :6
Delete three consecutive lines now (#dd) :3dd
Yet another way -
Symbol Means
“.” Current line where cursor is
“+” Line after current line
“-” Line before current line

Delete 3 lines below current line including current line :.,+3d
, denotes range
Move line 1 to after line 3 : 1mo3
Lines 1,2 and 3 after line 5 : 1,3mo5

SAVING AND EXITING
COMMAND FUNCTION
“:q!” Quit and abandon changes
“:wq” Save and quit
“:x” Replace old copy with new and quit
“ZZ” Save and Quit (from command mode)

REPLACING EXISTING TEXT
COMMAND FUNCTION
“r” Replace a single c at current cursor position
“R” Invoke insert mode and go on replacing till Esc
“s” Replace a single c with text - insert mode - continues till Esc - $ appears after c to be replaced
“S” Replace current line with new text - blanks entire current line - invokes insertion mode
“cw” Replaces a word - $ at end of word - replacement continues after end of $ if word size greater than current word
“C” Same as cw but acts on rest of line from current cursor position
“cc” Same as C but entire line

CUT AND PASTE
COMMAND FUNCTION
“#yy” Yank (cut !) specified number of lines into yank buffer
“p” Paste from yank buffer below current line
“P” Same as p but above current line

9 LIVES!
vi offers undo of as many as 9 line deletions (each deletion may be a group of lines) apart from the usual undo “u”. This follows the LIFO (Last In First Out) rule –
“#p or “#P - # is 1 to 9
PATTERN SEARCHING
Basic commands are:
/g search for pattern “g” forwards
?g search for pattern “g” backwards
n repeat the last / or ? command
SUBSTITUTION
These are ‘:’ (colon) commands.
To replace “string” by “string1” on the current line -
:s/string/string1/ - replaces only first occurrence
:s/string/string1/g - replaces all occurrences
Substitution can occur on multiple lines if range is specified -
:1,9s/string/string1/g
For substituting throughout the file -
:1,$s/string/string1/g
OR
:g/string/s//string1/g - note the double slash.
MACROS
vi macros are created using the “abbr” command -
:abbr uo “UNIX operating system”
In insert mode press “u” “o” and press “Spc bar” or “Enter” - the macro string is inserted at current cursor position.
:abbr -lists the macros defined
Macros can be unabbreviated or deleted -
:una uo - deletes macro uo
MAPPING KEYS TO COMMANDS
The “map” command maps a user defined key to a specific function.
The cursor movement keys j, k, l, h may be mapped by default to their respective cursor movement keys.
To map the key “ctrl-X” to the “wq” command –
:map ^X :wq^M
- note that ^M signifies , in vi “^” is typed by typing “Ctrl-v”

 Note : All characters typed with “ctrl” is taken as upper-case
An “unmap” command is available to cancel assignments -
:unmap ^X
Mapping in insert mode done using the “map!” command -
:map! ^x Sam fox
- In insert mode “Ctrl-x” will insert “Sam fox in the editor
An insert mode map cannot be unmapped unlike abbr
:map displays only keys mapped to command mode functions
RUNNING SHELL COMMANDS FROM WITHIN
 “:sh” temporarily “shells out” to $ and “ctrl-d” returns the shell
 “: !“ “!” stands for run which will directly run

READING COMMAND OUTPUT FROM WITHIN
This is done using “:r!” -
:r! date - reads in output of “date” at current cursor position
:r! banner Hi there

EDITING MULTIPLE FILES SIMULTANEOUSLY
vi can take multiple file arguments -
$ vi file1 file2 ..... OR metacharacters -
$ vi f*
:args
[file1] file2 file3 - The bracketed file taken for display/edit
:n - Switches to next file in list
:args
file1 [file2] file3
:rew
:args
[file1] file2 file3 -i.e. “rew” rewinds to file1
:rew! - discards changes and coercively rewinds
:e file - edits file outside chosen list but :args will not show this file in list
READ AND WRITE ACROSS FILES
Write first five lines of text from file1 to file2-
:1,5 w! file2
Read same text from file2 to file1-
:1,5 r file2
THE “SET” OPTIONS
The “set” option helps customise the vi environment -
: set all - displays all “set” options available
The more common options are:
SET COMMAND DEFAULT DESCRIPTION
:set number,nu nonumber Line numbers
:set showmode noshowmode Display vi mode
:set autowrite,aw noaw Save on n, rew, !
:set autoindent,ai noai Indent as first line
:set beautify,bf nobeautify Control characters omitted
:set directory,dir dir-/tmp Sets temporary directory
:set errorbells,eb noeb No error beep
:set ignorecase,ic noic Uppercase as lowercase
:set magic magic Allow all metacharacters
:set mesg nomesg Inhibit Write messages
:set redraw noredraw Act as intelligent terminal
:set scroll scroll=half window No. of lines to scroll
:set shell sh=/bin/sh Set which shell
:set term value of TERM Set which terminal
:set terse noterse Error messages terse
:set warn warn Warn save on Shell Esc
:set window dependent window speed No of lines in text window
:set wrapscan,ws ws Wrap pattern on find
:set warpmargin,wm wm=0 Blank screen margin right
:set writeany, wa nowa Allow writes freely

The “.exrc” file
vi reads .exrc before loading and all options like map,abbr and set can be entered here so that they are permanent for that vi session -
$ vi .exrc
set nu
set showmode
abbr uo UNIX operating system
map ^k :r! date^M
:wq

SEARCH RELATED COMMANDS

find
Description : The find command is used to find files matching a certain set of selection criteria.

$ find . –name file1 –print

$ find . –name core –exec rm {} \;


grep
Description : This command is used to locate a particular string. Wild card characters can also be used for string specifications.

Example:
grep
$ grep unix *.txt

$ grep –i unix *.txt

DISK TOOLS
df
Description : The df command displays the amount of disk space occupied by mounted or unmounted file systems, directories, or mounted resources, the amount of used and available space, and how much of the file system's total capacity has been used.

Example:

$ df
/ (/dev/dsk/c0t0d0s0 ): 967332 blocks 330533 files
/usr (/dev/dsk/c0t0d0s3 ): 779990 blocks 224501 files
/proc (/proc ): 0 blocks 7862 files
/dev/fd (fd ): 0 blocks 0 files
/var (/dev/dsk/c0t0d0s4 ): 1190894 blocks 366000 files
/oracle (/dev/dsk/c0t0d0s6 ): 2264162 blocks 322103 files
/opt (/dev/dsk/c0t0d0s5 ): 553948 blocks 356316 files
/users (/dev/dsk/c0t0d0s7 ): 8689026 blocks 1014784 files
/wd (/dev/dsk/c1t1d0s0 ): 391628 blocks 828668 files
/database2 (/dev/dsk/c1t1d0s1 ): 1208750 blocks 754741 files
/wd2 (/dev/dsk/c1t1d0s3 ): 2414238 blocks 397829 files
/database1 (/dev/dsk/c1t2d0s0 ): 2266602 blocks 1508165 files
/wd1 (/dev/dsk/c1t2d0s1 ): 1498746 blocks 417826 files
/tmp (swap ): 2828544 blocks 171037 files

$ df -k
Filesystem kbytes used avail capacity Mounted on
/dev/dsk/c0t0d0s0 640967 157301 425979 27% /
/dev/dsk/c0t0d0s3 1018191 628196 328904 66% /usr
/proc 0 0 0 0% /proc
fd 0 0 0 0% /dev/fd
/dev/dsk/c0t0d0s4 721335 125896 537733 19% /var
/dev/dsk/c0t0d0s6 3099287 1967206 1070096 65% /oracle
/dev/dsk/c0t0d0s5 721335 444361 219268 67% /opt
/dev/dsk/c0t0d0s7 8572345 4227844 4258778 50% /users
/dev/dsk/c1t1d0s0 8261393 8065579 113201 99% /wd
/dev/dsk/c1t1d0s1 6196234 5591859 542413 92% /database2
/dev/dsk/c1t1d0s3 2940743 1733624 1148305 61% /wd2
/dev/dsk/c1t2d0s0 12390920 11257619 1009392 92% /database1
/dev/dsk/c1t2d0s1 5022314 4272941 699150 86% /wd1
swap 1417912 3640 1414272 1% /tmp
du
Description : summarize disk usage
The du command reports the number of disk blocks used for each directory and subdirectory, and the files found there. It has an option to display the number of blocks of just the "top level" directory.


Example:
$ du
2 ./tmp
22 ./web/images/_vti_cnf
174 ./web/images
244 ./web
828 ./index/temp
836 ./index
14 ./.fm/users/balu1
16 ./.fm/users
18 ./.fm
14 ./.bin
2 ./database/utidb
2 ./database/utiarch
6 ./database
974 ./sql
32738 ./gary
274 ./TMP
2 ./.wastebasket
396 ./scripts
28 ./tape
16 ./menu
132258 .

Using Commands

Logging in UNIX performs several actions that prepare the user and the system for each other. These include performing system accounting, initialising the user environment, and starting a command interpreter commonly called a shell. Commands are how the user tells the system to do something. The command interpreter recognises these commands and passes the information off to where it is needed. UNIX systems originally came with a command interpreter called the Bourne Shell (usually referred to as sh). This shell is still available on most UNIX computer systems. A newer shell that is common to most UNIX systems is the C Shell (referred to as csh). Another commonly used, but not as pervasive, shell is the Korn Shell (referred to as ksh). Among different shells, there is some variation of the commands that are available.

What is a Command?

A UNIX command is a series of characters that the user types. These characters consist of words that are separated by white spaces. A white space is the result of typing one or more Space or Tab keys. The first word is the name of the command. The rest of the words are called the command's arguments. The arguments give the command information that it might need, or specify varying behaviour of the command. To invoke a command, the user can simply type the command name, followed by arguments (if any). To indicate to the shell that he is done with typing and is ready for the command to be executed, he should press Enter. For instance, the date command takes no arguments and hence, if the user enters ‘date’ at the prompt and press Enter, he should see that the computer has printed the current date and time. The echo command takes arguments. The echo command writes, or echoes, these arguments out to the screen.

UNIX commands use a special type of argument called an option. An option commonly takes the form of a dash made by using the minus sign key, followed by one or more characters. The options provide information to the command.

Redirecting Input and Output

One very pervasive concept in UNIX is the redirection of commands' input and output. Before looking at redirection though, it is a good idea to look at input and output without modification. UNIX uses the word standard in this subject to mean the default or normal mode. Thus, UNIX has the term standard input, which means input coming from the default setting, and the term standard output, which means output going to the normal place. When the user first logs in to the system, and the shell executes, the standard input is set to be what is typed at the keyboard, and the standard output is set to be the display screen.

UNIX shells have special characters that signify redirection. Output redirection is signified by the > character and input redirection is signified by the <>

cat >

where is a name of the user’s choice.

Pipes are one of the ways UNIX allows users to combine several commands. The pipe is signified by the vertical bar (|) symbol. A pipe is a means of taking the output of one command and redirecting it as the input of another command. Assume that the user wants to know how many files exist in his current directory. The ls command will list all the files in the current directory and the number of files can be counted. But UNIX has a command that counts the number of characters, words, and lines of input and displays these statistics. Therefore, these two commands can be combined to give the number of files in the directory. One way of doing it would be

ls -l | wc -l

Combining the two commands via a pipe takes the output of the first command (the long directory listing) and gives it to the input of the second command.

Managing the Password

During login, UNIX asks the user to enter his password. If he logs in for the first time, his password is what the system administrator configured. One of the very first things he should do after logging in is change his password so that, none, including the system administrator, knows what it is. This can be done via the ‘passwd’ command. If the user forgets his password, even the system administrator, cannot look it up. There is no alternative except to reset the password to a value.

Configuring Your Environment

In order to make using the shell easier and more flexible, UNIX uses the concept of an environment. Your environment is a set of values. You can change these values, add new values, or remove existing values. These values are called environment variables—environment because they describe or define your environment, and variables because they can change.

Viewing and Setting Environment Variables

Every user's environment looks a little different. Type the env command with no arguments. The output formatting and variable names depend on which shell you are using and how your system is configured. A typical environment might include some of the following:

$ env

PATH=/usr/local/bin:/usr/xpg4/bin:/usr/ccs/bin:/opt/SUNWspro/bin:/usr/sbin:/bin:/usr/bin:/etc/b2k/install:/etc/b2k:/usr/local/lib:/oracle/ora92-64

/bin:/unix/ibin:/FINACLE/V7/app/util:/FINACLE/V7/app/cust/INFENG/exe:/FINACLE/V7/app/cust/INF

B2K_INSTALL_ID=uet7

LOGNAME=prakash1

SHELL=/bin/ksh

HOME=/users/prakash1

TERM=vt220

PWD=/users/prakash1

Some Finacleâ Related variables

$ echo $TBA_PROD_ROOT

/FINACLE/V10/app

$ echo $TBA_MRT

/FINACLE/V10/app/cust/INFENG/mrt

$ echo $TBA_SCRIPTS

/FINACLE/V10/app/cust/INFENG/scripts

$ echo $TBA_COPT_FILE

coptdefault.rip

$ echo $TBA_UTIL

/FINACLE/V10/app/util

Sometimes the number of variables in your environment grows quite large, so much so that you don't want to see all of the values displayed when you are interested in just one. If this is the case, you can use the echo command to show an environment variable's current value. To specify that a word you type should be treated differently—as a value of an environment variable—you immediately precede the variable name with a dollar sign ($). Be careful not to type any white space between the $ and the word. One of the variables in the example is HOME. You probably have this variable in your environment, too. Try to display its value using echo.

You can create a new environment variable by simply giving it a value. If you give an existing variable a value, the old value is overwritten. One difficulty in setting environment variables is that the way you set them depends on the shell you are using.

In order for your screen to display the output correctly, the environment variable TERM needs to have a reasonable value. This variable name comes from the times when terminals were used as displays (before PCs and graphics displays were common). Different terminals supported varying output control. Therefore, UNIX systems have various terminal types that they support. These are not standard, so you need to find out which terminal type to use from your support personnel. If you are using a PC to connect to a UNIX system, your PC is running a terminal emulation tool. Most of these tools have the capability to emulate several types of terminal. The important point here is to make sure that your emulator and your TERM variable are the same (or compatible). You can start off by seeing what your TERM variable is set to, by entering ‘echo $TERM’.

Shell Startup Files

Where do all these environment variables come from? Well, the system sets up various ones for the user. And each user commonly sets up others during the login process. Yes, you may be doing this without even knowing it. During the startup, which happens at login, a shell is started. This shell automatically looks in a special place or two for some startup information. One of these places is the user’s home directory. The startup information in the user’s home directory is found in special files. The specific shell the user is using will determine the name of the particular file. When the shell starts up, it examines this file and performs whatever actions are specified. One of the common actions is to give values to environment variables. This action is called initialising or setting the values.

One environment variable that is commonly set in a user's shell start-up file is the PATH variable (or lowercase path for C-shell users). This variable's value is a list of places (directories) on the system where the shell should look to locate a command. Each command the user types, is physically located in a file somewhere on the UNIX file system. It is possible for the same command name to be located in different places (and to have either the same or different behaviour when executed). Say that you have a program called my_program that is stored in your home directory, and your friend has a program called my_program, which is in her home directory. If you type my_program at the prompt, the shell needs to know where to look to find the storage location of my_program. The shell looks at the value of the PATH variable and uses the list of directories as an ordered directory search list. The first directory that has a my_program stops the search, and the shell executes that file. Because all files within a single directory must be unique, this gives a straightforward and sufficient method for finding executables (commands).

SOME IMPORTANT UNIX COMMANDS

GENERAL COMMANDS
banner
Description : This command prints the argument supplied, in large letters so as to appear like a banner.

Example:
$ banner hello

# # ###### # # ####
# # # # # # #
###### ##### # # # #
# # # # # # #
# # # # # # #
# # ###### ###### ###### ####


cal
Description : The command can print the calendar for any year in the range 1 to 9999. The command, when given no arguments, generally prints the calendar for the previous, current and following months.

Example:
$ cal
July 2003
S M Tu W Th F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

$ cal 8 1947
August 1947
S M Tu W Th F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

 An unusual calendar is printed for September 1752. That is the month 11 days were skipped to make up for lack of leap year adjustments. To see this calendar, type:
$ cal 9 1752
date
Description : This command is used to display the current system date or set the system date.

Example:
$ date
Mon Jul 28 17:08:26 IST 2003

$ date +%a
Mon

$ date +%A
Monday

$ date +%b
Jul

$ date +%B
July

date +%d
28

$ date +%D
07/28/03

Try the output for other formats like x, X, c, C. An interesting option is j
echo
Description : Displays the text supplied as argument or the contents of the variable that is supplied as argument.

Example:
$echo Welcome to world of UNIX
Welcome to world of UNIX

passwd
Description : This command is used to change the user’s password or to create a new password. The argument is used by the super user alone.
pg
Description : This command displays the contents of the files specified, page-wise.

Example:


pipe “|”
Description : Channels the output of command1 as input for command2.

Example:
$ls /etc | pg
acct dmi hosts magic nsswitch.dns
aliases driver_aliases inet mail nsswitch.files
apache driver_classes inetd.conf minor_perm nsswitch.ldap
auto_home dumpadm.conf init mkfs nsswitch.nis
auto_master dumpdates init.d mknod nsswitch.nisplus
autopush ff initpipe mnttab opasswd
b2k fmthard ignitable motd openwin
cfgadm fn install mount opt
chroot format ioctl.syscon mountall ouser_attr
clri format.dat iplanet mvdir pam.conf
coreadm.conf fs iu.ap name_to_major passwd
cron fsck killall name_to_sysnum path_to_inst
cron.d fsdb krb5 nca path_to_inst.old
dacf.conf fstyp labelit ncheck power.conf
datemsk ftpd lib net ppp
dcopy fuser link netconfig printers.conf
default getty llc2 netmasks profile
defaultrouter group log networks project
device.tab grpck logadm.conf nfs protocols
devlink.tab gss logindevperm nfssec.conf prtconf
dfs gtk lp nodename prtvtoc
dgroup.tab halt lu nscd.conf publickey
dhcp hostname.hme0 lvm nsswitch.conf pwck
sleep
Description : This command is used to delay a process for a period of time.

Example:
$sleep 30
wc
Description : This command does a count operation on the file given as argument based on the options given.
Example:
$ wc /etc/passwd

58 70 2275 /etc/passwd

Try the options –l, -w & -c
who
Description : This command lists all the users who are currently logged in, along with the time of login and the terminal details.

Example:
$who
ram2 pts/8 Jul 29 15:59 (blrtwr18282.ad.infosys.com)
rajesh2 pts/9 Jul 29 09:57 (blrkec19005.ad.infosys.com)
balu1 pts/13 Jul 29 08:36 (blrkec18886.ad.infosys.com)
root pts/14 Jul 29 08:37 (blrkec18886.ad.infosys.com)
finadm pts/15 Jul 29 08:40 (blrkec18886.ad.infosys.com)
fabadm pts/16 Jul 29 09:26 (blrtwr15900.ad.infosys.com)
fabadm pts/3 Jul 29 09:33 (blrtwr15900.ad.infosys.com)
anil1 pts/4 Jul 29 09:40 (blrkec06585.ad.infosys.com)
venkat1 pts/5 Jul 29 09:42 (blrkec16592a.ad.infosys.com)
balu2 pts/6 Jul 29 10:11 (blrkec18886.ad.infosys.com)
kvs1 pts/18 Jul 29 11:45 (blrkec18900.ad.infosys.com)
kvs2 pts/19 Jul 29 11:46 (blrkec18900.ad.infosys.com)
prakash1 pts/20 Jul 29 14:39 (blrkec18930.ad.infosys.com)

ls
Description : This command lists the files if the argument is a directory. If the argument is a file, it lists the details about the file.
Example:
$ls /users

The above command does not give much information about the files
$ls –l /users
drwxrwxr-x 3 root bin 512 Dec 1 2000 openwin
drwxrwxr-x 8 root sys 512 Dec 5 2000 opt
-r-------- 1 root sys 2985 Aug 11 10:40 oshadow
-rw-r--r-- 1 root sys 1742 Dec 5 2000 pam.conf
-r--r--r-- 1 root sys 4461 Aug 8 17:46 passwd
-r--r--r-- 1 root sys 3498 Dec 12 2000 path_to_inst
-r--r--r-- 1 root sys 3498 Dec 12 2000 path_to_inst.old
-rw-r--r-- 1 root sys 190 Dec 1 2000 printers.conf
-rw-r--r-- 1 root sys 1501 May 17 11:42 profile
-rw-r--r-- 1 root other 700 Dec 14 2000 profile.preaw
The above command lists the details of the files
-rw-r--r-- Indicates the file permissions
1 Indicates the number of links
root is the user ID of the file's owner
other is the group ID of the group
700 is the size of the file in bytes
Dec 14 2000 is the time stamp—the date and time when the file was last modified
profile.preaw is the name of the file

The first and second columns require a bit more explanation. The first column is a ten-character field that indicates the file's mode—its type and its permissions. In the first line of the list, the file's mode is –rw-r--r—. The first character tells the file type, which is a hyphen (-) for regular files, and d for directories. In this example, the first two items are directories and rest ordinary files.
The next nine characters of the entry are the file's permissions—three sets of three characters that control which users may access a file and what they can do with it. The first set of three characters controls what the file's owner can do; the second set of three characters controls what others in the group can do; and the third set of three characters controls what all other users can do. Each set of three characters show read (r), write (w), and execute (x) permission, in that order. A hyphen (-) means that the permission is denied.
The second column of the long listing is the number of links to this file.

cd
Description : This command enables the user to change his current working directory and puts him in the directory supplied as argument. If no argument is given, it takes him to his home directory.

Example:
$ cd

$ cd /users

$ cd ../prakash1

pwd
Description : This command prints the path name of the current working directory.

Example:
$ pwd
head
Description : This command displays the first n lines of the files specified as arguments

Example:
$ head /etc/services
$ head -20 /etc/services

tail
Description : This command displays a part of the file, generally the last part, beginning at a designated place.

Example:
$ tail /etc/services
$ tail -20 /etc/services

finger
Description : This command lists the users who are logged on and details about their terminal, time of login and so on.

Example:
$ finger prakash1
Login name: prakash1
Directory: /users/prakash1 Shell: /bin/ksh
On since Jul 29 14:39:24 on pts/20 from blrkec18930.ad.infosys.com
No unread mail
No Plan.
man
Description : This command displays the reference manual pages related to the command given as argument.

Example:
$ man vi
Reformatting page. Wait... done

User Commands finger(1)

NAME
finger - display information about local and remote users

SYNOPSIS
finger [ -bfhilmpqsw ] [ username... ]

finger [-l ]
[ username@hostname1[@hostname2...@hostnamen] ... ]

finger [-l ] [ @hostname1[@hostname2...@hostnamen] ... ]

DESCRIPTION
By default, the finger command displays in multi-column for-
mat the following information about each logged-in user:
o user name
o user's full name
o terminal name (prepended with a `*' (asterisk) if
write-permission is denied)

FILE MANAGEMENT
cat
Description : This command can concatenates files or displays their contents.

Example:
$ cat /etc/services

$ cat -n /etc/services

cmp
Description : This command is used to compare the contents of the two file supplied as arguments.

Example:
$ cmp file1 file2
comm
Description : This command is used to select or reject lines common to two sorted files. It produces a three columnar output where the columns contain lines only in file1, lines only in file2 and lines in both files respectively.

Example:
$ comm file1 file2
cp
Description : This command is used to copy files from the given source to the specified destination.

Example:
$ cp file1 file2
mkdir
Description : This command is used to create a directory.

Example:
$ mkdir dir1

mv
Description : This command moves or renames files and directories.

Example:
$ mv file1 file2

rm
Description : This command is used to remove files or directories.

Example:
$ rm file1
rmdir
Description : This command removes the directory specified as argument provided it is empty.

Example:
$ rmdir dir1
cut
Description : This command is used to cut out columns from a table or fields from each line of one or more files. The fields as specified by list can be fixed length, that is, character positions as on a punched card (-c option), or the length can vary from line to line and be marked with a field delimiter character like Tab (-f option). If no files are specified, cut reads from the standard input.

Example:
$ cut -c 1-10 /etc/services
ln
Description : A link is a directory entry referring to a file; a single file (together with its size, all its protection information, and so on) may have several links to it. There are two kinds of link: hard links and symbolic links. By default ln makes hard links. A hard link to a file is indistinguishable from the original directory entry; any changes to a file are effective independent of the name used to reference the file. Hard links may not span file systems and may not refer to directories. This command is used to make a link to a file or directory. A symbolic link contains the name of the file to which it is linked; this file does not need to exist prior to the symbolic link.

Example:
$ ln file1 file2

$ ln –s file1 file2

$ ln –n file1 file2


FILE SECURITY
chgrp
Description : This command is used to change the group id of the files specified to the group id specified as argument.

Example:


$chgrp
$ chgrp dba file1
$ chgrp – R dir1 dba
 chgrp can be restricted in Solaris platform based on the parameter set rstchown = 1 in the /etc/system file.
chown
Description : This command is used to change the owner id of the files specified as arguments to the owner id specified.

Example:
$chown
$ chown dba file1
$ chown – R dir1 dba

chmod
Description : This command changes the access permissions of a file or directory.

Example:
$ chmod 755 file1
$ chmod – R 755 dir1
$ chmod u+x file1
$ chmod u+rwx file1
$ chmod o-rwx file1

MAIL RELATED COMMANDS
mail
Description : mail provides a flexible environment for sending and receiving messages electronically. For reading messages, mail provides commands to allow saving, deleting, and responding to messages. For sending messages, mail allows editing, reviewing, and other modification of the message as it is entered.
Example:
$mail recipient
$ mail trg1
mesg
Description : This command is used to permit or deny messages sent to a terminal.

write
Description : This command is to copy lines from your terminal to that of another user. When first called, it sends the message:
Message from your-logname your-tty ...

talk
Description : The talk utility is a two-way, screen-oriented communication program.

When first invoked, talk sends a message similar to:
Message from TalkDaemon@ her_machine at time ...
talk: connection requested by your_address
talk: respond with: talk your_addressto the specified address. At this point, the recipient of the message can reply by typing:

talk your_address

DIFFERENT PRIVILEGES FOR DIFFERENT USERS

UNIX systems have built-in security features. Most users cannot set up a new user account nor do other administrative procedures.
The user root is a special user, sometimes called a super-user, which can do anything at all on the system. This high degree of power is necessary to fully administer a UNIX system, but it also allows its user to make mistakes and cause system problems. For this reason, the user should set up a personal account for himself that does not have root privilege. Then, his normal, day-to-day activities will affect only his personal environment and the user will be in no danger of causing system-wide problems.
In a multi-user, non-personal environment, the user is most likely to have only user, and not super-user privileges. This security is even more important when more than one person is involved because one mistake by the root can affect every user and the entire system.
UNIX also has security to help prevent different users from harming each other on a multi-user system. Each user owns his or her environment and can selectively let groups or all others have access to this work. If the user is doing private work in one area that no one else should be allowed to see, then he should restrict access to the owner (himself). If the user and his team members are working on a group project, he can restrict access to the owner (himself) and everyone in his group. If this work should be shared with many or all people on the system, then he should allow access to everyone.
LOGGING OUT
When the user is done using the system, he should log out to prevent other people from accidentally or intentionally getting access to his files. The normal way to log out from almost any shell is to type exit. This causes the shell to exit, or stop running. When the user exits from his login shell, he logs out. Some shells, depending on the configuration, will also log the user out when he types the end-of-file character, typically Control + D.

WORKING WITH UNIX

This section discusses the process of logging into the UNIX operating system and the prerequisites for the same.
LOGGING IN
The user needs to have a user name for logging into the UNIX operating system and using it.
USER ACCOUNT SETUP
After a UNIX system is booted, the user cannot simply start using it like any PC. Before the user can access the computer system, someone—usually the system administrator—must configure the computer for use.
The user must know two things before he can start using the system: his user name and password. The user name is a unique name that identifies the user to the system. The system administrator, before creating a user name, will verify that no one else on the system has the same name before allowing the new user to have it. The password that has been assigned to the user is a temporary string that allows him to initially access the computer system. The initial password isn't of any real importance because the user should change it to something of his choice the first time he logs in to the system.
Whenever a new user is created by the system administrator or the super user, the following details have to be entered in the /etc/passwd file:
User Name
User’s Password
User_Id
Group_Id
User Description
Home Directory



a /etc/passwd file
LOGGING IN TO THE SYSTEM
Once the system administrator has done the necessary groundwork required for a new user, the new user can log in. The system will prompt (ask) the new user for his user name by printing

Login:
The user should then enter his user name. Next, UNIX will prompt him for his password by printing
Password:
The user should enter his password. As the password is being typed, the user will not be able to see the characters he typed, for obvious security reasons so that no one else can get to know his password by looking at the screen when the user logs in.
If the user types everything correctly and the system administrator has everything set up correctly, the user should be able to log in and use the system. On logging in successfully, the system puts the user on to his home directory, the directory allotted by the super user for the user to work in. If the system displays a message saying Login Incorrect, then the user may have typed his user name or password incorrectly and hence the system rightfully denies access for working.

THE FUNCTIONS OF A SHELL

It doesn't matter which of the standard shells are chosen, for all three have the same purpose: to provide a user interface to UNIX. To provide this interface, all three offer the same basic functions:
Command line interpretation
Program initiation
Input-output redirection
Pipeline connection
Substitution of filenames
Maintenance of variables
Environment control
Shell programming
Discussing the functions of the shell in detail is beyond the scope of this material.
THE FILE SYSTEM
One of UNIX's greatest strengths is the consistent way in which it treats files. Although some operating systems use different types of files each requiring unique handling, you can handle most UNIX files the same. For instance, the cat command, which displays a disk file on your terminal screen, can also send the file to the printer. That is, in UNIX, all devices are addressed as files. As far as UNIX is concerned, the printer and your terminal look the same and they look like any other UNIX file. UNIX also doesn't distinguish between files that you create and the standard files that come with the operating system—as far as UNIX is concerned, a file is a file is a file. This consistency makes it easy to work with files because you don't have to learn special commands for every new task. Often, the same command can be used for several purposes. This makes it easy to write UNIX programs because the user usually doesn't have to worry whether he’s communicating to a terminal, a printer, or an ordinary file on a disk drive. Besides, this ensures the effective control of the access to any device, which according to UNIX, is just another file.
3.3.1. THE TYPES OF UNIX FILES
There are four types of UNIX files namely,
Regular files
Directories
Special or Device files
FIFO files
REGULAR FILES
Regular files hold executable programs and data. Executable programs are the commands (such as cat) that you enter. Data is information that you store for later use. Such information can be virtually anything and there is no specific order of format enforced in the way the information is stored.
These files can be visualised as the leaves in the UNIX tree.
DIRECTORIES
Directories are files that contain other files and subdirectories, just as a filing cabinet's drawers hold related folders. Directories help you organise your information by keeping closely related files in the same place so you can find them later. For instance, the user might save all spreadsheets in a single directory instead of mixing them with other unrelated files.
The kernel alone can write the directory file. When a file is added to or deleted from this directory, the kernel makes an entry.
A directory file can be thought of as the branch of the UNIX tree.
SPECIAL OR DEVICE FILES
These files represent the physical devices. Files can also refer to computer hardware such as terminals and printers. These device files can also refer to tape and disk drives, CD-ROM players, modems, network interfaces, scanners, and any other piece of computer hardware. When a process writes to a special file, the data is sent to the physical device associated with it. Special files are not literally files, but are pointers that point to the device drivers located in the kernel. The protection applicable to files is also applicable to physical devices.
FIFO FILES
FIFO files are those that let unrelated files communicate with each other. These files are typically used in applications where the communication path is only in one way and where a number of processes have to communicate with a single process, often called the daemon process. Each message writes a message to the FIFO file and the UNIX system ensures that the other users do not overwrite a message written in the file.
4. THE UNIX FILE TREE
The designers of UNIX used directories to organise the UNIX file system into a structure that is shaped like an upside-down tree. Directories enable the user to keep related files in one place, where they can be seen only when wanted.
The figure that follows shows a part of the file tree for a typical UNIX system. In this drawing, which looks somewhat like an upside-down tree, names like home and jane are followed by a slash (/), which indicates that they are directories, or files of files. Note that ordinary files, such as cowboys and prufrock, are not followed by a slash. Such files are called leaves because they aren't connected to anything else. The connecting lines are the paths through the UNIX file tree. You can move around the tree by following the paths.

The file tree for a typical UNIX system

Unlike some operating systems, UNIX offers great flexibility in naming files and directories. The slash character cannot be used because it is the pathname separator and the name of the file tree's root directory. However, almost everything else is legal. Filenames can contain alphabetic (both upper- and lowercase), numeric, and punctuation characters, control characters, shell wild-card characters (such as *), and even spaces, tabs, and new lines.

The Structure of UNIX

The UNIX Operating System can be divided into three parts structurally, namely,

Ø A core unit called the kernel that interacts with the hardware for low level functions

Ø An outer unit called the shell that interacts with the user to perform functions desired by the user

Ø The File System

The idea of such a structure was to keep the units related but still separate so that the entire program did not park itself in the memory and slow down the system.

The Kernel

The core unit that manages the hardware and the executing processes is called the kernel. When the computer is switched on, the program UNIX is loaded into the computer's main memory, where it remains until the computer is shut down. This program, called the kernel, performs many low-level and system-level functions. The kernel is responsible for interpreting and sending basic instructions to the computer's processor. The kernel is also responsible for running and scheduling processes and for carrying out all input and output. The kernel is the heart of a UNIX system and there is one and only one kernel.

To list, the tasks of the kernel include

Ø CPU Scheduling

Ø Allocating the necessary hardware

Ø Controlling the I/O operations

UNIX is a multi-tasking, multi-user operating system, which implies that several users can work on the system at the same time and each user can submit several jobs for execution at the same time. In order to effectively manage the several tasks that run simultaneously, the kernel has to allot a tag that would enable the identification of any task, uniquely. The simplest form of identification for the kernel is the use of an integer.

For every job that is initiated, the kernel assigns an identification number called the process id. The kernel maintains a table called the Process Tab that describes every running process, enabling the identification of the details of the running process by means of the process id of the process, which is also stored in the table along with several other details. When a process is initiated, the kernel creates an entry in the process table and this entry is discarded from the table once the process is completed or its execution ceases. Each entry in the process table also indicates the priority of the process. This priority is a factor that determines the fashion or order in which the CPU allots time to the processes. Once a process gains the attention of the CPU, the process runs for an interval and then the CPU moves on to execute another process that deserves allocation. The interval is the period of time for which the process runs. This concept of giving each process a particular interval of time ensures that all processes get a chance to run and no process starves without the allocation of the CPU. The operating system handles the allocation and reallocation at a mind boggling speed that the users can hardly make out that the CPU is not allotted to them and they only have the feeling that the system is responding to all their requests simultaneously.

The Shell

As the shell of a nut provides a protective covering for the kernel inside, a UNIX shell provides a protective outer covering. As you might suspect from the critical nature of the kernel's responsibilities, the instructions to the kernel are complex and highly technical. To protect the user from the complexity of the kernel, and to protect the kernel from the shortcomings of the user, a protective shell is built around the kernel. The user makes requests to a shell, which interprets them and passes them on to the kernel.

A shell is a program dedicated to a single user, and it provides an interface between the user and the UNIX kernel.

Because any program can be executed from the login—and a shell is simply a program—it is possible for you to write your own shell. In fact, three shells, developed independently, have become a standard part of UNIX. They are

Ø The Bourne shell developed by Stephen Bourne

Ø The Korn shell developed by David Korn

Ø The C shell developed by Bill Joy

This variety of shells enables you to select the interface that best suits your needs or the one with which you are most familiar.

Bourne Shell

It is the most widely used UNIX shell and is evidently, named after its developer Stephen Bourne. The Bourne shell prompts the user with a $ symbol. The Bourne shell, by itself, is a program by name sh.

Korn Shell

The Korn shell has more features than the Bourne shell and is named after its developer David Korn. It is also called ksh.

C Shell

The C shell has still additional features and was developed by Bill Joy. It is also called csh.

OPERATING SYSTEMS – AN INTRODUCTION

An operating system is an integral part of a computer system. The computer system can be viewed as being built from three general components: the hardware, the operating system, and the applications. The hardware includes pieces such as a central processing unit called a CPU, a keyboard, a hard drive, and a printer.

Applications are why we use computers; they use the rest of the system to perform the desired task. The operating system is the component that, on one side, manages and controls the hardware and on the other, manages the applications. When a computer system is purchased, it must have at least hardware and an operat ing system. The hardware is able to use one or more different operating systems. The operating system is necessary in order to manage the hardware and the applications.

The UNIX Operating System

UNIX, like other operating systems, is a layer between the hardware and the applications that run on the computer. It has functions that manage the hardware and functions that manage executing applications.

Unix is a very popular multi-user , multitasking operating system.

FEATURES

Multi User Capability

In a multiuser system,the same computer resources – hard disk, memory etc are accessible to many users. The users are given different terminals to operate from. A terminal , in turn , is a keyboard and a monitor , which are the input and ouput devices for that user. All terminals are connected to the main computer whose resources are availed by all users.

Multi Tasking capability

Unix OS is capable of carrying out more than one job at the same time. It allows the user to type in a program in its editor while it simultaneously executes some other command the user might have given earlier , say to sort and copy a huge file.

This is managed by dividing the CPU time intelligently between all the processes being carried out. Depending on the priority of the task, the operating system appropriately allots small time slots to each foreground and background task.

Portability

Unix has been written in C , hence it can run on machines from microcomputers to mainframe computers.

Security

Unix is a very secure operating system. Without the proper username and passwords, it is not possible to work on Unix. At the file level , there are read , write and execute permissions to each file which decide who can access a particular file ,who can modify it and who can execute it.

Stable and Reliable

A very reliable and stable Operating System. It is less prone to crashes.

Unix is available in many flavors like :

AIX (Advanced IBM Unix)

HP-UX ( Hewlett Packard Unix)

MINIX (Minimal Unix)

SCO UNIX

SOLARIS

XENIX

LINUX, etc