Fundamentals of UNIX

SAMPATH 1

Chapter 1
UNIX Operating System

SAMPATH

2

Objectives
In this session, you learn about: ‡ The functions of OS ‡ The history of Unix ‡ The features of UNIX ‡ The Unix architecture ‡ Process management ‡ File management

SAMPATH

3

Operating System (OS)
‡ OS is a system software ‡ OS can be defined as an organized collection of software consisting of procedures for operating a computer ‡ OS provides an environment for execution of programs ‡ OS acts as an interface between the user and the hardware of the computer system.

SAMPATH

4

Operating System
‡ Operating system interacts with user in two ways ‡ Operating system commands Enables user to interact directly with the operating system. ‡ Operating system calls Provides an interface to a running program and the operating system. System calls in UNIX are written in C.

SAMPATH

5

History of UNIX
‡ Ken Thompson of AT&T Bell Laboratories designed UNIX in late 1960s ‡ Two versions of UNIX that emerged are AT&T Unix and BSD Unix ‡ In 1989, AT&T and Sun Microsystems joined together and developed system V release 4 (SVR4) ‡ Two of the main standards mainly in use are POSIX (Portable Operating System Interface) and X/open standard. In 1988, MIT formed Xconsortium developed vendor-neutral Xwindow System.
SAMPATH 6

Features of UNIX
‡ UNIX is multi tasking and multi user operating system ‡ UNIX is a character user interface (CUI ) operating system ‡ UNIX is a open source operating system ‡ Portability ± Can we use it on any OS ‡ Modularity ± Can change the source code ‡ File structure ‡ Security ‡ Strong networking support & advanced graphics

SAMPATH

7

Layered Architecture

... cp comp shell as ld vi ed

banner ls

kernel hardware

sort

sh who a.out date grep wc

SAMPATH

8

Layers

SAMPATH

9

Shell
‡ Acts as an interface between user and kernel ‡ User commands will be interprets and carried out to kernel. ‡ Its features include control flow primitives, parameter passing, variables and string substitution.

SAMPATH 10

Kernel
‡ Kernel will act as a interface between shell and hardware components. ‡ Core of the operating system ‡ Collection of system calls mostly written in C language ‡ Interacts directly with the hardware ‡ Allocates time and memory to programs and facilities IPC¶s ‡ Helps in I/O services,

SAMPATH 11

Standard directories in UNIX

/root

/etc

/bin

/usr

/dev

/lib

/tmp

12 SAMPATH

‡

/etc

stores the system administration utility files stores the most commonly used unix commands stores all user home directories and some Unix commands stores all device files stores library files for C programming used for temporary storage

‡

/bin

‡

/usr

‡ ‡ ‡

/dev /lib /tmp

13 SAMPATH

Determining file access permission

Rwx owner ls ± l total 240
-rwxr-x-r--

rwx group

rwx other

A
3 sampath nipuna 2080 oct 10 09:34 mydoc.doc

B

C

D

E

F

G

H

I

14

SAMPATH

File access permissions
‡ A- total number of disk blocks occupied by the files ‡ B- type of the file ± - ordinary file ± d ± directory ± c- character device ± b ± block device ‡ C- its access permissions ‡ D- number of links to the file ‡ E- the owner of the file ‡ F- the group of the owner of the file ‡ G- the size of the file in bytes ‡ H- the date and time of last modification ‡ I ± the file name

15

SAMPATH

Absolute mode
‡ Based on octal numbers ± No access 0 ± Execute access only 1 ± Write access only 2 ± Read access only 4 ± Write and execute access 2+1= 3 ± Read and execute 4+1= 5 ‡ $chmod 400 mydoc.doc ‡ Exercise ‡ Write the command to change the permission of the file mydoc.doc ± Read and write for the owner ± Write and execute for the group ± Execute only for others ( both in symbolic and absolute)

16

SAMPATH

‡ cat command
± Helps you to create a file ± $ cat > filename ± cat filename helps to view the contents of the file.

‡ touch command
-- Will allow to create multiple 0 byte files $ touch file1 file2 file3

‡ VI command
$ vi <filename> If the file doesn¶t exist, it will create otherwise open the existing one.

17 SAMPATH

