Professional Documents
Culture Documents
Version:2
INDEX
1. Introduction to UNIX.............................................................................................................................. 1
1.1. Single-User Systems............................................................................................................................................ 1
1.2. Multi-user Systems............................................................................................................................................... 1
1.3. Hardware - Multi-user Systems............................................................................................................................ 1
1.4. Classes of Multi-User Systems............................................................................................................................ 1
1.5. Multi-User Operating System - UNIX................................................................................................................... 2
1.6. STALLMAN AND TORVALDS: GNU AND LINUX................................................................................................2
1.7. History of UNIX..................................................................................................................................................... 2
1.8. Features and Benefits of UNIX............................................................................................................................. 3
1.9. Versions of UNIX.................................................................................................................................................. 3
1.10. THE KERNEL AND SHELL................................................................................................................................ 4
1.11. Security for the Unix User................................................................................................................................... 4
1.12. Creating a Password.......................................................................................................................................... 5
1.13. Rules for Passwords.......................................................................................................................................... 5
1.14. Changing the Password..................................................................................................................................... 6
1.15. Check your current Directory the pwd command............................................................................................6
3. Unix Utilities......................................................................................................................................... 15
3.1. The wc Command.............................................................................................................................................. 16
3.2. The pg Filter:...................................................................................................................................................... 16
3.3. The more Filters:................................................................................................................................................ 16
3.4. Pipes.................................................................................................................................................................. 17
3.5. The man command displays pages from the UNIX reference manual...............................................................17
3.6. Comparing two FILES The CMP Command....................................................................................................17
3.7. The comm-command further comparison..........................................................................................................17
3.8. The diff command............................................................................................................................................... 18
3.9. The banner command........................................................................................................................................ 18
3.10. The calendar command.................................................................................................................................... 18
3.11. Displaying the SYSTEM DATE with Date Command.......................................................................................18
4. Simple Filters....................................................................................................................................... 22
4.1. The Sort Filter:.................................................................................................................................................... 23
4.2. Options of the sort Filter:.................................................................................................................................... 23
4.3. The Cut Filter...................................................................................................................................................... 25
4.4. The head Filter................................................................................................................................................... 26
4.5. The tail Filter:...................................................................................................................................................... 26
4.6. paste command:................................................................................................................................................. 26
4.7. The uniq command:............................................................................................................................................ 27
4.8. Translating Characters - the tr command...........................................................................................................27
4.9. Sending the output to some other file using tee command................................................................................28
5. The Vi Editor......................................................................................................................................... 28
The three modes of vi................................................................................................................................................ 29
5.2. Getting started with vi:........................................................................................................................................ 29
5.3. Cursor Movement Commands of vi:................................................................................................................... 29
5.4. Commands to Copy Lines:................................................................................................................................. 31
5.5. Saving and Exiting Commands:.........................................................................................................................31
5.6. Pattern Finding Commands:...............................................................................................................................31
5.7. Paging and scrolling:.......................................................................................................................................... 31
5.8. Customizing vi.................................................................................................................................................... 32
5.9. The set command............................................................................................................................................... 32
5.10. Options used by vi............................................................................................................................................ 32
6. Advanced Filters.................................................................................................................................. 33
6.1. The grep Filter.................................................................................................................................................... 33
6.2. Options with grep............................................................................................................................................... 33
6.3. sed Filter............................................................................................................................................................. 35
6.4. Reading and Writing to files (r,w):......................................................................................................................35
6.5. Bracketed regular expression:............................................................................................................................ 36
6.6. awk Filter :.......................................................................................................................................................... 36
6.7. Locating files using the find command:..............................................................................................................37
6.8. Options of the find command.............................................................................................................................37
6.9. The ln command................................................................................................................................................. 39
7. Process................................................................................................................................................. 39
7.1.Ps command....................................................................................................................................................... 39
7.2. Background Processing:.................................................................................................................................... 39
7.3. Requesting a Background Process:...................................................................................................................39
7.4. Checking on Background Process :...................................................................................................................40
7.5. Terminating a Background Process :.................................................................................................................. 40
8. Communications.................................................................................................................................. 41
8.1. To-way communication-The Write Command.....................................................................................................41
8.2. The mesg Command.......................................................................................................................................... 41
8.3. The mail command- The Mail Box...................................................................................................................... 42
9. Introduction to Shell............................................................................................................................ 43
9.1. Login process..................................................................................................................................................... 43
9.2. Types of shells :.................................................................................................................................................. 43
9.3. expr Command................................................................................................................................................... 44
9.4. Environment Variables:...................................................................................................................................... 44
9.5. The HOME variable:........................................................................................................................................... 44
9.6. The PATH variable:............................................................................................................................................. 44
9.7. Shell Script......................................................................................................................................................... 45
9.8. EXIT statement:.................................................................................................................................................. 45
9.9. Break AND continue:.......................................................................................................................................... 45
9.10. Exit status of the last command- the $?...........................................................................................................45
9.11. if..then..else condition....................................................................................................................................... 46
9.12. String test Operators........................................................................................................................................ 48
9.13. Logical operators.............................................................................................................................................. 48
9.14. The case...esac construct................................................................................................................................ 49
9.15. for..do..done..................................................................................................................................................... 50
9.16. while..do..done................................................................................................................................................. 51
9.17. Parameter handling in shell scripts..................................................................................................................52
APPENDIX A............................................................................................................................................. 53
Block commands in vi................................................................................................................................................ 53
Set Commands :........................................................................................................................................................ 53
Map command:.......................................................................................................................................................... 53
APPENDIX B............................................................................................................................................. 54
Shell Scripts and Shell Functions :............................................................................................................................ 55
Key To Unix
Page 1 of 57
1. Introduction to UNIX
1.1. Single-User Systems
The personal computer (PC) is a small, general-purpose system that can execute programs to perform a wide variety
of tasks. The PC, however, was designed for use by one person at a time; that is, it is Single-User oriented with MSDOS as the de facto standard operating system for this range of machines. Single user systems became very popular
due to the low cost hardware and wide range of software available for these machines.
Mainframes
Key To Unix
Page 2 of 57
Minicomputers
Key To Unix
Page 3 of 57
Portability
The system is written in a high level language making it easier to read, understand, change, and move to
other machines. Customers can now choose from over a wide variety of hardware vendors without being
locked in with a particular vendor.
Machine Independent
The system hides the machine architecture from the user, making it easier to write applications that can run
or micros, minis, and mainframes.
Multi-user operations
UNIX is a multi-user system designed to support a group of users simultaneously. The system allows for the
sharing of process power and peripheral resources while at the same time providing excellent security
features.
Unix shell
UNIX has a simple user interface called the shell that has the power to provide the services that the user
wants. It protects the user from having to know the intricate hardware details.
Utilities
UNIX has over 200 utility programs for various functions. These utilities form a powerful set of tools to cater
to user requirements without the necessity of writing new programs.
Background Processing
UNIX has facility which the user can start a task and then proceed to work on other tasks while the system
runs the first task in the background and the second task in the foreground. Background processing helps
the user in effective utilization of time.
Maturity
UNIX is a time-tested operating system. It offers a bug free environment and high level of reliability.
The UNIX SYSTEM supports a wide variety of languages - C, Fortan, Basic, PALCAL, Ada, COBOL, Lisp and Prolog.
Developed
Key To Unix
Page 4 of 57
AT & T
UNIX System
UNIX BSD(4.xx)
XENIX
ULTRIX
AIX
IBM
Key To Unix
Page 5 of 57
administrator. This user account is maintained by the system to check for illegal responses. So, you have to ask your
administrator for your account name.
NOTE:The system never indicates what went wrong- the login name or the password.
This procedure followed by users to start a UNIX session- the login procedure-has been explained earlier. The login
procedure ensures that only authorized users can try to access the system.
However, it is easy for anyone to find out a users login name since this is displayed on screen when being entered.
Thus the login procedure is not a foolproof system.
To overcome this problem, UNIX allows a user an additional measure of security by allowing him to have a password
associated with his name.
To login using password-protected user names, the user has to enter not only the user name but also the password.
UNIX to see if they are authorized entries checks the combination of the two. The $ prompt appears only if the user
has entered both the name and password correctly, otherwise he is asked to re-enter the user name followed by the
password. Passwords are not displayed on the screen while they are being entered. Sample login sequence is show
below:
It is desirable to have slightly complex passwords so that they are not easily deciphered. The use of simple
passwords has in fact become a major cause for concern in the U. S. A, where there have been many incidents of
Key To Unix
Page 6 of 57
unauthorized access to a UNIX system simple because users used very simple and obvious passwords such as their
own names, or birth dates or age.
However, passwords should not be so complex that they cannot be remembered.
$passwd <RET>
Changing password for gopal
Old password:
Sorry.
$
If the old password is entered correctly, then the user is asked to enter the new password and re-enter it to confirm. If
the two entries do not match, then the user is given another chance to enter the new password.
$passwd
<RET>
Old password:
New password:
Re-enter new password:
They do not match, try again.
New password:
Re-enter new password:
$
When changing the password, the new password should differ from the old password by at least 3 positions.
Key To Unix
Page 7 of 57
bin
dev
etc
mnt
bin
tmp
unix
kumar
lib
var
sharma u1001p
Key To Unix
Page 8 of 57
NOTE: -
It is possible for two directories to have the same name. Every file, apart from root, must
have a parent, and it should be possible to trace the ultimate parentage of a file to root. It is
not possible that the two directories or files have the same name in one level.
<RET>
/user/asha
$ls <RET>
Z.c
alpha-doc
asha1
asha2
tempdir
x.c
$
From this listing, it is impossible to tell whether asha1 is a directory file or an ordinary file or a special file; whether
tempdir is actually a directory file as its name indicates.
UNIX provides a way of doing so by using the l option of ls command this is shown below:
$ pwd <RET>
/user/asha
$ ls l <RET>
total 6
This ls l command shows a long listing for files in the current working directory with:
$pwd
/usr/user1
$mkdir dir1
$_
$pwd
$/usr/user1
$_
Key To Unix
Page 9 of 57
The new directory dir1 is created under the current directory called user1. Creating a directory does not change the
users position in the tree structure.
2.5.Changing Directories
You can move around the file system by using the CD (Change Directory) command.
COMMAND: cd (change directory)
FUNCTION: The command changes the directory to the directory specified as argument.
Ex:
$pwd
/usr/user1
$cd dir1
$ pwd
/usr/user1/dir1
$_
Here, the cd command changes the directory /user/user1 in to the sub-directory called dir1.The cd command used
without arguments gets the user back to his HOME directory from wherever he is. To get back to the parent directory,
the special argument . . (dot-dot) is used with cd. The symbol . always means the parent of the current directory .
Description
-a
-l
-r
-R
-s
-t
-d
-F
-i
Key To Unix
Page 10 of 57
SYNTAX
: cat <filename>
A sample file.
Good bye.
$_
Here, the contents of the file file1 are displayed on the screen using cat command. You can concatenate two files
using cat command
$cat file1 file2 >file3
$_
Here, the contents of file1 and file2 are displayed and they are redirected to the file3. File3 contains the contents of
file1 and file2. If file3 is already exists it simply over writes the file.
Using cat command you can create file
$cat>file4
Here `cat command takes the input from the keyboard and redirects the output to the file4. After entering the text you
have to press `ctr+d` to save and come out of that file.
NOTE: The [Control-d] OPTION is used to terminate all input not only with cat, but with several other commands as
well.
Key To Unix
Page 11 of 57
$cd /usr/kumar
$ pwd
/usr/kumar
$
Every absolute pathname must trace its origin to root, as discussed earlier in this chapter. The first /indicated root
directory, while subsequent ones act as delimiters to sub-directories. This method can be inconvenient and tedious,
especially when the absolute pathname is quite long i.e., when you located a number of generations away from
root. If you have a long chain of sub-directories, then the use of an absolute pathname is an unsatisfactory solution
to the problem of moving a single generation up the hierarchy.
UNIX also permits use of a relative pathname, which locates a filename with respect to the current directory. In ls
command you discovered two hidden directories--. and.. Indicating the current and parent directory, respectively.
Using. . As an argument to cd, you can issue an instruction to move to the parent directory /usr/kumar/dir1/data/text,
i.e. the directory /usr/kumar/dir1/data:
$ pwd
/usr/kumar/dir1/data/text
$cd ..
/usr/kumar/dir1/data
$_
This method is compact and more useful when ascending the hierarchy.
: cp (copy)
FUNCTION
SYNTAX
Ex
: $ cp file1 file2
The cp command copies the source file to the target file if the target file is already exist in that directory.
Ex: cp i file1 file2
Here i, option allows you to copy the files interactively. If file2 exist than it will prompt you to over write the file.
Note: using cp command you cannot copy multiple files to a single file.
: mv
$ mv file1 /usr/user1/dir1
Key To Unix
Page 12 of 57
>
$ rm file1
$_
The rm command removes files from current directory. It can remove more than one file on a single command line.
There are three options with rm :
Options
Significance
-I
-f
-r
that
dont
have
write
Group Owner
Consider the following situation:
A project team of 5 people of the SMARTEST CONSULTANTS PVT. LTD. is working on a software development
project for a private detective agency. The team is headed by an analyst. The other 4 members are all programmers.
The team is working on a UNIX system. Each programmer has been given a few programs to develop. The data,
provided by the detective agency, is of a highly confidential nature, and so the data file has been created in the
analysts HOME directory.
One programmer may have to link his program to another programmers program in order to test his program.
Key To Unix
Page 13 of 57
In this situation, each programmer is the File Owner of his own program files. Each program, however, would also
belong to the other programmers, so that they could use the programs for linking. The project team (of 5 users) is
said to be the Group Owner for the file.
In UNIX, it is possible to define which user of the system is member of a group. A group of users are also given a
name, just as a user is given a name.
So, for the project team, the programmer Anita may be the owner of her program files. The group of users in the
project team may be called PROJECT-ABC, which becomes the Group Owner of Anitas program files.
Other Users
In the example of the SMARTEST CONSULTANTS PVT. LTD., all users of the system who are not members of the
group PROJECT-ABC are referred to as Other Users for all files that belong to that group. Their Other Users are
thus those users who do not have any common needs for data or programs with the Group Owner. For e.g. people of
the accounts department who use the system to generate reports, or data entry operators entering data for payroll
can be referred to as Other Users for the files that belong to the group PROJECT-ABC.
To summarize:
Associated with each file in UNIX is:
-
a File Owner
a Group Owner which consists of users who need to share that file
Other users who do not belong to the group.
The file Owner can define the following kinds of permissions for each of the above user categories:
a) Read access
If a user has read access permission for a file then he can display the
file, copy it compile it etc.
b) Write access
If a user has write access for a file then he can write on to it, change its
contents or delete it.
c) Execute access
The permissions are given in the first column from the second position
The first three characters show the read, write and execute permissions for the file owner the next
three are the permissions of the Group owner and the last three are the permissions of the Other
users.
If a permission is allowed to a user, then r (read), w (write) or x (execute) is displayed otherwise a
hyphen appears in the place.
Directory Permissions
for groups and others
Cd
r--
-w-
ls
Cp
cp
Into
from
Key To Unix
Page 14 of 57
--x
rw-
r-x
-wx
rwx
No write permission
No execute permission
2.16. Changing the FAP for the File Owner, Group Owner and other users:
To control the access permissions for a file for different users, the File Owner can use chmod command.
The following example shows how the file access permissions for the file file1 can be changed. The permissions for
this file are shown below:
-rwxrwxrwx 1 vara unix2 20
jan 31
10:40
file1
To change permissions for all the three categories of users, the file owner has to indicate:
-
SYNTAX: $chmod
<permissions>
filename
For example: The file Owner can remove the write permission for all users for the file file1 by the following command:
$chmod
-w filename
<RET>
Key To Unix
Page 15 of 57
$_
The FAP for the file file1 will now appear as follows:
-r-xr-xr-x 1 vara unix2 20 jan 31
10:40
file1
+w
file1
<RET>
$_
More than one-file access permissions can be changed in one command (instead of using two or more commands).
For example:
$chmod
+wx
file1 <RET>
$_
This is done by specifying one of the following before the + or sign in the command.
u
Key To Unix
Page 16 of 57
3. Unix Utilities
3.1. The wc Command
The wc filter counts the number of lines, words and characters in the specified file or files. It is capable of accepting
input directly from the keyboard. By entering wc without any arguments, it waits for the user to type in the input. By
default, wc prints all the three counts lines, words, bytes. It comes with three options, which allow user to obtain the
number of lines, words or characters individually or in any desired combination.
Options
SYNTAX : wc
Ex:
[options]
Significance
-c
-w
-l
<filename>
file1
Raju
Rani
Vara
Sai
$ wc
file1
4
19 file1
In the above example, it displays the number of lines, words and number of characters along with the filename.
Ex:
$ wc
-l
file1
file1
$_
Ex:
$ wc
-c
16
file1
file1
$_
Key To Unix
Page 17 of 57
3.4. Pipes
UNIX has a feature by which filters and other commands can be combined in such a way that the standard output of
one filter/command can be sent as standard input to other filter/command. The vertical bar (|) is the pipe character.
Let us now see some examples of piping:
1)
$ls | wc
Here, the output of ls command becomes the input to wc which promptly counts the number of lines it receives as
input and displays this count on the screen.
1)
$ who | sort
Here, instead of displaying the output of who to the screen it is piped to sort. Sort sorts whatever it receives as
input and displays the sorted output.
3.5. The man command displays pages from the UNIX reference manual
For Example, if the user wants detailed information about the ls command, the command to be given is:
$man ls
NAME
: Command/file name, along with a brief description
SYNOPSIS
: How to use the command, it contains the optional and non-optional arguments.
DESCRIPTION : An explanation of how to use the command, and also an explanation of each option.
EXAMPLE
: gives example of how to use a command.
FILES
: files which have to be available for this command to work
SEE ALSO
: commands similar in purpose
DIAGNOSTICS : Explanation of error messages
WARNINGS
: Things to be careful about when using the command
BUGS
: Known problems and suggested improvements.
Key To Unix
Page 18 of 57
$ cat file1
c. s .das
singhvi chanchal
sumitha roy
Krishna murthy
Satish kumar
$$cat file 2
barun gupta
c. s. das
anil kumar
smitha chowdary
sumitha roy
$$ comm. File1 file2
anil kumar
barun gupta
Krishna murthy
c.s. das
Satish kumar
Singhvi chanchal
Sunitha chowdary
sunitha roy
Key To Unix
Page 19 of 57
The cal command is accurate, and also takes into account the leap year adjustments that took place in the year
1752.
Date+%h
Date+%y
console
jan
30
10:32
kumar
tty01
jan
30
14:09
tiwary
tty02
jan
30
14:15
Sharma tty05
jan
30
13:17
-H option with who command displays the heading for each column
The who command, when used with the arguments am I, displays a single line of output only, i.e., the login
details pertaining to the user who invoked this command:
$ who am I
kumar
tty01
jan 30
14:09
Significance
Key To Unix
Page 20 of 57
[]
file2
file3
file4
fileabc
file
$_
When * (asterisk) is appended to the string file, the pattern file* expands in to all files, in which the first four constitute
the string file and after this four characters it can be anything or nothing i.e. the above command lists all files in the
current directory as shown above.
The * Wild-Card can also be repeated as shown below:
Ex: Assume that files, file1.txt, file2.txt, file3.abc already exists and
gives the command
$ls
file*.*
file1.txt
file2.txt
file3.abc
$_
This displays the files starting with file and containing any sequence of characters or no characters followed by a .
(dot) and then followed by any sequence of characters or no characters .
$_
The ? matches exactly any one character. Here all the files whose names are of single character are
displayed.
2) $ls file?
file1
file2
$_
3)$ls *.???
file1.txt
$_
4) $ls file2.?
file2.c
$_
In the second example, it is listing all files with a pattern file and followed by any single character i.e. file1 and file2. In
the third example, it is displaying for any file with a. (dot) extension of three characters. In fourth example, it displays
the files with pattern file2 and with a . extension of a single character.
Key To Unix
Page 21 of 57
Ex: Assume that the files a, b, c, d, file1, file2, file3 exists in the
current directory.
1) $ls
[abc]
a
$_
It displays the files with single characters of either a or b or c.
2) $ls file[13]
file1
file3
$_
All the above Wild-Cards can be combined with each other.
Ex: 1)$ls *.???
It will display files with pattern anything with the . (dot) extension of three characters.
2)$ls [a-z]*
It displays all files starting with a to z and anything or nothing afterwards.
3)$ls [!a-c]*
It displays all the files which do not start with a or b or c.
3.17. Redirection
Most UNIX commands perform one simple function, which consists of the following steps :
- They take some data as input from some source
- They process the data
- They produce the results as output and send it to some destination
In UNIX, when a user executes a command, the shell runs the command and assigns to the command:
- The terminal keyboard as the source of input
- The terminal VDU as the destination of output
In UNIX keyboard and VDU are referred by special names - the keyboard is the Standard input file and
the VDU is the Standard output file.
Consider the cat command. When followed by the file name, this command displays line by line from that
file, tills the end of the file. Without the file name it takes its input from the standard input file and writes its
output to the standard output file.
The cat command waits for the input from the keyboard. When the user presses the <RET> key after
entering a line, the cat command does what it is supposed to do - simply displays the entire line on the
screen.
The user can keep on inputting lines. To indicate the end of input, the user has to press <Ctrl> d
Not all commands use the Standard input file for input and the Standard output file for output. Given
below
Key To Unix
Page 22 of 57
are examples of the commands that use one or neither of these files.
a. Command that uses neither the Standard input file nor the Standard output file.
E.g. cd - does not except input from the keyboard
- does not write anything onto the VDU.
b. Command that uses the Standard output file but not the Standard input file.
E.g. pwd - does not expect input from the keyboard
- displays its output ( the name of the current working directory) on the VDU.
Device Files
Redirection operator
Stdin
<
Stdout
>
Stderr
>
Ex:
$ls>abc
Redirects the output of ls command to abc file
$ls -l 2> abc
Key To Unix
Page 23 of 57
4. Simple Filters
4.0. Filter:
A filter is a command or a user program that takes its input from standard input, filters the input and sends output to
the standard output. These are file-related commands.
$sort
<RET>
Anitha
Swapna
Madhu
<ctrl> d
Anitha
Madhu
Swapna
$_
The user enters names, each on one line. When the user indicates end of input by pressing <ctrl>d, sort compares
the corresponding characters of each line and displays the lines in proper sequence.
The following are the options with sort:
Options
Significance
-t
-n
-r
-f
+n[-m]
-d
Stores
default
output
is to
in
file.
The
send output to
Key To Unix
Page 24 of 57
-o file
standard output.
-t -
+2 -3
file1
$sort
-n
<RET>
6
5
8
2
9
<ctrl>d
2
5
6
8
9
3) The r option:
This changes the output of sort to reverse order. Consider the following example:
Ex:
$sort
-r <RET>
Anitha
Laitha
Madhu
<ctrl>d
Madhu
Lalitha
Anitha
$_
4) The -f option:
In case the input is to sort consists of digits and alphabets and other characters, sort arranges data in ASCII
sequence. In the ASCII sequence of characters, A-Z has lower values than the corresponding lowercase alphabets.
So, sort would place anitha after Anitha since a has higher ASCII value than A. The f option folds all lowercase
Key To Unix
Page 25 of 57
characters into uppercase characters, i.e. it considers all lowercase characters as uppercase characters without
actually changing them.
Ex:
$sort
-f
<RET>
Anitha
asha
rama
Rama
Prasad
<ctrl>d
asha
Anitha
Prasad
rama
Rama
$_
5) The +pos1
-pos2 option:
By default sort assumes that fields are separated by a blank space or a tab character. To sort a file on one field, the
position of the field in the file has to be specified. This is done as follows:
The number of field separators that have to be ignored to reach that field must be specified as +pos1
where the pos1 the appropriate number.
The field separator at which sort is to stop must be specified as pos2 where the pos2 is the appropriate
number.
For example, to specify sorting on third field, the user will have to specify +2 and -3 i.e. ignore the first two field
separators and stop at the third separator.
The sort command would then be as follows:
$sort
+2 -3
filename
6) The -o option:
The output of sort always appears on the VDU (standard output file). In case, this output is to be saved on a disk file,
the o option should be used. Consider the following example:
$sort
-o file2
file1
Here, the o option indicates that the output is to be sent to the file specified after it. The name of the file to be sorted
is entered at the end of the command. o is followed by the name of the output to which the sorted data should be
written.
Significance
Key To Unix
1)
Page 26 of 57
-f
-d
-c
The -f option:
This option is used to specify the fields to be listed out. Consider the example:
Ex: cut
-f 1, 2
filename
Here, the name of the file whose fields are to be displayed is specified at the end of the command. The f option is
followed by the number of each field to be displayed. The numbers are separated a comma. So, this command
displays the first and second fields from the given file.
A range of fields can also be specified with cut as shown in the following examples:
2)
The -d option:
The cut command assumes that a tab character separates the fields. If some character other than default tab
character delimits the fields, cut supports an option -d, which allows to set the delimiter. The field separator must be
enclosed in double-quotes.
Ex: Consider the file file1 may have the information for each student stored in the following format:
Name: rollno: total: grade
Each piece of information is separated by a colon, hence we require the field delimiter to be recognized as :. The
command for listing the name and grade fields would now be:
$cut -d : -f 1, 4 filename
3) The -c option:
The option is used to cut the specified columns from a file. For example,
UNIX
$cut -c 2,4 filename
As a result, the second column and the fourth column from each line in the given file would be displayed.
filename
We can specify a line count and display say, the first three lines of the file. Use the - symbol, followed by a numeric
argument as follows:
Ex: $head - 3 filename
Here, the first three lines of a file would be displayed.
filename
Key To Unix
Page 27 of 57
Here, the last three lines from the given file will be displayed.
Ex:
file1 file2
file2 <RET>
6556688
7612214
6556688
Krishna
7612214
01|kalyani|accounts|6213
02|satish|office|4214
03|murthy|office|4214
Key To Unix
Page 28 of 57
Description
-d
-s
Squeezes
multiple
occurrence
occurrences
in
to
single
4.9. Sending the output to some other file using tee command
SYNTAX: cat names.lst |sort |tee names1.lst
Here, cat command displays the contents of file called names.lst, sort command uses that out put and sorts the file
in ascending order, tee command divides that output into two parts. One part will be sent to the file name specified
as argument, and second part will be displayed on the screen.
Key To Unix
Page 29 of 57
5. The Vi Editor
Vi is visual editor used to enter and edit text files containing data or documents or programs. It displays the contents
of files on the screen and allows the user to add, insert, delete or change parts of the text.
5.1. The VI program has three modes of operation:
a) Command Mode: In this mode all the keys pressed by the user are interpreted to be editor command mode. In
command mode the keys that are hit are not displayed on the screen.
b) Insert Mode: This mode permits the insertion of new text, editing of existing text or replacement of existing text.
c)
Each of these operations can be performed only after changing over from the command mode to insertion mode
using appropriate commands. The insertion mode is also known as input-text mode.
The ex Mode: This mode permits us to give commands at the command line. The bottom line of the VI editor is
called the command line. All commands entered in the ex mode are displayed in the command line.
COMMAND
MODE
i,I,a,A,r,R,o,O,s,S
<ESC>
INPUT
MODE
<control-d>
:sh <enter>
ex
MODE
filename
<RET>
If the give file does not exist then vi displays a blank screen with a ( ~) tilde character at the beginning of
each line as follows:
~
~
~
filename
[ New
file
If the file exists, then its contents are read in to the memory and also displayed on the screen.Assume that the given
file is a new file and the user wishes to enter text into his file.
In vi to add, change, delete text etc., commands have to be given. These commands are summarized in the following
pages.
Key To Unix
Page 30 of 57
Command
Action
The above command allows a user to start entering text. The user has to press i. Key or the a key and then start
entering the text. To end insertion or appending of text, the user has to press <ESC> key. The user thus keeps
changing between command mode and edit mode.
Assume that the entered some text in the file and presses the <ESC> key after entering the second line. This takes
him out of edit mode and command mode meaning that he cannot make the changes to lines but can only give
another command. The <ESC> key toggles between the command mode and the edit mode.
Action
^D
^U
^F
Page forward.
^B
Page backward.
Ng
Go to nth line.
0(zero)
Action
r-
Action
Moves to the first character of the next word.
Key To Unix
Page 31 of 57
B
E
Action
Dw
Dd
Delete a line.
Cw
Change word.
Cc
Change line.
x-
Delete
character
position.
before
cursor
Join lines.
u-
U
. (dot)
Action
Yy
Nyy
P
P
Action
:w
:wq
:q
Quit vi
:q!
Key To Unix
Page 32 of 57
Action
f.x
Fx
/pattern
Find next
pattern.
?pattern
Find previous
the pattern.
line
containing
line
the
containing
<control-b>
<control-d>
<control-u>
Paging functions
Command
Function
Control f
Control b
Control d
Control u
5.8. Customizing vi
VI can be tailored to behave in a way desirable to the user. There are a number of commands available in the ex
mode which make it suitable for writing programs as well as preparing documents. You make like to set a pleasant
and comfortable environment for yourself so that the editor behaves and responds to your directives exactly in the
way you would like it to.
There is also the facility to store all the user-set options in a separate file, which is used by the editor to take its
startup instructions from. This is the file .exrc containing all user-defined settings conceptually similar to the .profile
which is maintained by each user.
Key To Unix
Page 33 of 57
Abbreviation
Significance
Autoindent
ai
Autowrite
aw
Ignorecase
Ic
Showmode
Number
Key To Unix
Page 34 of 57
6. Advanced Filters
6.1. The grep Filter
Grep stands for Global regular expression print. The grep filter is used to search for a particular pattern of
characters in a file and displays all the records that contain that pattern.
grep cannot be used without specifying a regular expression. The format of the grep filter is:
Syntax: grep [options] <pattern >
<filename(s)>
Option
-c
Displays
matches,
text.
only
the
number
of
without
quoting
the
-n
-i.
-v
-l
Displays
only
filenames
containing
a
match,
without
quoting the text.
-E
The options must be specified before the regular expressions. Options can also be combined (ex -n and v can be
used together as nv).
Note that regular expressions are always given in double-quotes.
While forming the patterns to be searched, we can use shell met meta-characters also.
Character
Use
Key To Unix
Page 35 of 57
[]
new[abc]
^character
To specify that the pattern following must occur at the beginning of each
line.
Ex: ^[abc] specifies the pattern either a or b or c at the beginning of
each line.
^[ ]
To specify that the pattern must not contain any character in the set/range
specified within [ ]
Ex: [^abc] specifies a pattern which does not contain either a or b or
c.
So, the command ;
$grep new[^abc]
Specifies the search patterns as any pattern containing the word new
followed by any character but a or b or c.
Sed by default prints all lines in addition to lines affected by the action. So, the addressed lines are printed twice. Use
of, -n option disables this default printing
sed -n 'p' myfile
sed -n '10p' myfile
sed -n '5,$p' Mayfield #print all lines from 5th to the last line
Key To Unix
Page 36 of 57
Delete(d):
d- Instead of p in above examples would delete the lines.
sed '1,/^$/d' Mayfield
Above example may be used to delete a mail header from an Internet mail message that user have saved in a file
Substitute(s):
Syntax: sed '[address]s/pattern/replacement/[flags]' file
Flags :
:n : replacement should be made for the nth occurrence of the
pattern.
g: replaces all occurrences of a pattern in each line
Example:
.
.
$cat Mayfield
col1<tab>col2<tab>col3<tab>col4
.
.
.
.
.
.
Key To Unix
Page 37 of 57
Suppose you want to abbreviate everyone's first name to just their first initial and a period in the phone.list file (refer
exercise). Storing the first character after a comma and a space in register 1 and then substituting that for entire first
name can do it.
:
:
:
:
filed separator
output field separator
current record number
number of fields in current record
NOTE: awk assumes by default space as the delimiter. If you have any delimiter other than space you have to
specify the delimiter using F option.
Examples:
awk '{print $1,$2}' Mayfield
awk '{print NR,$1,$2}' Mayfield #print record numbers in the output
awk '$1~/vara/{print}' Mayfield
~: match operator for regular expressions. It searches for expression vara in the first column
awk 'NR==5{print}' Mayfield
Key To Unix
Page 38 of 57
Option
-f
-F
Description
It allows another filename.
i.e. the file contains the procedure for the
awk command
It allows you to specify delimiter
If you have any delimiter other than space
Key To Unix
Page 39 of 57
This option lists out the names of the files and directories in the named directory and its sub-directories, which have
the number of links specified.
Ex:
Here, it lists all the files and directories from the current directory downwards which have exactly 2 links.
5) The -size option:
This option lists out all the files and directories from the current directory downwards which are of mentioned size.
Ex: $find . size 10 print
Searches in current directory downwards all files whose size is 10 blocks.
Ex: $find .
Key To Unix
Page 40 of 57
7. Process
7.1.Ps command
The ps command is used to display attributes of a process. It is one of the few command of the UNIX system, that
has knowledge of the kernel built into it. It reads through the kernel data structures or process tables to fetch the
process characteristics.
Ps is also a highly variant command, the actual output depending on the version of UNIX
$ps
PID
TTY
TIME
CMD
476
tty03
00:00:01
login
659
tty03
00:00:01
sh
684
tty03
00:00:00
ps
278
$ vi
filename2
Here, the user starts sorting the file with command sort and then immediately begins editing new file, without waiting
for the completion of the previous process.
The shell initiates the sorting process, the kernel assigns a process number (278) and then it returns control of the
screen to the user.
When performing background processing, the user has to be careful that the background process and the foreground
process do not interfere with each other. Any screen output from the background process could not disturb the screen
in the middle of the users work with the foreground process.
In the above example, redirecting the standard output and standard error of the sort command can solve this
problem.
Key To Unix
Page 41 of 57
An urgent job needs to be completed quickly and so all unimportant process need to be stopped immediately.
A command/shell script did not terminate as desired and the only way to stop it from executing is to terminate it.
The terminal went into hang because the program being executed did not function as desired.
The kill command can be shown below
$kill
279 <RET>
$_
Note the process identifier (279 in this case) which is first displayed when a background process is started,
is given as an argument with the kill command.
Key To Unix
Page 42 of 57
8. Communications
In
Multi-user system, it is often necessary for one user to know what the other is doing. When there are a hundred
odd users sharing the system resources, some of them are invariably located quite a distance apart. Communication
through the system seems quite natural and necessary the system administrator also requires to send messages to
some, and sometimes to all of them. Since UNIX was invented at Bell Laboratories, its not surprising that the
electronic mail facilities are simple and quite elaborate. Its rich communication features can be exploited by the user,
both to communicate with others, as well as with himself. These features are so useful that for some people UNIX
means electronic mail.
$ write u1003p
have you completed your program ?
I have completed mine- kumar
o
Write is used interactively in this manner, both users need to invoke the command individually. This establishes two
write channels, one for the receiver, and the other for the sender. However, if there is need for an extended
communication, then one user can simply wait after he has keyed in this message, without pressing [control-d]. write
is so designed that it enables the user to send and receive messages at the same time, without his having to quit to
the shell.
NOTE: For two users to communicate with each other, both must invoke the write command. if two users have the
same login name, then the terminal name has to be used along with the login name.
Key To Unix
Page 43 of 57
$mesg
is y
$_
Key To Unix
Page 44 of 57
9. Introduction to Shell
The shell is an intermediary program, which interprets the commands that are typed at the terminal, and translates
them into commands that the kernel understands.
C shell :
C shell doesn't use .profile instead it uses ~/.login file for storing instructions that are to be executed during login time.
Variable assignment in C shell
set var=value
or
@ var=value
Values can be put into an array with set
set num=(1 2 3 4)
echo $num[1]
C shell can handle integers for computation
set c=$x+$y
#bourne(sh) uses expr for arithmetic operations
syntax ( if ) :
if (condition) then
.
.
endif
Key To Unix
Page 45 of 57
#No = symbol
Key To Unix
Page 46 of 57
The SHELL variable : Shell variable determines the type of shell that a user sees on logging in. Today, there are a
host of shells that accompany any UNIX system, and you can select the one you like the most. The C shell is known
by the program csh, and the Korn shell by ksh. In linux default shell is bash.
The TERM variable: TERM indicates the terminal type being used. There are some utilities which are terminaldependent, and they require to know the type of terminal you are using. One of them is the Vi editor, which makes
use of a control file in a sub-directory under the directory /usr/lib/terminfo. If TERM is not set correctly, Vi wont
work, and the display will be faulty.
$echo hellow
hellow
$
A Sequence or a set of UNIX commands to do a particular task can be stored in a file and executed. Such a file is
called shell script.
Example:
#Script to find the length of the string
echo "Enter the string"
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
echo " length of $str is $len"
Constructs used in shell scripts
Key To Unix
Page 47 of 57
Test d filename
Test r filename
Test w filename
Key To Unix
Page 48 of 57
Test x filename
Test s filename
Operator
-eq
-ne
-gt
-ge
-lt
-le
Meaning
Equal to
Not equal to
Greater than
Greater than or equal
Less than
Less than or equal to
When you utilize if to evaluate expressions, the test statement is invariably used as its control command. On most
UNIX system, it is an internal feature of the shell, test evaluates the condition place on its right, and returns either a
true or false exist status. This return value is used by if for taking decisions.
#Illustrating test option
if test -f abc
then
echo "File is an ordinary file"
else
echo "File is not an ordinary file"
fi
Arithmetic tests and string tests
#finding the percentages and grades of the students
echo enter the maths marks
read maths
echo enter the science marks
read science
echo enter the social marks
read social
total=`expr $maths + $science + $social`
echo the total of the three subjects is $total
percentage=`expr $total / 3`
echo the percentage of the subjects is $percentage
if test $maths -lt 35 -o $science -lt 35 -o $social -lt 35
then
echo student is failed
else
if test $percentage -lt 35
then
echo sorry the candidate is failed
else
if test $percentage -ge 35 -a $percentage -lt 60
then
echo congratulations u got "c" grade
else
Key To Unix
Page 49 of 57
$1 != $2
-z S1
Or
And
Key To Unix
Page 50 of 57
ls l ;;
ps f ;;
date ;;
who ;;
exit
esac
The five menu choices are displayed with a multi-line echo statement. The meanings attached to these choices are
obvious enough. The user response from the keyboard is accepted to the variable choice. The case statement then
Key To Unix
Page 51 of 57
matches the value of this variable for the strings 1,2,3,4, and 5, as shown in the five lines below the key word case. It
then relates each value to a command that has to be executed.
Note : The last case option need not have the ;; though you can provide them if you want.
#Script to check if a given character is Vowel or not
echo "Enter a character"
read a
case $a in
[aA])echo $a is Vowel;;
[eE])echo $a is Vowel;;
[iI])echo $a is Vowel;;
[oO])echo $a is Vowel;;
[uU])echo $a is Vowel;;
[0-9]*)echo $a is a number;;
*)echo $a is consonant;;
esac
* is interpreted as any value other than those listed earlier with case
9.15. for..do..done
The for loop is different in structure from the ones used in other programming languages. There is no next statement
here, neither can a step be specified. Unlike while and until, it doesnt test a condition but uses as list instead. The
syntax is as follows:
syntax :
for variable name in list of values
do
command
command
:
:
done
Example
for x in 1 2 3 4
do
echo $x
done
#Demonstrating a for loop
#To rename all .c files to .cpp
for fname in *.c
do
nname=`echo $fname | sed 's/.c$/.cpp/'`
mv $fname $nname
done
9.16. while..do..done
The pattern scanning scripts developed so far still suffer from serious defects. You have no way of giving the user
another chance when he has made a faulty response. And you are able to scan only one pattern at a time. Loops let
you perform a set of instructions repeatedly. The shell features three types of loops while, until and for. The first
two are basically complementary to each other. All of them repeat the instruction set enclosed by certain keywords
as often as the control command permits.
The while statement should be quite familiar to most programmers. It repeatedly performs a set of instructions till the
control command returns a true exist status. The general syntax of this command is as follows:
Key To Unix
Page 52 of 57
while condition
do
command
command
:
:
done
# sample script using while loop
$ cat menu.sh
while :
do
echo menu
6. list of files
7. process of user
8. todays date
9. users of system
10. quit to system
echo Enter your option :\c
read choice
case $choice in
6)
7)
8)
9)
10)
ls l ;;
ps f ;;
date ;;
who ;;
exit
esac
echo do you want to continue
read chc
if [ $chc = y o Y ]
then
continue
else
break
done
#Script to display "Hello" 10 times
i=1
while [ $i -le 10 ]
do
echo Hello
i=`expr $i + 1`
done
#Script demonstrating while true loop
while true
do
clear
cat<<MENU
1.list of files
2.Current directory
3.Users
4.exit
MENU
echo -e "Enter Your choice: \c"
read choice
case $choice in
1)ls;;
2)pwd;;
3)who;;
Key To Unix
Page 53 of 57
4)break;;
*)echo "Invalid Choice"
esac
read
done
Key To Unix
Page 54 of 57
APPENDIX A
Block commands in vi
Command
:nd
:m,nd
:n mo p
:m,n mo p
:m,n w filename
:m,n w >> filename
Function
Deletes nth line
Deletes lines from m to n
Moves line n after line p
Moves lines m to n after line p
Writes lines m to n to a file
Appends lines m to n to a file
Reads the contents of the file filename at current cursor position
:r filename
:r! Filename
Set Commands :
:se nu
:se ic
:set showmode
:set aw
Map command:
The map command lets you assign the undefined keys so that when a key is pressed it expands in to a command
sequence. The command :map is followed by the key, which needs mapping, and the key sequence, which is
mapped.
For example we can use one of the function keys say f2 and map it to the function.
Key To Unix
Page 55 of 57
APPENDIX B
Logical operators && and ||
Above code in italics can simply be written as who|grep "^$1"||echo "$1 not logged in". Command after '||' get
executed only if the first one fails. Command after '&&' get executed only if the first one succeeds. These operators
have a limitation that they are used for making simple decisions.
Handling command line options using getopts
getopts reads the options given on the command line and decides whether they are valid or not. getopts accepts
single character options from the command line provided the option is preceded by a minus (-) sign. On encountering
an option getopts stores it in the variable at the end of getopts command. On encountering an illegal option getopts
stores a '?' in the variable at the end of getopts.The ':' tells getopts that an argument is expected along with an option.
The word immediately following the option on the command line is read as argument to that option. getopts stores
argument to an option in the variable OPTARG.
#Script illustrating getopts
while getopts abcd choice
do
case $choice in
a)echo "you entered a";;
b)echo "you entered b";;
c)echo "you entered c";;
d)echo "you entered d";;
?)echo "Wrong choice"
esac
done
#Script illustrating arguments for an option
while getopts a:c:d choice
do
case $choice in
a) echo "Argument for a is $OPTARG"
c) echo "Argument for c is $OPTARG"
d) echo "Argument for d is $OPTARG"
esac
done
Shell Functions:
Syntax:
funame()
{
.
.
}
Ex:
dated_fname()
{
set -- `date`
year=`expr substr $6 2 2`
Key To Unix
Page 56 of 57
echo "$2$3_$year"
}
Key To Unix
Page 57 of 57