Professional Documents
Culture Documents
Course Objective
To introduce the Unix Operating System. To develop an ability to use general Unix commands. To develop ability to write programs in shell. To develop ability in using Unix utilities. To introduce the security issues in Unix.
By the end of the course, you will have Knowledge of Unix Operating System. Knowledge about the different Unix commands. Knowledge about writing programs in shell. Knowledge about the different Unix utilities used in Software Development. Knowledge about the different security issues in Unix.
NOTE: This course does not cover Differences in command behavior in various Unix flavors. Introduction/Implementation of System Calls. System Administration in Unix. Hacking/tricky issues in using Unix commands. Awk and sed programming.
References
Brain W. Kernighan and Rob Pike, The UNIX Programming Environment. Stephen Prata, Advanced UNIX - A Programmers Guide. Stephen G. Kochan and Patrick H. Wood, Exploring the Unix System. James R. Groff, Paul N. Weinberg , Understanding Unix - A conceptual guide. Maurice J. Bach, The Design of the Unix Operating System
Session Plan
Day 1 Introduction to Unix. Unix System Architecture. Processes in Unix. Basic Unix Commands and Utilities. Regular Expressions. The vi editor. Text Processing Commands.
Day 1 Introduction to Unix. To understand Need for Unix in todays world. Chronology of events that led to the development of Unix. Different flavours of Unix. Features of Unix. Unix System Architecture. To understand Major Components of Unix. How Unix Works. Unix File System. Directory Organization. Processes in Unix. To understand the term Process in Unix and the various commands associated with it. Basic Unix Commands and Utilities. To understand Unix commands and utilities required for basic operations in Unix. Regular Expressions. To understand and match expressions viz wildcards, etc.Day 2 The vi editor. To understand Types of Editors. Different modes. Cursor Movement Commands. Text Processing Commands. To understand the concept and usage of pipes and redirection in Unix.
Session Plan
Day 2 Types of Shell. Shell Programming. File Handling Commands.
Day 2 Types of Shells in Unix. To understand the different types of shells and their features. Shell Programming. To develop an ability for writing shell scripts. File Handling Commands. To understand the commands associated with files . viz. file, touch, etc .
Session Plan
Day 3 Unix Utilities. Unix Program Development Tools. Unix Communication. Security Features
Day 3 Unix Utilities. To understand the different types of utilities like compression, tar, etc. Unix Program Development Tools. To understand tools like cc, make, SCCS, etc Unix Communication. To understand commands for communication: write, wall, ftp, telnet, etc.. Security Features. To understand Security features: Permissions, Passwords, etc.
Introduction to Unix.
What is Unix? Need for Unix in todays world? Evolution of Unix. Flavors of Unix.
Unix is a very popular multi-user, multitasking, time-sharing operating system. Unix has become the operating system of choice for various engineering and scientific applications. The need for Unix could be determined by the different categories of application it suffices. viz. networking, programming, multimedia, high-performance computing to name a few. The chronology of events that led to the evolution of Unix are as follows: 1965 : AT&T, GE, IBM and Project MAC join together to develop a time-sharing system named MULTICS (Multiplexed Information and Computing Service). 1969 : AT&T Bell Labs drops out of MULTICS project. Ken Thompson writes first version of UNICS on a PDP-7 machine. UNICS is pun on MULTICS and stands for Uniplexed Information and Computing Services. UNICS is changed to UNIX. 1973 : Re-written in high level language C, thus making it machine-independent. 1977-1981 : Unix was widely available at minimal cost and became popular for scientific applications. 1982 : Unix System III is released. 1984-85 : Unix System V is released. 1992-93 : 4.4 BSD is released. 1994 : Linux 1.0 is released. 2001 : Linux 2.4 is released. Unix is available in many flavors like : AIX (Advanced IBM Unix) HP-UX ( Hewlett Packard Unix) MINIX (Minimal Unix) SCO UNIX SOLARIS XENIX SUN OS LINUX
Features Of Unix.
The important characteristic features are
Portability. Security. Background Processing. Pipes. Redirection Tools. Software Development Tools. Stable and Reliable
Portability : As Unix has been re-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. Background Processing : Unix employs a technique of Background Processing, wherein jobs/tasks are executed in the background with the minimal interaction from the user. Pipes : Using the concept of pipes, a Unix user/administrator can easily link and work with multiple commands at the same time. Redirection Tools : These tools allow data to be re-directed between files as per the requirement of the user. Software Development Tools : Unix supports a wide variety of tools using which the user could create and work on different programs .viz. C Language on the Unix Operating system. Unix supports any language that has an interpreter or compiler. Stable and Reliable : Unix is a very reliable and stable Operating System. It is less prone to crashes.
Easy to write programs : Its easy to write programs in Unix as it hides machine architecture from the user. Hierarchical File System : Unix employs a hierarchical file system which is easy to implement and maintain. Shells : Unix has different types of shells .viz. Bourne, C, Korn, etc. Communication : Unix has commands which allow communication between different users connected to the system.
System Architecture.
Major components of Unix are :
Kernel Shell Utilities User Applications
10
Kernel, the most important part of OS, interacts directly with the hardware providing common services to programs and insulating them from hardware. It is a program that monitors and controls the resources of a computer and allocates them among its users in an optimal manner. The shell provides a processing environment for the user programs. It acts like a command translator. The shell offers the following facilities: File name short-hands through metacharacters Input/Output (I/O) redirection Personalizing the environment Defining new commands. The most powerful feature of the shell is, it is programmable. Shell scripts, which are a series of shell commands, can be executed using the shell. Utilities includes programs that interact with the kernel by invoking a well-defined set of system calls. Typically, these programs are Unix commands like ex, wc, grep etc., and may include certain user-developed programs also. User Applications are the programs which the user writes.
10
System Architecture.
User User
SHELL
UNIX
User User
SHELL
OTHER APPLICATIONS
COMMANDS
HARDWARE
DATABASE
PACKAGES
KERNEL
COMPILERS
SHELL
SHELL
User User
User User
11
Unix follows a layered approach. Layer 1 : This layer is the hardware over which the different OS layers are built. Hardware is not a part of the Unix OS. Layer 2 : This layer is the Kernel, which is the most important part of the Unix system. It is a collection of programs which directly communicate with the hardware. It is that part of the Unix system that is loaded into memory when the system is booted. It manages the system resources, allocates time between users and the processes, decides priorities of different processes, etc. Layer 3 : This layer contains programs that interact with the Kernel by invoking a welldefined set of system-calls. Typically, these programs are Unix commands like wc, grep, etc. Layer 4 : This layer is called Shell. Technically another Unix command, it is the interpreter of user requests. It takes a command from the user, deciphers it, and communicates with the Kernel to see that the command is executed. It is actually the interface between the user and the kernel, which effectively insulates the user from the knowledge of kernel functions. It also has a programming capability of its own. The kernel and shell together make the Unix system work. The shell provides a processing environment to the user programs.
11
12
Kernel: The Kernel performs various primitive operations on behalf of user programs: Control of execution of processes - creation, synchronization, termination, suspension and communication of processes. Scheduling of processes - deciding of time-sharing of processes. Memory management - Allocation of memory for executing processes. Two methods are used: Swapping: Entire process is written to swap device to accommodate another process. Paging: Pages of memory is written to swap device.
File system service - Allocation of secondary storage efficiently for user data files. Controlled access to peripherals, which are treated as special files.
Hardware Transparency. Processes usually run in one of the two modes: User mode - cannot access kernel instructions and data Kernel mode - can access kernel and user addresses and privileged instructions not allowed for user mode. The hardware only views processes in terms of kernel or user modes. It does not distinguish between the user processes and the OS has internal bookkeeping to distinguish between processes.
12
13
Interrupts and Exceptions Interrupt: Interrupt is a signal by which priorities and instruction-scheduling sequence can be controlled. The kernel saves the current context (a frozen image of what the processes was doing) when an interrupt occurs. After servicing the interrupt the context is restored and the process proceeds. The order in which an interrupt is serviced depends upon its priority. Exception: An exception is an unexpected event like reference to an illegitimate address caused by events external to a process - exceptions usually occur in the middle of a process where as interrupts occur between them. Different algorithms are used to handle interrupts/exceptions efficiently. Processor execution levels Based on the priority level of an interrupt the kernel masks certain interrupts in order that a critical process may proceed without error . The priority levels in descending order are: Machine error Clock Disk Network Devices Terminals Software Interrupts Memory Management The concept of a Virtual Machine is central to memory management under Unix. The compiler generates addresses for a virtual machine as if no other program will execute simultaneously on the physical machine. Virtual addresses generated by the compiler need not be identical to the actual physical addresses in the machine. The memory management coordinates with the machine hardware to setup a virtual to physical address translation that maps the compiler-generated address to the physical machine address.
13
14
From the users viewpoint the following aspects of Unix will be highlighted: Processing environment. File system. Building block primitives.
14
Processing Environment.
User Program. Process. Shell.
15
A program is just an executable file whereas a process is an instance of a program in execution. The creation / termination of processes, the synchronization and reaction to interrupts is controlled through system calls. An example of a program using the kernel through system calls is the shell - the COMMAND INTERPRETER. The shell is a user program that can be tailored to the needs of the user. The shell can also execute commands asynchronously i.e. without waiting for the prior command to terminate, background execution. The shell provides a processing environment for the user programs.
15
/ (root)
home bin sh
dev ls tty lp
etc
tmp
lib
usr
passwd terminfo
user1 user2
16
The Unix file system follows a tree structure with root represented by /. Every non-leaf node is a directory of files and the leaf nodes can be directories, regular files or special device files. The main features of a Unix file system: Hierarchical structure Consistent treatment of file data Create and delete files Dynamic growth of files Protection of file data Treats peripheral devices as files Keeps track of files using i-node numbers. Information of files is kept in the i-node block.
File access permissions can be set 3 classes of users: File Owner File Owners Group Others
Read (r), Write (w), Execute (x) permissions can be set for each file.
16
Another directory
file1 file2
The information pertaining to the Unix files is kept in the i-node (identification node) block, which is reserved exclusively for the use of kernel. Every file in the file system will invariably have an entry in this area. Every file is associated with an i-node number, called the i-number. The command ls is used to list the contents of the current directory. The option i used with ls would indicate the i-node number for a file.
17
18
There are 2 ways to access any file in Unix. They are : Absolute Path. Relative Path.
In Absolute Path method, the complete path from the root to the required file is mentioned. E.g. To access a file called file1, present in directory trng10, we need to write /home/trng10/file1 In Relative Path method, the path from the current position to the required file is mentioned. .. represents parent directory. . represents present working directory itself. E.g. If the current directory is /home/trng10, then to go to the previous directory , we write ..
18
The user connects to the Unix Server using an utility called telnet, and by mentioning the IP address of the Unix Server. The Server requests for the username and password. The user needs to give the valid username and password, for log-in. Once the username and the password is right, the shell-prompt is displayed, where-in the user could execute the commands.
19
Login Sequence.
Login id and password. The /etc/passwd file. The /etc/profile and .profile file. Shell prompt. Environment variables.
The $HOME, $PATH, $PS1, $TERM and other variables.
20
Connect to a machine from the classroom PC. And show the trainees how to login . Show the /etc/passwd file. The .profile file contains the settings for the user to customize his shell.
20
21
Show them the variables like $HOME, $PATH , $PS1 etc. Show some simple aliases using alias command. Tell them the importance of /etc/profile file. Show them the man command.
21
Processes in Unix.
Understanding the term Process. Process related commands.
ps command.
22
A Process is a program which is under execution. When any program (i.e. and executable file) is executed, it gives rise to a process. In other words, a process is said to be born when the program starts execution, and remain alive as long as the program is active. The process is said to die when the program completes execution. Since Unix is a multi-tasking system, multiple processes could run at the same time. Each process is identified by a number called Process Identifier (PID), which the Kernel allocates when the process is born. Just as a file has a parent, every process also has its own creator, which itself is another process. The process responsible for giving birth to another process is called the parent process, and the born process created is called child process. The parent process is identified by a number called PPID ( Parent Process Identifier). There are different commands associated with processes. ps command : This command lists out the processes associated with a user at a particular terminal.
22
23
E.g. $> ps PID TTY 20353 pts/2 20446 pts/2 TIME CMD 00:00:00 bash 00:00:00 ps
To see the details about the ancestry (PID and PPID) of a process, ps command has an option f. E.g. $> ps -f UID ajit ajit PID PPID C STIME TTY 20353 20352 0 16:31 pts/2 20539 20353 0 16:34 pts/2 TIME CMD 00:00:00 -bash 00:00:00 ps f
23
24
The Unix philosophy supports building complex programs from simple primitives. The following are supported: Redirection of I/O Both the standard input and the standard output can be re-directed from/into files: $> cat prog1 > prog2 Here the contents of prog1 file are redirected to fie prog2. $> cat>prog1<prog2 The contents of the file prog2 is transferred to the file prog1. Pipes A pipe is a mechanism that allows a stream of data to be passed between the read and the write processes. It allows the output of one command to become input of another command. In other words, the standard output file of the command gets redirected to standard input file of the next command . Example $> ls | wc -l Counts the number of lines in the directory display. Note: In most situation pipes makes it unnecessary to create temporary files.
24
25
Unix is all about working on commands. The user can execute commands or create programs containing the commands for his/her application. Shell reads lines - either executes them or interprets them. Each i/p line is scanned and split into tokens parameters are substituted (subject to quoting) filenames are generated I/O are redirected (optional) commands are executed For executing a command, the user simply needs to write the command and press the Enter key for its execution. Example. $> pwd /home/ajit Some commands have options which allow a detailed display. Example. $> ls -l ls is a command in Unix to display the contents of the current directory. Attaching an option l , displays in detail the contents. Some commands have arguments which allow a display, as per user requirement. Example. $> cal 01 2004 cal is a command to display the calendar information. The above statement would display the calendar for January 2004. Here 01 and 2004 represents arguments and are not options.
25
26
ls (List) List contents of a directory. Output is sorted alphabetically by default. Option -a list includes files starting with (.) Option -i lists the i-node numbers. Option -l lists in long formats - includes the mode, number of links, owner, group, size in bytes, time of last modification and name of the file. Option -r lists the contents in reverse order. Example: $> ls a.out dem2 file1 makefiles sds asc demo1 file2 name shell1.sh cat - Concatenate and display files. The command cat with filename, would display the contents of the file. The command used with sign > , is used for creating a file, or over-writing if it already exists. The command used with sign >> , is used for appending a file. Examples. $> cat prog1 Output : Displays the contents of the file prog1. $> cat > prog2 Output : Creates a file by the name prog2. $> cat >> prog1 Output : Concatenates the contents of the file prog1.
26
27
cp - Copy files. Copies the contents of file1 to file2. The file1 is kept intact. If a directory is mentioned in place of file2, then file1 is copied into the mentioned directory. Directory must exist before the execution of the command. Examples $> cp demo1.c demo.c Output : Copies the contents of file demo1.c to a file demo.c $> cp demo1.c cprogs Output : Copies the contents of file demo1.c to a directory cprogs. mv - Move or rename files. Moves the contents of file1 to file2. The file1 is deleted. If a directory is mentioned in place of file2, then the file1 is moved to the mentioned directory. Examples $> mv s2.c s3.c Output : The contents of the file s2.c is moved to file s3.c. It could also be said that the file s2.c is renamed as s3.c, as the file s2.c no longer exists. $> mv s2.c cprogs Output : The contents of the file s2.c is moved to the directory cprogs.
27
28
pwd - Display Working directory. Example. $> pwd Output : /home/ajit cd - Change Working directory. This command is used to change the working directory. Example. $> cd cprogs Output : Changes the working directory to the directory named cprogs. $> cd Output : Changes the working directory to the home directory.
28
29
mkdir - Make Directories. Creates directories by the name requested by the user. Standard entries in a directory ( ., .. ) are made automatically. Option -m allows user to define the mode to be used for directory. Example: $> mkdir javaprogs Output : Creates a directory by name javaprogs. rmdir - Remove Directories. Removes the specified directory if it is empty .i.e. it does not contain any files or directories. Example: $> rmdir javaprogs Output : Removes the empty directory javaprogs.
29
30
rm - Remove files or directories. Removes one or more files from a directory. Removal of a file requires write permission in its directory. Option -f force the removal of files that do not have write permission. Option -r recursively delete the entire contents of directories specified and the directories themselves. Option -i interactively ask to delete each file. Example: $> rm dept.dat Output : Deletes the file dept.dat. ln - Make link to files. A link is a directory entry to a file. Any changes to a file are effective, independent of the name by which the file is known. Links are of two types : Hard Link and Symbolic Link. Example: $> ln cprog1 cprog1.bat This is an example of hard link. cprog1 should already exist and cprog1.bat should not exist already. If cprog1 is removed, cprog1.bat is not deleted. cprog1 and cprog1.bat have same i-node numbers ( that could be seen in the long listing). $> ln s cprog2 /tmp/cprog2.bat This is an example of symbolic link. cprog2 should already exist and cprog2.bat in tmp directory should not exist. This creates a link to cprog2.bat under tmp directory. If cprog2 is removed cprog2.bat stays as an invalid link. cprog2 and cprog2.bat have different inode numbers ( that could be seen in the long listing). could be seen in the long list of the symbolically linked file entries.
30
31
cal Displays Calendar. Examples. $> cal Output : Displays the calendar for the present month. $> cal 2004 Output : Displays the calendar for the year 2004. $> cal 04 2004 Output : Displays the calendar for the month April 2004. who Displays the users connected to the system. Example $> who deepak pts/0 May 11 09:20 (172.21.102.80) isjan8 pts/1 May 11 14:19 (172.21.103.107) comp10 pts/7 May 11 14:50 (172.21.102.186) osjan6 pts/2 May 11 15:09 (172.21.106.100) pract1 pts/9 May 11 15:26 (172.21.106.107)
31
32
more Allows display page-wise Example /home/ajit>more grt1 grt4 Output : Displays each file pagewise. On pressing the Enter key, the 2nd file gets displayed. whoami Displays the username. Example $> whoami ajit The command if used with spaces, would give details about the user. $> who am i ajit pts/2 Jun 27 17:15 (myshec25336d.ad.infosys.com)
32
Standard Files.
Standard Input (0).
This file is opened by shell to accept information.
33
Standard Input (0) Takes the keyboard or files as input. Example $> mail user1 < s1 #s1 is the name of the file. The contents of s1 is mailed to user1. Here the input for the command mail is coming from file user1
Standard Output (1) This is the default file. Example $> ls 1> infy infy. Is same as $> ls > infy Standard Error (2) Is used for redirecting the errors. Example $> ls abcd11 2> def def $> cat def ls: abcd11: No such file or directory #Redirects the errors into a file called #Redirects the output to a file called
33
Special Files.
/dev/tty
It is used to display on the terminal.
/dev/null
This file is used to suppress the output from being displayed. Used for suppressing error messages from being displayed on the screen in programs.
34
/dev/tty Allows display of output on ones own terminal. Example $> ls > /dev/tty Output : Outputs the directory listing on the terminal screen. /dev/null Suppresses output from being displayed. Example $> ls > /dev/null $> Output : Output of ls will not be displayed.
34
Regular Expressions.
What is it? String of ordinary and metacharacter which can be used to match more than one type of pattern. Uses character set
* , [], ^, $, {}, etc.
35
A regular expression is a string of ordinary and metacharecter which can be used to match more than one type of pattern.
35
36
* - Matches all filenames in current directory. $> ls a* Output : Displays the files and directories starting with the letter a. ? - Matches a single character. $> ls f? Output : Displays the files and directories starting with the letter f, followed by a single character. [abc] - Matches a single character either a, b or c. $> ls f[abc] Output : Displays the files and directories starting with the letter f, followed by a single letter which is either a, b or c. [!abc] - Matches a single character which is not a, b or c. $> ls f[!abc] Output : Displays the files and directories starting with the letter f, followed by a single letter which is not a, b or c. [a-c] Matches a single character which is within the range of a and c. $> ls f[a-c] Output : Displays the files and directories starting with the letter f, followed by a single letter which is in the range within a and c.
36
Unix.
The Vi Editor.
37
Editor in Unix
Need for editor in Unix. Types of editor
Line Editor.
ed : UC Berkeley ex : Powerful than ed, Bell Systems
38
Type of editors available under UNIX Line editors ed : UC Berkeley ex : Powerful than ed, Bell Systems Screen editors Vi (stands for visual) is the full screen editor VIM - Vi Improved Emacs (GNUs)
38
The vi Editor.
The important characteristic features are:
Omnipresent. Fast. Powerful UNDO features.
39
Omnipresent : vi Editor works on different Unix flavors. Fast : In vi editor, Editing and various operations are very fast. Powerful UNDO features : In vi Editor, texts in lines could be undone with very less effort.
39
The vi Editor.
The limitations are:
Less user-friendly. Highly Case-sensitive. Keystrokes could have more than one meaning.
40
Less User-friendly : As vi Editor does not have a graphical user interface, hence the user needs to have a knowledge of the commands before the user works on the editor. Highly case-sensitive : vi Editor is highly case-sensitive. i.e. a letter in small case would has a different meaning in comparison with the same letter in upper case. E.g. a appends the data after the current character, whereas A appends the data after the current line. Keystrokes : As vi Editor operates in various modes, a letter could have different meaning in different modes. E.g. In command mode, a stands for append operation, whereas in insert mode, a stands for data.
40
The vi Editor.
Modes of working:
Command Mode. Insert Mode. Escape Mode.
41
vi works in 3 different modes Command mode Insert mode Keys echoed in edit buffer. The letter entered represents data. Keys interpreted as commands of ex mode. E.g. :wq is used for saving and exiting from vi editor. Escape mode Keys are interpreted as commands. E.g. a is used for appending, i is used for inserting, x is used for deleting.
41
vi Operating modes.
i, I , o, O, a, A ..
Command mode :
Enter esc
Insert mode
Escape mode
:q
42
Whenever a particular file is opened using the vi editor, it opens up in the Command mode. In this mode, the keyboard-character represents command and not data. A user cannot enter the data in the Command mode. The user has to switch over to the insert mode to enter the data. This could be done using the letters like a, i, etc. Once the user is in insert mode, the data could be entered. The user could switch back to the Command mode. For saving or exiting the application the user has to get into the Escape mode by pressing Esc key.
42
Vi editor commands.
To move around
h, j, k, l, ^D, ^U, G, 0, ^, $, w, b
Inserting/Deleting text
i, a, I, A, r, R, o, O, dd, dw, c$, D, x, X.
Changing/Replacing text.
cc, cw, c$, ~, J, u, . , yy, yw, p, P
File manipulation.
:w, :wq, ZZ, :w!, :q, :q! , :![command]
43
The instructor should show few commands, to make the audience aware of the usage in vi editor. Rest of the commands could be given as a self-study and practice for the audience.
43
Searching a pattern.
/pattern
Searches forward for first occurrence of a pattern.
?pattern
Searches backward for first occurrence of a pattern.
n
Repeats the last search.
N
Repeats the last search command in opposite direction.
44
44
Pattern Substitution.
:s/ptn1/ptn2
Replaces first occurrence of ptn1 with ptn2.
: s/ptn1/ptn2/g
Replaces all occurrences in the current line.
: m, n s/ptn1/ptn2/g
Replaces all occurrences in lines m to n.
: ., $ s/ptn1/ptn2/g
Replaces all occurrences from current line to end of file.
45
45
Customizing vi.
The set command.
:set all :set nu
46
Using the set command, the user could customize the shell.
46
Vi Startup File.
.exrc
vi reads .exrc before loading. settings are permanent for a vi session.
47
47
:n
Permits editing of next file in the buffer.
: rew
Permits editing of first file in buffer.
: args
Displays names of all files in the buffer.
:f
Displays the name of the current file.
48
48
Unix.
Advanced Unix Commands
49
50
50
cmp
Compare 2 files. cmp file1 file2
51
tr - Translate characters. tr is a filter program that doesn't recognize file arguments. Option. -c complement the content of string1;that is, all characters except those in string1 are considered. Option -d delete all the input characters specified in string1.Any string2 is ignored. Option -s compress all strings of repeated output characters that are in string2 into single character. Example: $> cat prog1 | tr a b Output: This will convert any occurrence of the letter a in the file to the letter b. $> cat prog1 | tr -s : Output: This will convert all multiple occurrences of white spaces between the words in the text into one and replaces spaces with a :. cmp - Compares 2 files. Compares two files and if they are different, it displays the byte and the line number of the first difference. Option -l prints the byte number and the differing bytes for each difference Option -s returns an exit code 0 for identical files, 1 if files are different and 2 if file is inaccessible. Example: $> cmp prog1 prog2
51
paste
Merging the corresponding lines of given files. paste file..
52
cut - Cuts selected fields of each line of a file. List following -f is a list of fields assumed to be separated by a delimiter character. Character following -d is the delimiter. Example: $> cut -f1 -d : prog1 Output: This statement extracts the first field from file prog1. The delimiter between the fields is a :. $> cut -c1 prog1 Output: This statement extracts the first character of all the lines from file prog1. paste - Merging the corresponding lines of given files. This command allows horizontal pasting of files. Example. $> cat file1 1000 2000 $> cat file2 8000 9000 $> paste file1 file2 1000 8000 2000 9000
52
comm
Compares 2 sorted files. comm file1 file2
53
sort - Ordered arrangement. Option -d dictionary order Option -n arithematic order Option -r reverse order Example: $> cat file3 frankfurt mysore canberra $> sort file3 canberra frankfurt mysore
53
54
comm - Compares 2 sorted files & displays a three-columnar output. The first column contains lines unique to the first file, while the second column shows lines unique to the second file. The third column displays lines common to both the files. Example: $> cat data1 argentina india myanmar $> cat data2 brazil denmark india $>comm data1 data2 argentina brazil denmark india myanmar
54
tail
Display the last count lines of a file. tail [+/-[number] [lbc]] [option] file
55
head - Display the first count lines of file If count is not specified, then ten lines are printed. If no files are specified, head reads from the standard input. Example: $> head -5 prog1 Output : The first 5 lines of the files would be displayed. tail - Display the last count lines of a file. Copies the last part of the named file to the standard output. Displays lines of file beginning with +number from the beginning or -number from end. Number is counted in units of lines (l), disk block size (b), or characters (c). By default, number is counted in units of lines. Option -f enables tail to follow the growth of a file. Example: $> tail -5 prog1 Output : The last 5 lines of the files would be displayed.
55
grep
Pattern searching in a file. grep [option...] pattern [file...]
56
wc - Counts and displays the number of lines, words and characters in a file. Option -l display the number of lines Option -w display the number of words Option -c display the number of characters (includes whitespace characters ). Example: $> wc cprog1 Output : 3 9 60 cprog1 $> wc -c prog2 Output : 60 cprog1 grep - Pattern searching in a file. Display the lines containing the pattern on the standard output. Option -c report only the number of matching lines Option -l list only the names of files containing pattern. Option -v display all lines except those containing pattern. Example: $> grep the prog2 Output: Displays all the lines in the file prog2 containing the word the. $> grep -c the prog2 Output: Displays the number of lines in the file prog2 containing the word the.
56
uniq
To remove adjacent repeated lines. uniq [option] [file]
57
diff - Reports the differences between files. Option b ignore trailing whitespaces and compare other strings of space characters. Example: $> diff prog1 prog2 uniq - To remove adjacent repeated lines. Option -u display only the lines not repeated in the file named input. Option -d display only the lines that are repeated in the file input. Option -c precede each line displayed by the number of times it occurs. +n ignore the first n characters including whitespace -m ignore the first m fields and any blanks. Example: $> uniq -u prog1
57
nice
Execute commands with lower priority. nice [option] command [arguments]
58
NOHUP Continue to execute the named command and optional command arguments after you log out. NICE Execute a command and arguments with lower priority (i.e., be nice to other users) Option: -n Run command with a niceness of n ( 1-19). Default is 10. Higher n implies lower priority. A privileged user can raise the priority by specifying a negative n ( e.g., -4)
58
59
kill - send signal to processes Kill sends signal to the process with process ID as pid. If no option is specified ,it sends signal 15 (software termination signal) to the process. Option - 1 Hangup signal. Option - 2 Interrupt signal. Option - 9 terminate the process. Option -15 Software termination signal. Example: $ sleep 20 & [1] $ kill -9 280 [1] + killed sleep 20 & 280 #280 is the PID
59
Summary
Background Features of Unix Unix System Architecture Unix File System Processes General Unix commands and utilities. Regular Expressions.
60
60
Summary (Contd...)
Vi Editor Modes of operation Pattern Searching Text Processing Commands Process oriented commands.
61
61
Thank You!
Copyright 2004, Infosys Technologies Ltd 62 ER/CORP/CRS/OS31/003 Version No: 2.00
62