Creating a directory
‡ You can create a directory by using the mkdir command. For example, to create a directory named dir1 within the current working directory:
± $ mkdir dir1 ± $ mkdir /usr/user1/dir1 ± Options

‡ cd command ± To change the directory to dir1 type ± cd dir1 ‡ pwd ± To see the current working directory

18

SAMPATH

Removing a directory
‡ Removes a directory ‡ To remove a directory
± ± ± ± ± Must be empty Should not be the current directory or at a higher level Should not be the home directory -p -> Removes all directories along the path name specified $ rmdir -p /home/demo/mydir

19

SAMPATH

Removing files
‡ To delete a file
$ rm <file_name>

‡ You can remove more than one file at a time.
$ rm oldbills oldnotes oldjokes would remove the three named files. Options -r recursive -i interactive mode -f forceful

20

SAMPATH

Basic commands for file handling
‡ ls command ± Lists the contents of your current working directory ± Few useful options with ls are:‡ -l ‡ -a ‡ ‡ ‡ ‡ ‡ -i -x -C -R -F like*/=@| ‡ -ld long listing of files in a directory list in long format all files including those which begin with

dot
list the inode number in the first column sorts in multi column output across the page displays file in columns recursively list all directories and subdirectories indicates the type if files using symbols

21 SAMPATH

File management utilities
‡ cp : copy a file $cp <file1> <file2> ‡ mv : move / rename a file $ mv <file1> <file2> $ mv file1 file2 $ mv dir1/f ile1 dir2/fil2

22

SAMPATH

wc
wc word count
‡ -c character count ‡ -w word count ‡ -l line count ‡ $ wc employee ‡ To count the number of files in a directory To count the number of users currently logged in WC ±l file1 WC ±c file1 Wc ±w fil1

23

SAMPATH

tr
‡ Translates the input into some other form ‡ Eg: ‡ Convert all lowercase to uppercase
± tr ³[a-z]´ ³[A-Z]´ file1

24

SAMPATH

FIND ‡ find command
± Helps to find a file in the specified directory ± The syntax is find <path> -name <filename> ‡ find . ±name doc1.doc ‡ find . /tmp/project ±name new_data ‡ find /usr ±type d ±name backup

25

SAMPATH

File Comparisons
‡ ‡ ‡ ‡ comm : displays common lines in the two files cmp : to compare two files diff : reports the difference between two files uniq : display the duplicate and unique lines of a file

26

SAMPATH

comm
Customer A Navneet Neeraj Ritu Jamil Richa customer B Rachna Ritu Neeru Joby Seema

$ Comm customer A customer B Navneet Neeraj Jamil Richa Rachna Neeru Joby Seema

Ritu

27

SAMPATH

cmp
‡ Compares two files $ cmp file1 file2 Eg: document1 document2 This is a document. This a document. $cmp document1 document2 document1 document2 differ : char 6, line 1

28

SAMPATH

diff
‡ Reports the difference between two files $ diff [options] [file1][file2] -b ignore trailing blanks -h used for files of unlimited length Eg: file1 file2 cal cal cp cp crypt crypt z cut $ diff file1 file2 4c4 <z ---->cut

29

SAMPATH

uniq
‡ Displays the duplicate and unique line of a file ‡ $ uniq [option..] <filename>
± -u display only the unique lines ± -d display only the duplicate lines ± -c display all lines each preceded by the count of the records.

30

SAMPATH

uniq
Emp.dat E001 Shagufa C005 MKT E002 Shekhar C001 MKT E004 Anurag A001 PROD E004 Anurag A001 PROD E005 Madhu A002 ADMIN E005 Madhu A002 ADMIN ‡ $uniq emp.dat ( avoids duplicate lines, E004 and 5 are printed only once)

31

SAMPATH

Miscellaneous Sending and receiving mails
‡ $ mail <login name >
mail neeraj Subject : «. <ctrl+d>

32

SAMPATH

Input/ output redirection
‡ Input redirection ‡ Output redirection ‡ Standard error redirection

33

SAMPATH

Input redirection
‡ ‡ ‡ ‡ Changing the default input source Input redirection operator µ<µ $ command <filename After the execution the standard input is reassigned to the default device ‡ Eg :
± $ cat <emp.dat ( similar output will be displayed by cat emp.dat)

34

SAMPATH

Output redirection
‡ ‡ ‡ ‡ Changing the default destination of the output The output redirection operator : µ >¶ $ command > filename After the execution the standard output is reassigned to the default device ‡ Eg:
± ± ± ± $ cat emp.dat > emp.out $ date > todays_date $ cat > file1 $ cat >> file2

35

SAMPATH

Standard error redirection
‡ ‡ ‡ ‡ ‡ 0 standard input 1 standard output 2 standard error $ command 2 > err_file Eg :
± $ cat emp.dat > newemp 2 > emp_err

36

SAMPATH

Pipes and filters
‡ ‡ Pipes :Connects two or more commands Filters : Unix command which takes its input from standard input processes or filters it and generates and output.
± ± ± ± ± ± ± ± ± ± grep sort cut paste head tail wc pg More tr

37

SAMPATH

Filters: grep
‡ ‡ ‡ ‡ grep ± ( global search for regular expression and print) grep is the standard file searching and selection utility $ grep [options] ³pattern´ <filename> Options :
± -n : prints line numbers ± -v: the reverse search criterion ± -c: display only a count of matching patterns

38

SAMPATH

grep
‡ grep ³madhu´ emp.dat
± Display the line(s) containing the string madhu.

‡ grep ±n ³anurag´ emp.dat
± Display the lines containing the string anurag preceded by the line number

‡ grep ±v ³Madhu´ emp.dat
± Display the lines of the file excluding the lines contatining the string Madhu.

39

SAMPATH

sort
‡ sort [options] [-o output file name] [file name] ‡ options
± ± ± ± ± ± d letters, digits and blanks n sort on the numeric field r reverse sort u eliminate duplicate lines t filed separator o stores the output

40

SAMPATH

cut
‡ Retrieves selected fields from a file ‡ $ cut [options] <filename> ‡ Options
± -c selects characters specified by list ± -fselects fields specified by list ± -d field delimiter

41

SAMPATH

cut
‡ $ cut ±c4-9 employee
± Display characters 4 to 9 from each record

‡ $ cut ±d " " ±f2-4 employee
± Display 2-4 fields

‡ $ cut ±f 4- employee
± Display 4th field onwards

42

SAMPATH

paste
‡ Merges files horizontally. ‡ $ paste <file1> <file2> ‡ Options ±d delimiter

43

SAMPATH

Head and tail
‡ ‡ ‡ ‡ ‡ head employee will display first 10 lines head -5 employee tail employee last 10 lines from the employee tail -7 employee tail +10 employee display lines from 10th till the end of the file.

44

SAMPATH

Others

SAMPATH

45

The Unix manual

‡ Information on all the commands can be found in the on-line manual pages. ‡ $man <command> ‡ Shows the details (syntax and description) of the command ‡ help, info

46

SAMPATH

vi

$ end of the current line ^ or 0 beginning of the current line w beginning of the next word W same as w, but ignores the punctuation b back to the beginning of the current word e end of the current word

47

SAMPATH

vi
‡ Moving the cursor over the paragraph { to the beginning of the current paragraph } to the end of the current paragraph n{ nth paragraph above the current paragraph n} nth paragraph below the current paragraph ‡ Moving the cursor over sentences ( first character of the sentence ) last character of the sentence

48

SAMPATH

Screen movements
Ctrl+D scrolls down half a window Ctrl+U scrolls up half window Ctrl+B moves to the previous window Ctrl+F moves to the next window ‡ What is the command to move the cursor to the right by five words?

49

SAMPATH

Entering and deleting text
Append text :a, A Insert text Open a new line Delete : x nx dw ndw dd ndd :i, I :o, O

one character n characters current word n words line n lines

50

SAMPATH

Modifying text
r, R cw ncw cc ncc c$ replace character change current word change n words change current line change n lines changes remainder of the line

51

SAMPATH

Cutting and pasting
yy nyy yw nyw y$ yanks the current line and puts in the buffer yanks n lines yanks a word yanks n words yanks remainder of the line

P , p copy the contents of the current buffer U undo all changes to the current line u undo the last command

52

SAMPATH

Thank You

SAMPATH

53