46
UNIT -1 1.1 Introduction: A computer system cannot function without an operating system. “An operating system is a collection of programs that co-ordinates the operation of computer hardware and software”. It is a life – giver to a computer. Technically speaking it is a program that is loaded into the computer’s memory when the system is booted, and it always remains there. There are many different operating systems available for Personal Computers, minicomputers and mainframes, the most common ones being Windows NT and 2000, VMS and variations of UNIX. UNIX is available for many different hardware platforms whereas most other Operating Systems are tied to a specific hardware family. What is UNIX? UNIX is a powerful computer operating system originally developed at AT&T Bell Laboratories in the 1960’s. It is very popular among the scientific, engineering, and academic communities due to its multi-user and multi-tasking environment, flexibility and portability, electronic mail and networking capabilities, and the numerous programming, text processing and scientific utilities available. It has also gained widespread acceptance in government and business. 1.1.1 UNIX features: 1. Multi user capability: It allows many users to access a computer system at the same time (multi user capability). It shares CPU’s memory and disk space in a fair and efficient manner among competing processes. 1. It supports the creation, modification and destruction of programs, processes and files. 1. Hierarchy: It provides directory hierarchy that gives a location to processes and files. 1. Multi tasking capability: it is capable of carrying out more than one job at the same time. 1. Communication: Unix has excellent provision for communicating with fellow users. The communication may be within the network of a single main computer, or between two or more such computer networks. The users can easily exchange mail, data, programs through such networks. 1. Security: Unix allows sharing of data, but not indiscriminately. Unix has three inherent provisions for protecting data. The first is provided by assigning passwords and login names to individual users ensuring that not anybody can come and have access. At the file level, there are read, write and execute permissions to each file which decide who can access a particular file, who can modify it and who can execute it. Lastly, there is file encryption. This utility encodes a file into an unreadable format, so that even if someone succeeds in opening it, our secrets are safe. 1. Portability: one of the main reasons for the universal popularity of Unix is that it can be ported to almost any computer system. 2. It supports both Graphical User Interface (GUI) and Character User Interface (CUI).

Linux Important Material

Embed Size (px)

Citation preview

Page 1: Linux Important Material

UNIT -1

1.1 Introduction:

A computer system cannot function without an operating system. “An operating system is a collection of programs that co-ordinates the operation of computer hardware and software”. It is a life – giver to a computer. Technically speaking it is a program that is loaded into the computer’s memory when the system is booted, and it always remains there. There are many different operating

systems available for Personal Computers, minicomputers and mainframes, the most common ones being Windows NT and 2000, VMS and variations of UNIX. UNIX is available for many different hardware platforms whereas most other Operating Systems are tied to a specific hardware family.

What is UNIX?

UNIX is a powerful computer operating system originally developed at AT&T Bell Laboratories in the 1960’s. It is very popular among the scientific, engineering, and academic communities due to its multi-user and multi-tasking environment, flexibility and portability, electronic mail and networking capabilities, and the numerous programming, text processing and scientific utilities available. It has also gained widespread acceptance in government and business.

1.1.1 UNIX features:

1. Multi user capability: It allows many users to access a computer system at the same time (multi user capability). It shares CPU’s memory and disk space in a fair and efficient manner among competing processes.

1. It supports the creation, modification and destruction of programs, processes and files.

1. Hierarchy: It provides directory hierarchy that gives a location to processes and files.

1. Multi tasking capability: it is capable of carrying out more than one job at the same time.

1. Communication: Unix has excellent provision for communicating with fellow users. The communication may be within the network of a single main computer, or between two or more such computer networks. The users can easily exchange mail, data, programs through such networks.

1. Security: Unix allows sharing of data, but not indiscriminately. Unix has three inherent provisions for protecting data.

• The first is provided by assigning passwords and login names to individual users ensuring that not anybody can come and have access.

• At the file level, there are read, write and execute permissions to each file which decide who can access a particular file, who can modify it and who can execute it.

• Lastly, there is file encryption. This utility encodes a file into an unreadable format, so that even if someone succeeds in opening it, our secrets are safe.

1. Portability: one of the main reasons for the universal popularity of Unix is that it can be ported to almost any computer system.

2. It supports both Graphical User Interface (GUI) and Character User Interface (CUI).

Page 2: Linux Important Material

3. It is Case Sensitive operating system. It treats lower-case characters differently than upper-case characters. For example, the files readme, Readme, and README would be treated as three different files.

4. A major feature of Unix is that much of it was written in C and made it very popular as a systems programming language.

1.1.2 Unix system Architecture:

The functioning of Unix is divided in three levels: on the outer crust reside the application programs and other utilities, which speak over language. At the heart of Unix, on the other hand, is the kernel, which interacts with the actual hardware in machine language. The stream lining of these three modes of communication is done by the middle layer, called the shell. The shell or the command interpreter as it is called, is the mediator which interprets the commands that we give and then conveys them to the kernel which ultimately executes them. The kernel has various functions. It manages files, carries out all the data transfer between the file system and the hardware, and also manages memory.The kernel program is usually stored in a file called ‘Unix” whereas the shell program is in a file called sh. At a particular point in time there may be several shells running in memory but only one kernel. This is because, at any instance Unix is capable of executing only one program as the other programs wait for their data. And since it’s the kernel which executes the program one kernel is sufficient and since the user interacts with the kernel through the shell different shells are necessary.

. . .

Page 3: Linux Important Material

Fig: Unix Architecture

Page 4: Linux Important Material

1.2 The UNIX File System

The Unix file system resembles an upside down tree. Thus, the file system begins with a directory called root. The root directory is denoted as slash (/). Branching from the root there are several other directories called bin, lib, usr, etc, tmp and dev. The root directory also contains a file called Unix which is Unix kernel itself. These directories are called sub-directories, their parent being the root directory. Each of these sub-directories called sub-sub-directories. The following figure shows the basic structure of the Unix file system:

STRUCTURE OF A FILE SYSTEM

Page 5: Linux Important Material

1. The bin directory contains all executable files, for the most of the Unix commands which the user can run.

2. The sbin directory contains unix commands which are required for Administrator. The system contains there machine language files in sbin directory.

3. The proc directory contains system information as per process based. It well very much useful to identify the regions for system crashes.

4. The lib directory contains all library functions, provided by the Unix programmers.5. The dev directory contains files that control various input/output devices like

terminals, printer, disk drivers etc.6. In the usr directory there are several directories, each associated with a particular

user. Within the usr directory there is another bin directory, which contains additional Unix command files.

7. The tmp directory contains the temporary files created by Unix or by the users.8. The usr directory contains compilers, windowing software’s, games, debuggers,

manual pages, other documentation along with header files and language files. a. The usr/local directory contains a site specific software (whenever we loaded

new software into system it is stored in local directory). b. The usr/include directory contains the standard header files used by c

programs.c. The usr/lib directory contains all library files in binary form. The user require

to link c programs with files in these directories.9. The etc directory contains the configuration files of the system. Which are required

to the modify by the admin whenever he wants to add a new driver or partition or win wide driver or has a user or enable a network card etc.,

10. The boot directory contains operating system files i.e kernel files.11. The home directory contains one directory for every legal user of the machine. For

users can create, remove, modify the files and directories in his home directory only nowhere else they will be having freedom unless the users are ignorant about how to protect themselves.

12. The var directory contains all print jobs and outgoing and incoming mails.13. The man directory contains the format and displays the on-line manual pages.

manpath - determine user's search path for man pages.

Following are the salient features of the Unix file system.1. It has a hierarchical file structure1. Files can grow dynamically1. Files have access permissions1. All devices are implemented as files

Page 6: Linux Important Material

1.3 LINUX UTILITIES:

There are several hundred commands in UNIX. The Utilities are used to do specific task of required by user. We can issue commands at $ prompt.

1 All Unix commands must always be entered in small case letters.1 Between the command name and the options there must be always a space or a tab.

For example, ls –l. Here ls is the command whereas –l is the option and the two have been separated by a space. The option is usually preceded by a minus (-) sign. The options available with a command are often known as switches.

1 Two or more options available with a command can usually be combined. For example, the command ls –l –a is same as ls –la.

1 If you make a typing mistake, press backspace to erase characters. Don’t try to correct them using arrow keys and then deleting with the Del key.

1 To cancel the entire command before you press Enter, press the Del key.

Basic Commands:

1.3.1 Creating Files:

To create a file with a few lines in it, cat command is used.

Syn: $cat options <filename>

option “>” – Creating file“<” – Displaying content of given file.“>>”– Appending to output redirection operator“–“ – Interactive input“<<“ – Here the document

Ex: $ cat > exampleVCE is affiliated to JNTU HYDERABADIt offers UG & PG coursesThen press ctrl^d (which is EOF)

To display the content of a file, $cat filenameEx: $cat <exampleThen the content of example file are displayed.The cat command is also used to concatenate the contents of two files and store them in the third fileEx: $cat example1 example2 > new1This would create ‘new1’ which contains contents of ‘example1’ followed by ‘example2’. If ‘new1’ already contains something it would be overwritten.If the contents of new1 should not change, then ‘append output redirection operator’ >> is used.

Ex: $cat example1 example2 >> new1

1.3.2 Present Working Directory:

To display current working directory of user, pwd command is used.

Syn: $pwd

Page 7: Linux Important Material

Displays current working directory of user

Ex $ pwd /usr/user1Here the current working directory is displayed as /usr/user1. The / denotes the root directory of the Unix file system. Within this root directory there is a sub- directory called usr within which there is another directory called user1. That’s when we are working right now. Hence the path from the root directory is listed as /usr/user1.

1.3.3 Change Directory:

To change current working directory to new directory.

Syn: $cd <directoryname>

It change directory to new directory

Ex: $ cd ..This will change directory to previous directory.cd when given without any argument is interpreted by the shell as a request to change over to the current user's home directory.

1.3.4 Listing files and directories:

Syn: $ls options <directoryname>/<filename>

The ls command gives the directory listing or lists the contents of current or specified directory.

Ex: $ lsonedw.citsvecIt lists all the files and sub directories of current working directory.

Ex: $ls -a-a option is used to display the hidden files.Here. stands for current directory.. stands for parent directory.Ex: $ls p*It displays all the files starting with character ‘p’Ex: $ls ?ainThis displays all the files ending with ‘ain’, where the length of the file is 4 characters (starting character may be any letter).Ex: $ls [aeiou]*This indicates that the first character of the filename to be listed must be any one of the character given within the square brackets.Ex: $ls [!aeiou]*The ‘!’ symbol complements the condition that follows it. Hence the above command would list all those files whose first character is anything other than a vowel.

Page 8: Linux Important Material

The [ ] is always substituted by a single character.Ex: $ls [a-m][c-z][4-9]??This will list all 5 character filenames in the current directory whose first character is in the range ‘a’ to ‘m’, the second character is in the range ‘c’ to ‘z’, the third character is in the range‘4’ to ‘9’, whereas the fourth and fifth are any valid characters.$ ls –lTotal 22

‘Total 22’ indicates that the total number of disk blocks that the files in the current directory have occupied is 22. unix treats all entities files directories, devices as files. Thus to distinguish between all of them it uses file types. All the lines listed in the output of the command ls –l the left most character indicates this type. A ‘-‘ indicates the file is an ordinary file, whereas a ‘d’ indicates that it is a directory. Other possible file types are given bellow:Ex: $ ls –rThis lists all the files in the present directory including the files present in any sub-directories that may be present in the current directory.Two more options are useful with ls ‘-s’ and ‘-i’. The ‘-s’ option lists the files along with their sizes (in blocks, not bytes), whereas, the ‘-I’ option lists the files along with their inode numbers.lc: It displays the files in columns fashionlf : It puts a * after all executable files and a / after all sub-directories present in the current directory.Thus, lf provides a handy way to determine which is an ordinary file, which is a directory and which is an executable file. File type Meaning

- ordinary filed directory filec character special fileb Block special filel symbolic links semaphorep named pipem shared memory file.

The character special files and the block special files are normally present in the /dev directory. These files are used to handle character-oriented devices like terminals or block-oriented devices like disks.The next nine characters following the file type character are the file permissions. Each column in succession gives the m=number of links, owner name, group name, size of file in bytes, date and time when the file was last modified, and finally the file name.

Ex: $ln file1 filename1This establishes one more links for the file, in the form of the name filename1.The number of links to a file is determined by the number of different names through which the file is accessible. When a file has two links, it is not physically present at two places, but can be referred to by either of the names.If one file is deleted, then another exists. The concept of having several links to a file offers another advantage. If one file is to be shared between several users, instead of giving each user a separate copy of the same file we can create links of this file in each user’s directory. This avoids unnecessary duplication of the same file contents in different directories.By default any new file that we create has one link whereas any new directory we create has two links. Because that directory name appears in two directory files. For example, if

Page 9: Linux Important Material

we create a directory ‘dir1’ in a directory ‘d1’, the directory file ‘d1’ would have an entry ‘dir1’, whereas the directory file ‘dir1’ itself would also have an entry ‘dir1’ (which stands for current directory).

1.3.5 bc: best calculatorOnce you type bc at the prompt, you are in the calculator mode, and the $prompt disappears.

Ex: $bc10/2*210quit

Ex: $bc scale=1 2.25+1 3.25

The variable scale is used to indicate the no.of digits after decimal point. Here, the actual result is more than 3.3, (i.e 3.35) that's why scale has no effect.

Ex: $bcibase=2obase=1611010011892751010Aquit

By setting the variable ibase to 2 and obase to 16 all input that we supply is taken as a binary number whereas all output is displayed in hexadecimal.

bc also supports functions like sqrt, cosine, sine, tangent etc.

$bcsqrt(196)14

$bc -l s() & c() are sine() and cosine() which are invoked only with -l option of bc.scale=2s(3.14)0

1.3.6 To display the current date and time, there is the date command.$dateFri may 16 08:25:53 IST 1997

1) $date '+Date:%d-%m-%y%n Time:%H:%M:%S'Date:17-12-05Time:10:55:35

Page 10: Linux Important Material

Different Linux Utilities are:

1. File Handling Utilities: cp, mv, rm, mkdir, rmdir.2. Security by File Permissions: chmod, chgrp, chown, umask.3. Disk Utilities: du, df, ulimit, find, mount, umount. 4. Process Utilities: ps, who, w, finger.5. Text Processing Utilities: head, tail, sort, nl, uniq, grep, egrep, fgrep, cut,

paste, join, more, pg, wc, tee, comm, cmp, diff, tr, ln, awk.6. Backup Utilities cpio, tar. 7. Network Utilities: ftp, rlogin, telnet, arp.

1.4 File Handling Utilities: cp, mv, rm, mkdir, rmdir.

1.4.1 cp (Copying Files)

The cp command copies a file or a group of files. It creates an exact image of the file on disk with a different name.

Syn: $cp options <sourcefilename> <destinationfilename>

Options:-i – Interactive copying-R – Copying Directory Structure

Ex: $cp t1 t2This will copy the contents of t1 into t2. If t2 does not exist, it will be created. However, if t2 already exists, then its contents are overwritten.Ex: $cp t1 t2 threeProvided that the directory three exists, both files t1 and t2 would be copied to it.Ex: $cp /usr/a1/chapter1 /usr/a1/n1/chap1Here the file chapter1 is copied from the directory /usr/a1 to the directory /usr/a1/n1 instead of chapter1.Ex: $cp -i t1 t2cp: overwrite t2 (Yes/No)?This will copy the contents of t1 into t2. If t2 does not exist, it will be created. However, if t2 already exists, then it warns the user before overwritten contents to a file. If “y” at this prompt overwrites the file, any other response leaves it uncopied.Ex: $cp –R progs newprogsThis will copies all files and sub directories in progs to newprogs directory recursively.

1.4.2 rm (Removing files)

The rm command removes the given file or files supplied to it.

Syn: $rm options <filename(s)>

Options:-i – Interactive deletion -r – Recursive deletion-F – Forcing Removal

Page 11: Linux Important Material

Ex: $rm –i file1Where –i is a switch, removes file1 interactively; i.e. you are asked for confirmation before deleting the file.Ex: $rm –r dir1This command recursively (-r) removes all contents of ‘dir1’ and also ‘dir1’ itself.rm used with –F option removes files forcibly, irrespective of whether you have write permission to them or not.

1.4.3 Renaming of files:

The mv command renames files. It has two functions:1. It renames a file (or directory). 2. It moves a group of files to a different directory.

Syn: $mv <sourcefilename> <renamefile>/<directoryname>

Ex: $mv s1 t1Then s1 is renamed as t1.Ex: $mv olddir newdirThe mv command also has the power to rename directories. ‘olddir’ will be renamed to ‘newdir’, provided ‘newdir’ is not already existing.

Ex: $mv file1 file2 newdirOn execution of this command ‘file1’ and ‘file2’ are no longer present at their original location, but are moved to the directory ‘newdir’.

1.4.4 To create a directory

The mkdir command is used to create a new directory

Syn: $mkdir options <directoryname>

Ex: $ mkdir bookThe above command creates a directory named 'book'Among the options available with mkdir is -p, which allows us to create multiple generations of directories specified in the given path too.

Ex: $mkdir -p works/dir1/unix/bookThe '-p' option tells Unix to first create works, then within it dir1 next its child directory UNIX, and lastly book, nested within all these.

Ex: $mkdir -m 754 newdirThis creates a directory with permissions 754 irrespective of umask value.

1.4.5 To remove empty directory

Syn: $rmdir options <directoryname>

The rmdir is used to remove directories.

With '-p' option, it removes not only the specified directory but also its parent directories. However rmdir removes only the empty directories.

Page 12: Linux Important Material

Ex: $ rmdir -p works/dir1/unix/book

It removes the directory book.In order to remove the parent directories of book, we say here on removing the book directory if the Unix directory falls empty then it is removed. On removing the Unix directory if the dir1 directory falls empty then that too gets removed and so on. This process stops when rmdir bumps into a non-empty parent directory.

Ex: $mkdir newdir$cd newdir$pwd/usr/user1/newdir

That is, the current directory is changed to newdir. To get back to the original directory.

$cd$pwd/usr/user1

Security by File Permissions: chmod, chgrp, chown, umask.

1.5 File permissions:

The permissions signify who all can access the file, and for what purpose. There are three types of permissions to a file.r readw writex execute

There are three entities to which any combination of these permissions are assigned. These entities are the owner, the group, and the rest (those outside the group). In ls –l, a set of nine characters denote these permissions of the nine characters, the first three characters decide the permissions held by the owner of the file. The next set of three characters specify the permissions for the other users in the group to which the file owner belongs, while the last set decides the permissions for the users outside the group. Out of the three characters belonging to each set the first character is for indicating the ‘read’ permission, the second character is for ‘write’ permission and the last is for ‘execute’ permission. For example, a file ’sample’ has long listing as rwxr-x—x, then

1. The owner can read, write as well as execute the file sample.1. The members of the group can read and execute the file but can‘t write to it. A

‘-‘indicates that the permission is denied.1. All others can only execute ‘sample’

These permissions can be encoded numerically. The weights assigned to the three permissions are

Permission Weight

Read (r) 4

Page 13: Linux Important Material

Write (w) 2Execute(x) 1

Thus, when all three permissions are available, the total weight age or value is 4+2+1 = 7, as in the case with the owner of ‘sample’The group permissions of ‘sample’ are r-x, hence the value is 4+0+1 = 5. the permissions for the rest are –x, thus the value assigned is 0+0+1 = 1.Then everybody has all permissions, rwxrwxrwx, they would amount to 777.The existing file permissions can be changed by the owner of the file or by the super user. The way to change the permissions is by using chmod command. If ‘changes the mode’ of the file it is executed on. If we want the owner of ‘sample’ to have all the permissions, group and others none, we say

1.5.1 chmod: changing mode (permissions)

The chmod command is used to set the permissions of one or more files for all three categories of users.It can be used two ways:

1. In a relative manner by specifying the changes to the current permissions.2. In an absolute manner by specifying the final permissions.

Relative manner:

The chmod command only changes the permissions specified in the command line and leaves the other permissions unchanged.

Syn: $chmod [who] [+/-/=] [permissions] <filename>

The who refers to whom the permissions are to be assigned. It may be the user or owner (u), the group (g) or others (o). if none is specified, all are assumed. The ‘+’ refers to add permission, ‘-‘ refers to remove permission and ‘=’ instructs chmod to add the specified permission and take away all others, if present.

Ex: $ chmod a + w sampleIt adds write permissions for all users

Ex: $ chmod go-x sample.In order to take away execute permission from others as well as group.

Ex: $ chmod go+r,go-w pack1This removes write permission for group and others and adds only read permission.

Ex: $ chmod go=r,u=rw file1This command remove all existing permissions and replaces them with read permission for group and others, and read and write permission for owner of the file ‘file1’.

Octol Notation (Absolute manner):The expression used by chmod here is a string of three octal numbers read - 4, write – 2, and execute – 1

Page 14: Linux Important Material

Syn: $chmod permissions <filename>

Ex: $ chmod 700 sample

This way of changing file permissions is referred to as the absolute mode. It assigns read, write, and execute permissions for user and no other permissions to group and others.

Instead of using u/g/o and +/-/= we can straightaway use the weight (read=4, write=2, execute=1) associated with each permission as shown below.Ex: $ chmod 741 file1This would assign the permission rwxr—x—x to ‘file1’.

1.5.2 Changing File Ownership:The chown command transfers ownership of a file to a existing user and it seems that it can optionally change the group as well. The command requires the user-id of the recipient followed by one or more filenames. Changing ownership requires the super user permission, to change to super user type su command$suPassword ***** if entered correct password displays # prompt as shown below#Syn: #chown <newownername> <filename>

Ex: #chown it02 add.shThis change ownership it01 to it02 of add.sh file

1.5.3 Changing group owner:The group owner of a file is the group to which the owner belongs. The chgrp (change group) command changes a files group owner.A user can belong to more than one group, and the one shown in /etc/passwd is the users main group. No super user permission required.

Syn: $chgrp permissions <filename>

Ex:ls –l dept.lst_rw_r__r__ 1 kumar metal 139 jun 8 16:47 dept.lst$chgrp dba dept.lst; ls -l dept.lst_rw_r__r__ 1 kumar dba 139 jun 8 16:47 dept.lstIt changes group from metal to dba using chgrp command#chown sharma:dba dept.lstIt changes ownership to Sharma and group to dba for dept.lst file.

1.5.4 Masking File Permissions:Unix uses the value stored in a variable called umask to decide the default permissions umask stands for user file creation mask, the term mask implying which permissions to mask or hide. The umask value tells UNIX which of the three permissions are to denied rather than granted. The current value of umask can be easily determined by just typing umask.

Syn: $umask <defaultpermissions>

Ex: $umask0022

Page 15: Linux Important Material

Here, the first 0 indicates that what follows is an octal number. The three digits that follow the first zero refer to the permissions to be denied to the owner, group and others. This means that for the owner no permission is denied, whereas for both, the group and others write permission (2) is denied whenever a file is created UNIX assumes that the permissions for this file should be 666. But since umask value is 022, UNIX subtracts this value from the default system wide permissions (666) resulting in a value 644. This is the reason why the permissions turned out to be 644 or rw-r—r—for the file ‘sample’.Similarly, system wide default permissions for a directory are 777. This means that when we create a directory its permissions would be 777-022 i.e 755. Execute permission for a directory has a special significance. If a directory doesn’t have an execute permission we can never enter into it.We can change current umask value to a new value. For example,Ex: $ umask 242Here onwards any new file that is created would have the permissions 424 (666-242) and any directory would have the permissions 535 (777-242).

1.6 The UNIX File System:A file system is a group of files and relevant information regarding them. Whole hard disk may comprise a single file system or it may be portioned to house several file systems. The disk space allotted to a UNIX file system is made up of 'blocks', each of which are typically of 512 bytes as well. The block size depends upon how the file system has been implemented on a particular installation.The cmchk command is used to find the block size.Ex:$ cmchkBSIZE=1024All the blocks belonging to the files system are logically divided into 4 parts. The first block of a file system is called 'Boot block' which is followed by 'super block'. 'Inode Table' & 'Data blocks'.

1.6.1 The Boot Block:This represents the beginning of the file system. It contains a program called 'bootstrap loader'. This program is executed when we 'boot' the host machine. Although only one boot block is needed to start up the system, all file systems contain one (possibly empty) boot block.

1.6.2 The Super Block:The super block describes the state of the file system how large it is, how many maximum files it can accommodate, how many more files can be created etc.

1.6.3 The Inode Table:All entities in UNIX are treated as files. The information related to all these files (not the contents) is stored in an Inode Table on the disk. For each file, there is an Inode entry in the table. Batch entry is made up of 64 bytes and contains the relevant details for that file. These details are:

1. Owner of the file2. Group to which the owner belongs3. Type of file4. File access permissions5. Date and time of last access6. Date and time of last modification

Page 16: Linux Important Material

7. Number of links to the file8. Size of the file9. Addresses of blocks where the file is physically present

1.6.4 Data Block:These contain the actual file contents. All allocated block can belong to only one file in the file system. This block cannot be used for storing any other file’s contents unless the file to which it originally belonged is deleted.

1.7 How Does UNIX Access Files:Internally a file is identified by UNIX by a unique ‘Inode number’ associated with it. We can obtain the inode number associated with a file by using the command ls –i.

Ex: $ls –i sample1sample1 12234Here 12234 is the inode number. The inode number is an index into the inode table where the information about the file is stored. For example, amongst several slots present in the inode table slot number 12234 contains information about the file sample1. Using this inode number the information about sample1 is accessed from the inode table. The contents of sample1 file are read from the disk addresses mentioned in the inode entry of sample1 and displayed on the screen.

1.8 Storage Of Files:Each inode entry in the inode table consists of 13 addresses each, which specify completely where the contents of the file are stored on the disk. These addresses may be numbered 0 through 12 of these, the first ten addresses 0-9 point to 1KB blocks on disk. For example, a file of size 3KB may have its entries as shown in Fig. The address 4970 signifies where the first 1KBs of the file are stored. The next 1KB chunk is at 5231, and the next at 3401.

4970OwnersGroupsFile typePermissionsAccess timeModification timeInode mode timeFile size0 49701 52312 34013 76544 86755 98776 76667 74418 76659 877110 7777

Page 17: Linux Important Material

11 888812 9999

5231

3401

Fig1.2:These addresses may be scattered throughout the disk, as files are stored in chunks whenever empty blocks of disk are available. This is specially the case with large files, for which a very big chunk may be impossible. Thus, the addresses 0-9 can handle a file of a maximum size of 10KB. For files larger than this, Unix has a very interesting way of indicating their location.

OwnersGroupsFile typePermissionsAccess timeModification timeInode mode timeFile size0 49701 52312 34013 76544 86755 98776 76667 7441

Page 18: Linux Important Material

8 76659 877110 777711 888812 9999

Page 19: Linux Important Material

Fig1.3:

As can be seen from the Fig1.3, the 10th entry also contains an address of a 1KB block. This block doesn’t contain the file contents. Instead, it consists of 256 ten-byte slots, which can store 256 more addresses. Each of these 256 addresses can point to 1KB block on disk. Thus, for a file which occupies 12 blocks on the disk, the first 10 addresses would be found in the inode entry for this file, whereas address of 11th and 12th block would be present in a 1KB block whose address in turn is stored as the 11th address in the inode entry.Thus, the maximum file size that can be addressed using the 10th address entry is 256KB. This is called single Indirection.For a still larger file, Double Indirection is used. The 12th address in the inode entry points to a block of 256 addresses, each of which in turn points to another set of 256 addresses. These are the addresses of 1KB chunks, making the maximum file size accessible by Double Indirection equal to 256*256KB, which is 64MB.For an even larger file Unix uses Triple Indirection. This way, the last address in the inode entry yields an massive 256*256*256KB that is 16GB.That means, the maximum file size Unix provides for is the sum of sizes accessible by the 13 addresses that occur in the inode entry. Together, they yield 10KB+256KB+64MB+16GB, which is more than sufficient for all practical purposes

1.9 Disk Related Commands:

Disk Utilities: df, du, find, ulimit, mount, umount.

1.9.1 Reporting free space:

If we want to see has much of the disk is being used and what part of it lies free, Unix has for us a command called df (for disk free). This command reports the free as well as the used disk space for all the file systems installed on one machine.

Syn: $df/(/dev/root): 12970 blocks 27587 i-nodesWe have an our machine only one file system installed, the root file system or simply /dev/root. df reports the number of free disk blocks and free inodes for this file system.

If we want a more detailed information about disk wage we should say,Ex: $df –ivtmount dir file system block used free %used i-used i-free %i-used

/ /dev/root 282098 269146 12952 95% 7410 27854 21%

Now, available blocks and inodes are reported numerically as well as percentages of total available blocks and inodes. This possibly gives a better idea of how much disk space is free.

df counts blocks in sizes of 512 bytes irrespective of the actual block size a s reported by

Page 20: Linux Important Material

the cmchk command.

1.9.2 Estimate file space usage: df reports the disk space available in the file system as a whole where as du reports the disk space used by specified files and directories. For example,Syn: $du

226 ./backup418 ./fa/backup1182 ./fa4 ./check16 ./dbf1662

Here du is reporting the number of blocks used by the current directory (denoted by ‘.’) and these used by sub-directories within the directory.If we specify a directory then du descends down this directory locating any sub-directories lying in it and reports the blocks used by the directory and the sub-directories.Ex: $du /dev

2 /dev/string4 /dev/rdsk2 /dev/mouse20 /dev

Thus, the no.of blocks occupied by each sub-directory within /dev, as well as those occupied by /dev are displayed.If we want only the blocks occupied by the directory and not those occupied by the sub-directories within in it we can say.

Ex: $du –s /dev20 /dev

du is often used to single out directories that occupy large amounts of diskspace unused and redundant files and directories can then be eliminated from them, there by freeing the valuable disk space.

1.9.3 ulimit: It stands for ‘user limit’ and contains a value, which signifies the largest file that can be created by the user in the file system.

Syn: $ulimit //the current value of ulimit variable.2097152

This implies that the user can’t create a file whose size is bigger than 2097152 bytes or 2048KB. If we try to create a file bigger than this size, its size would be curtailed to 2048KB and the program creating this file could be aborted.User can reduce this value by saying.

Ex: $ulimit 1

Here onwards no file can be created whose size is bigger than 512 bytes. Once reduced this value remains effective till the user doesn’t logout. Thus this change will be effective only for the current session and the system will return to its default value when you logout.An ordinary user can only reduce the ulimit value and is never permitted to increase it. A super user is an exception to the rule and can increase or decrease this value.

Page 21: Linux Important Material

1.9.4 find:The find command is recursively examines a directory tree to look for file matching some criteria, and then takes some action on the selected files or locating files.

Syn: $find pathlist selection_criteria action

Ex: find / -name a.out –printIt will find files from root directory as name of file name a.out after finding file it will display file content. Otherwise it will display no file found.

selection_criteria:!-name – Negation (inverse)-inum n – identification number (inode number)-perm nnn – permissions -links n – number of links

Ex: find / -perm 600 –printIt search for files from root directory with permissions for user read, write and no permissions to group and others.

1.9.5 mount/unmount:A super user may extend the file system by using the ‘mount’ utility.Syntax : mount –o options [device name directory]

unmount device name The file system built on the floppy disk can be linked into the existing file system on the hard disk using the ‘mount’ command. Once mounted we can create files and directories in the new file system and treat it as a normal directory existing in a file system.When we use mounting a file system, we are simply attaching it’s root directory to a particular point in the existing file system. This point of attachment is called the ‘mount point’ for that file system. The mount point of any new file system, must always be specified as a path from the root directory of the existing file system. The default one is /mnt.# /etc/mount /dev/fdo/mnt/mnt is an empty directory in the root (/) directory, with root (super user) as it’s owner. All users have the permission to access this directory.Option ‘rw’ mounts a file system for read\write Option ‘ro’ mounts a file system for read onlyThe ‘umount’ utility unmounts a previously mounted file system.$mount #lists the currently mounted devices./dev/dsk1 on/(rw)$ls /user #/user is currently empty.$mount /dev/dsk2 /usr #mount the /dev/dsk2 device$mount #list the currently mounted device./dev/dsk1 on/(rw)/dev/dsk2 on/usr (rw)$ls /usr #list the contents of the mounted device Bin/…………$umount /dev/dsk2 #unmount the device$mount #list the currently mounted device

/dev/dsk on/(rw)$ls /usr #/usr is empty againThe files were no longer accessible.

Page 22: Linux Important Material

1.10 Process Related Commands

Process Utilities: ps, who, w, finger.

1.10.1 ps (viewing the process):The ps command used to view all processes that you are responsible for created.

$psPID TTY TIME CMD364 console 0.00 ksh shell running all the time.This process has a unique number 364 and when you logout, this process is killed. When you run several programs, there will be multiple lines in the ps output.

1.10.2 who

who command displays details about the users who logged into system.

NAME LINE TIME COMMENTSito1 pts/2 2012-10-20 13:37 192.168.0.164

The 1st column shows the user namesThe 2nd column shows the device names of their respective terminalThe 3rd column shows the date and time of logging in.The last column shows the machine name from where the user logged in.

1.10.3 W:A command that reports on system usage, logged on users and what they are doing and the load average. The load average is the average number of processes in the last 1, 5 and 15 minutes.Syntax : w [options] usernameNormally, w reports on all users, but if you specify username as a comma separated list of names, the report is restricted to information on just these names.

Ex: $ w

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATUser1 tty1 192.168.0.3 1:24 6.00s 00.12s 00.01s man w

Jcpu – The jcpu time is the time consumed by all processes at that terminal. It include currently running background jobs.

Pcpu – Time is used by current process named in what field.

OPTION DESCRIPTION-h Turns off the report header line.-l Creates a long listing, including the following columns:

Page 23: Linux Important Material

User TTY Login Idle JCPU PCPU what-q Creates a quick listing that consists of user, TTY, Idle and What-t Displays only the heading line; equivalent to the o/p from uptime.

1.10.3 finger:

The finger command that tells you which users are connected and which, if any, can receive messages. It is a more informative tool.

$finger

LOGIN NAME TTY IDLE WHEN AND LOGIN TIME WHEREhenry henry james *1 16 Dec, 27 Fri 8:56 mercury.heavens

1st column shows user id.2nd column shows users full name.3rd column shows terminal, preceded by an asterisk ( i.e the terminal doesn’t have write permission).4th column shows the idle time by the user.The last column shows the name of the machine from where the user logged in.

.plan and .project files:

These files makes finger a useful communication tool. Its often necessary to leave behind your schedule and other important information for others to see, especially if you are going on vacation. When someone runs finger with userid as argument the contents of these two files are displayed.The .plan file can display multiple lines The .project file can display single line

1.11 Text Related Commands Text Processing Utilities: head, tail, sort, nl, uniq, grep, egrep, fgrep, cut, paste, join, more, pg, wc, tee, comm, cmp, diff, tr, ln.

1.11.1 head: Displaying the beginning of a file.Ex: $head -10 newfileThe first 10 lines of newfile are displayed.

1.11.2 tail: Displaying the end of a file.The tail command is the counter part of the head command, displays the end of the file. By default head and tail commands display first and last 10 lines of a file respectively.Ex: $tail -3 empIt displays the last 3 lines in emp file.The disadvantage of head and tail is that they cannot display a range of lines.

1.11.3 sort: This command can be used for sorting the contents of a file. Apart form sorting files, sort can merge multiple sorted files and store the result in the specified output file. While sorting the sort command bases its comparisons on the first character in each line in the file. If the first character of two lines is same then the second character in each line is compared and so on. It sorts the spaces the tabs first, then the punctuation marks followed by numbers, uppercase letters and lowercase letters in that order.

Page 24: Linux Important Material

This simplest form of sort command would be$sort file1This would sort the contents of file1 and display the sorted output on the screen.If we want to sort the contents of several files at one short as in$sort file1 file2 file3Instead of displaying the sorted output on the screen we can store it in a file by saying,$sort –oresult file1 file2 file3The above command sorts the three files file1, file2 and file3 and saves the result in a file called result.And if there are repeated lines in each of these files and we want that such lines should occur only once in the output we can ensure that using –u option which output only unique lines.

$sort –u –oresult file1 file2 file3If the files have already been sorted and we just want to merge them we can use:$sort –m file1 file2

If we want to combine the contents of a file with the input from the keyboard and then carry out the sorting. This can be achieved by saying:

$sort –file1Where ‘-‘ stands for the standard input i.e keyboard.We can even sort only the input from standard input by just saying

$sortHere, no file name is specified, so it considers input is from standard input device.

Assume that a file student has four fields, for roll number, names of the students, their marks and their grades. These fields would be numbered 0,1,2 and 3

$sort –r +1 -2 students

The +1 indicates that the sort key begins at the second field (i.e names field) and the -2 indicates that it ends before the third field (i.e up to marks field). This yields names of the students as the sort key.The –r switch indicates a reverse sort. So, the records arranged in reverse alphabetical order of names could be displayed on the screen.If we want to sort the same file according to marks we must use –n option which specifies that the sorting is to be done on a numeric field.The following table shows all the options that can be used with sort.

Option Meaning

-b Ignores leading spaces and tabs-c Checks if files are already sorted. If they are, sort does nothing.-d Sorts in dictionary order (ignores punctuation)-f Ignores case-m Merges files that have already been sorted.-n Sorts in numerical order-o file Stores output in file. The default is to send output to standard output.-r Reverse sort-tc Separates field with character (default is tab)

Page 25: Linux Important Material

-u Unique output if merge creates identical lines, uses only the first.+n[-m] Skips ‘n’ fields before sorting and then sorts through field ‘m’.

1.11.4 nl: line numbering:This is for numbering the lines.$nl file11 chairman2 special officer3 principal4 head5 lecturernl uses the tab as the delimiter. The –w(width) option to specify the width of the number format, and –s(separator) to specify the separator.$nl –w2 –s “:” file11: chairman2: special officer3: principal4: head5: lecturerThe –nrz option right- justifies the number, with leading zeros to fill the gaps:$nl –w2 –s “:” file101 : chairman02 : special officer03 : principal04 : head05 : leturerThere are two other options –i(increment) option and the –v k option that sets the initial value to k.

1.11.5 uniq: locating repeated lines:$uniq mp1Uniq simply fetches one copy of each record and writes it to the standard output.Like sort, uniq also accepts the output filename as an argument. Uniq uses only one filename at a time. If three filenames are given, then uniq simply processes the first file and overwrites the second with it’s output.If uniq is to merely select uniq lines, it is preferable to use sort –u that does this job with a single command. Uniq has a couple of useful options; they can be used to make simple database queries. The –u(unique) option selects only the non-repeated lines. The –d(duplicate) option selects only one copy of the repeated lines. And –c (count) option displays the frequency of occurrence of all lines, along with the lines.When uniq is used with other filters like cut and sort acts as a database tool to query the database ie to retrieve information from database.

1.11.6 grep: ‘grep’ is an acronym for “global search a regular expression and print it”. The command searches the specified input fully (globally) for a match with the specified pattern and displays it. While forming the patterns to be searched we can use shell meta characters, or regular expressions like ‘,’ ‘?’ ‘[]’ ‘!’.

1) $grep picture newfileThis would search the word ‘picture’ in the file newfile and if found the lines containing it would be displayed on screen. We can use grep to search a pattern in several files.

For example:

Page 26: Linux Important Material

$grep picture newfile file1

Here, the word ‘picture’ would be searched in both the files, newfile and file1 and if found, the lines containing it would be displayed along with the name of the file where it occurred.For a search pattern comprising more than a single word, single quotes can be used to enclose the same as in.

$grep ‘the picture taken’ –i –n newfile file1

The above command searches for the pattern enclosed within ‘ ‘, without heeding the case (-I makes it case insensitive). The ‘-n’ option also causes the number of lines in which the pattern was found to be printed by the side of each line.

$grep [Rr]ain file2

Here grep would search for all occurrences of ‘Rain’ as well as ‘rain’ in file2 and display the lines which contain one of these words.

$grep b??k file2

This command would display all four-letter words whose first letter is a ‘b’ and last letter, a ‘k’. The two ‘?’ symbols represent one character each.

$grep –v a* file2

All those lines that don’t contain words starting with ‘a’ are displayed.‘^[abc]’ would help to search only those lines which begin with ‘a’, ‘b’ or ‘c’.

‘[s-z]$’ would help to search those lines which end with any character between ‘s’ to ‘z’.The various options available with ‘grep’ are shown bellow.

OPTION MEANING

-c Returns only the number of matches, without quoting the text.-i Ignores case while searching-l Returns only filenames containing a match, without quoting the text.-n Returns line number of matched text, as well as the text itself-s suppresses error messages-v Returns lines that don’t match the text

1.11.7 egrep: Extending grep:The egrep command, extends grep’s pattern-capabilities. It offers all the options of grep, but it’s most useful feature is the facility to specify more than one pattern for search. Each pattern is separated from the other by a |(pipe).Expressions and their significance:

ch+ : Matches one or more occurrences of character ch. ch? : Matches zero or one occurrence of character ch. exp1|exp2 : Matches expression exp1 or exp2. (x1|x2)x3 : Matches expression x1x3 or x2x3.

The above are not used with grep.

Page 27: Linux Important Material

For example, b+ matches b, bb, bbb etc while b? matches either nothing or a single b.$egrep –i ‘agg?[ar]+wal’ emp‘-i’ indicates case insensitive.Searching for multiple patterns:$egrep ‘Renigunta|Panjagutta’ emp‘|’ is separator of the patterns.$egrep ‘(sen|das)gupta’ emp$cat file1admin/accounts/sales$egrep –f file1 file2This command searches for patterns for file1 in file2.1.11.8 fgrep : Multiple string searching:fgrep like egrep accepts multiple patterns, both from the command line and a file, but unlike grep and egrep, doesnot accept regular expressions. So, if the pattern to search for is a simple string, or a group of them, fgrep is recommended. It is faster than grep and egrep.Alternative patterns in fgrep are specified by separating one pattern from another by the new line character.Ex : $cat ex1SalesPersonnelAdmin$fgrep –f ex1 ex2

1.11.9 cut: Like sort, cut is also a filter. It cuts or picks up a given number of character or fields from the specified file cut is slitting a file vertically cut identifies both columns and fields.

Ex: a) $cut –c 5-10 16-20 file1

Displays the columns from 5 to 10 and 16 to 20 of file1

b) $cut –c -3 5-10 16-20 30- file1

The expression 30- indicates column number 30 to the end of the line. Similarly, -3 is the same as 1-3.

Files often don’t contain fixed length records, in which case, it is better to cut fields rather than columns. Two options need to be used here –d (delimiter) for the field delimiter and –f (field) for specifying the field list.

Ex: $cut –f 2,7 empinfo

Here, 2nd and 7th field of empinfo are displayed.

If we are to view fields 2 through 7 we can say

$cut –f 2-7 empinfo

The cut command assumes that the fields are separated by tab character. If the fields are delimited by some character other than the default tab character, cut supports an option –d which allows us to set the delimiter. The file empinfo may have the information for each employee stored in the following format:

Page 28: Linux Important Material

name: age: address: city: pin: division

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 division fields would now be.$cut –f 2,7 –d “:” empinfo

1.11.10 paste: pasting files: It is a special type of ‘concatenation’ in that it pastes files vertically rather than horizontally.$ paste file1 file2Then the fields of file2 are concatenated to fields of file1. By default the delimiter is tab. The delimiter of our choice is set by ‘-d’(delimiter) option.$cut –d “|” –f 1,4 –shortlist | paste –d “|” –cutlist

1.11.11 Join: A command that extracts common lines from two sorted files.Syntax : join [options] filename1 filename2One line of o/p is created for each line in the two files that match, based on option.This command joins the common lines found in filename1 and filename2; if filename1 is not specified, join reads from the standard input.

OPTION DESCRIPTION-a n Lists un matched lines from file ‘n’ or from both if ‘n’ is

omitted.-e string Replaces any empty field with ‘string’.-jn n Joins the two files on the mth field of file n; if ‘n’ is not

specified, the files are joined on the mth field of both files.-on.m Specifies the fields to o/p from each file for each line for each

line with matching join fields, where ‘n’ specifies the file number and ‘m’ specifies the field number.

-t char Uses character ‘char’ as a field separator for i/p and o/p.

1.11.12 pg and more: Unix provides two commands, which offer more flexibility in viewing files. These are pg and more. Each of them is used to display page by page with following options:Set the number of lines to be displayed per page.Ability to move either forward or backward in a file just at the touch of a key.Skip pages while viewing the file page by page.Search the file for a pattern in forward or backward direction.

Ex: $pg +10 -15 –p “page no.%d” –s myfileThis command starts displaying the contents of myfile,15 lines at a time from 10th line onwards. At the end of each displayed page, a prompt comes which displays the page number on view.This prompt overrides the default ‘:’ prompt of the pg command. The ‘-s’ option ensures that the prompt is displayed in reverse video.

$more +10 -15 –s –d myfile yourfileThis is also similar to pg command. The difference being it could display contents of two files myfile and yourfile. Also, the ‘-s’ option could squeeze multiple blank lines in a file to a

Page 29: Linux Important Material

single blank line. The ‘-d’ option changes the normal --more-- prompt displayed at the end of each page to a more explanatory ‘[hit space to continue, delete to abort]’.The most obvious difference between pg and more is , pg permits us to set the prompt whereas more doesn’t. Also, while using more at the end of each page a number appears which indicates how much percentage of file have we viewed so far.

1.11.13 wc: It counts the number of lines, words and characters in the specified file or files. It comes with the options -l, -w and –c which allow the user to obtain the number of lines, words or characters individually or in any desired combination.

Ex: $wc –lc file1 file2 file1 20 571 file2 30 804

The file file1 constitutes 20 lines and 571 characters similarly for the file2.The wc command 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. On terminating input (using ctrl d), the appropriate counts are displayed for the input.

Note: Filters are: cat, pg, more, head, tail, grep, sort, wc, nl, uniq, cut, paste etc.

1.11.14 I/O Redirection and Piping, tee:Unless otherwise instructed Unix commands\programs get their input from standard input device and send their output to the standard output device. Any error messages if they occur are sent to the standard error device.Thus, if a command is described as reading from the standard input and writing to the standard output, that means it takes input from keyboard and sends output or error messages to the screen. The three streams, ie the standard input, standard output and the standard error are denoted by the numbers 0,1,2 respectively.

Stream Device ValueStandard i/p Keyboard 0Standard o/p Terminal screen 1Standard error Terminal screen 2

It is useful to redirect the i/p or o/p to a file or a printer. The symbol > implies redirection of output and the symbol < implies redirection of input. The symbol > sends the o/p of a command to a file or a device, such as a printer. The symbol < takes the i/p needed for a command from a file rather than from the keyboard. The symbol >> adds o/p from a command to the end of a file without deleting the information in the file.

1) $cat file1>file2The redirection operator declares file2 to the standard output. Thus the output of cat which is normally sent to the screen is now sent to file2 and not to the screen. If file2 doesnot exist, it is created. If it already exists, it’ s contents are overwritten with file1 content.

2) $catInput is from keyboard.$cat< newfileChennai is the capital of Tamilnadu.Then$cat newfile

Page 30: Linux Important Material

Chennai is the capital of Tamilnadu.$cat newfile –Hyderabad is the capital of Andhra pradesh.ctrl dChennai is the capital of Tamilnadu.Hyderabad is the capital of Andhra Pradesh.‘-’ is the keyboard input.

3) $cat< file1 >newfileThe first part of redirection, < file1, indicates that input is to be taken from the file ‘file1’ and the second part of redirection, >newfile establishes that o/p is to be routed to the file newfile.

The order in which the two indirection operators are used doesnot matter. The same thing is represented as follows:$cat > newfile < file1Another redirection operator which is popularly used is the append operator,>>. It is similar to >, except if the target file already exists, the new output is appended to it’s end.For example,$who >> logfileThis command appends the current list of users who have logged into the end of the file logfile.Operator Action> file Make ‘file’ the standard output< file Make ‘file’ the standard input>>file Make ‘file’ the standard output, appending to itif it already exists.<< word Take shell input up to the first line containing Word or up to end of file.n >file Make file the output for file descriptor n.1>&2 Redirect standard output to standard error.cmd1|cmd2 Make standard output of cmd1, the standardinput of cmd2.

Piping:Through redirection facility commands can be connected to files. The Unix piping facility connects commands to other commands. This facility is of utmost importance in combining Unix commands and operations. It can be really useful to redirect the output of one program and that it becomes the input of another program, there by joining the two programs. To send the output one command as input for another, the two commands must be joined using a pipe (|) character.It is possible to join commands using a pipe since many Unix commands accept input from the standard input and send output to the standard output. Incidentally such commands are known as filters.Thus a filter is a program, which can receive a flow of data from standard input, process (or filter) it, and send the result to the standard output.

Ex: $ls | wc -lHere, the output of ls becomes the input to the wc which promptly counts the number of lines it receives as input and displays this count on the screen. Instead of displaying this count, we can store the result in a file as shown below:(with output redirection)$ls | wc –l >countfileHere, both piping and redirection are used.Ex: $who | sortHere, instead of displaying the output of who on the screen it is piped to sort. Sort sorts whatever it receives as input(output of who, in this case) and displays the sorted

Page 31: Linux Important Material

output(sorted according to first character in each line) on the screen. Because sort being a filter unless otherwise mentioned it sends the output to the screen. We can redirect sorted output to a file by$ who | sort >sorted listEx: $who | sort –logfile >newfileThe output from who becomes the standard input to sort. Meanwhile, sort opens the file logfile. The contents of this file are sorted together with the output of who(represented by the hyphen) and the sorted output is redirected to the file newfile.Redirection routes the output to files while pipes route the output to other programs(commands). Unix offers tee command to achieve both. It reads the standard input and sends it on to the standard output.Ex: who | tee logfile | sortHere, the output of who becomes the standard input of tee. tee now sends one copy of the input to sort through one pipeline, whereas the other copy is stored in a file called logfile.$who | tee file1 file2 | sortHere, the output of who is stored in file1 and file2 and one copy is sent to sort.If we want to store the output of who in file1 and file2, display the same output on the screen and store the sorted output in file3, then we write$who | tee file1 file2 /dev/tty3a | sort >file3If we wish to append the output of tee to a file, -a option is used with it as shown below:$cat file1 file2 | tee –a file3 | moreIn this pipeline the output of cat(contents of the files file1 and file2) are appended to the existing contents of file3. Another copy of output of cat is sent to more for displaying on the screen.So far we have redirected only the standard input and the standard output. We can however also redirect the standard error. The following example shows this:$cat myfile >newfile 2 >errorfileIf the file myfile exists, then the contents that file would be copied to newfile. However if it doesnot exist, then an error message would be produced. However instead of displaying the error message on the screen, it would be redirected to a file errorfile. The > symbol redirects the standard output whereas 2 > redirects the standard error. While redirecting the standard output instead of > we we may also well use 1 >.

1.11.15 comm:finding what is common:comm requires two sorted files, and compares each line of the first file with it’s corresponding line in the second.Ex : $cat file1 $cat file2 $cat file3Raghu.V Varun.T Raghu.VShiva.P Tharun.Q Shiva.PRoja.R Raghu.V Gopi.NGopi.S Roja.R Sekhar.S$comm file1 file3Raghu.VShiva.PGopi.NRoja.RGopi.S Sekhar.Scomm can also produce selective output, using options -1, -2 or -3. To drop a particular column, simply use it’s column number with the ‘-‘ sign. We can also combine options and display only those lines that are common.$comm -3 file1 file2 $comm -12 file1 file2

1.11.16 cmp: comparing two files:

Page 32: Linux Important Material

Whether two files are identical or not is determined through cmp, diff and comm. commands.$cmp chap1 chap2chap1 chap2 differ :char 9,line1The two files are compared byte by byte and the location of the first mismatch(in the 9th character of first line) is echoed to the screen. cmp, when invoked without options, doesnot bother about possible subsequent mismatches.The –l(list) option gives a detailed list of the byte number and the differing bytes in octal for each character that differs in both files:$cmp –l file1 file317 162 147....30 12 56If the two files are identical, cmp displays no message, but simply returns the $ prompt.

1.11.17 diff: converting one file to other:diff is used to display file differences. It tells us which lines in one file have to be changed to make the two files identical.$diff file1 file33,4c3,4 #change line 3 of file1< roja.r #}replace these< gopi.s #}lines- - - - - #with> gopi.n #}these> sekhar.s #}two linesdiff uses certain special symbols and instructions to indicate the changes that are required to make two files identical.Maintaining several versions of a file: diff has an extremely useful option for the system administrator the –e option. If we have ten versions of a file differing only nominally, we now need to keep only one in full. For the others, we can keep only the differences, which help us conserve disk space.

1.11.18 tr (translate):The tr (translate) command is a simple filter designed to replace one or more characters in given files with one or more characters. The syntax is:tr [-cds] [in-string] [out-string]The ‘-s’ option substitutes all of the specified characters with another specified character and displays the results.Ex: $cat items To substitute ‘z’ s for al ‘s’, Shoes $tr –Ss Z < items Socks Zhoes pants Zocks PantsThe ‘-d’ option deletes characters specified in the given file and displays the results.$tr –d s < itemshoeockpantThe ‘-c’ option tells tr not to match the specified characters. It is used with the ‘-s’ and ‘-d’ options to modify that those options operate.Ex: $tr –cd s < items Ss Here, it will delete all characters except ‘s’.

Page 33: Linux Important Material

Ss sWithout any options, tr does a straight substitution.$tr o ‘ ’ < itemsSh es Here, all o’s are replaced with spaces.S cksPantsCharacters can be replaced with ; & ( ) | ^ < > new line and tab, but these must be specified in single quotes.$tr ‘[a-z]’ ‘[A-Z]’ < itemsShoesSocksPants

1.11.19 ln :

The ln command is used to linking two files (directories). There are two types of links

Hard links Soft links

Hard Links:

A file is linked with the ln command, which takes two file names as arguments.

$ ln emp employee $ls –li emp employee

INODE PERMISSIONS NO.OFLINKS USERNAME GROUPNAME SIZE DATE&TIME FILENAME29518 _rwxr_wr_w 2 kumar metal 915 may 4:9:58 emp.lst29518 _rwxr_wr_w 2 kumar metal 915 may 4:9:58 employee.lst

The link count which is normally one for unlinked files, above shows as two for two files linked. The number of links increase by one if one more file linked.

The rm / ulink command removes a file by removing its directory entry. The link count is decreased by one.

When do you require to link files?

1. Links provide some protection against accidental deletion especially when they exist in different directories. By creating a link, you create a “backup”. So, even if you inadvertently delete one link, another will still be available.

2. Because of links, we don’t need to maintain two programs as two separate disk files if there is very little difference between them.

Two limitations in hard links: Can’t link two files in two different file systems. Can’t link a directory even within the same file system.

Symbolic Links:

Page 34: Linux Important Material

To overcome the limitations of hard links use symbolic links. A symbolic doesn’t have the files contents but simply provides the pathname of the file that actually has the contents.

$ ln –s raju raju1

29518 _rwxr_wr_w 1 kumar metal 915 may 4:9:58 raju.lst29519 _rwxr_wr_w 1 kumar metal 915 may 4:9:58 raju -> raju1.lst

1.11.20 awk:

‘awk’ got it’s name from the combined first letters of it’s author’s surnames. Aho, Weinberger and Kernighan. It borrows its control structures and expression syntax from the C language.The awk programming language is also known as “pattern scanning and processing language”.Syntax: awk –FC [-f filename] program {variable=value}*{filename}*‘awk’ is a programmable text-processing utility that scans the lines of it’s i/p and performs actions on every line that matches a particular criteria. An ‘awk’ program may be included on the command line, in which case it should be surrounded by single quotes; alternatively, it may be stored in a file and specified using the ‘-f’ option. The initial values of variables may be specified on the command line. The default separators are tabs and spaces. To override the default, use the ‘-F’ option followed by the new field separator. If no filenames are specified ‘awk’ reads from standard input.An ‘awk’ program is a list of one or more commands of the form [condition] [\ {action \}]Where condition is either,

the special token BEGIN or END or an expression involving any combination of logical operators, relational operators and

regular expressions and ‘action’ is a set of one or more of the following kinds of C-like statements, terminated by semicolons:

if (condition) statement [else statement] while (conditional) statement for (expression; conditional; expression) statement break continue variable=expression print [list of expressions] [> expression] printf format [list of expressions] [> expression] next (skips the remaining pattern on the current line of the i/p) exit (skips the root of the current line) {list of statements}

‘action’ is performed on every line that matches ‘condition’ . If condition is missing, action is performed on every line. If action is missing, then all matching lines are simply sent to standard o/p. The statements in an ‘awk’ program may be indented and formatted using spaces, tabs and new lines.Accessing individual fields:The first field of the current line may be accessed by $1, the second by $2 and so forth. $0 stands for the entire line. The built-in variable ‘NF’ is equal to the number of fields in the current line.Ex: $cat float #look at the original file(float is a text file) This is UNIX command Programming commands are available in UNIX $awk ‘{print NF,$0}’ float #execute the commandOutput:

Page 35: Linux Important Material

4 This is UNIX command6 Programming commands are available in UNIXBegin and end:The special condition BEGIN is triggered before the first line is read, and the special condition END is triggered after the last line has been read. When expressions are listed in a print statement, no space is placed between them, and a new line is printed by default. The built-in variable FILENAME is equal to the name of the i/p file.$cat awk2 #look at the awk scriptBEGIN{print “start of file:”, FILENAME}{print $1 $3 $NF} #print 1st, 3rd and last fieldsEND{print “End of file”}

$awk –f awk2 float #execute the scriptOutput:Start of file:floatThis is unix commandProgramming commands are available in unixEnd of fileOperators:When commas are placed between the expressions in a ‘print’ statement, a space is printed. All of the usual C operators are available in ‘awk’. The built-in variable ‘NR’ contains the line number of the current line.$cat awk3 #look at the awk script NR>1 && NR<4 {print NR,$1,$3,$NF}$awk –f awk3 file1 #execute the scriptOutput:2 sree engg3 dept information Where,$cat file1There are 60 students in IT-ASree vidyanikethan Engg College A.RangampetDept of Information TechnologyAdvanced Unix programmingVariables:‘awk’ supports user-defined variables. There is no need to declare a variable. A variable’s initial value is a null string or zero, depending on how we use the variable.The following program counts the number of lines and words in a file as it echoed the lines to the standard output.$cat awk4 #look at the awk scriptBEGIN {print “Scanning file”}{

printf “line %d: %s \n”,NR,$0; linecount++; wordcount+=NF;}END {printf “lines=%d,words=%d\n”,linecount,wordcount}$awk –f awk4 file1 #execute the scriptOutput: Scanning fileline1 : There are 60 students in IT-Aline2 : Sree Vidyanikethan Engg College A.Rangampetline3 : Dept of Information Technologyline4 : Advanced Unix programminglines=4 words=18

Page 36: Linux Important Material

control structures:‘awk’ supports most of the standard ‘C’ control structures.Ex:$cat awk5 #look at the awk script{

for(i=NF ; i >=1 ; i--) printf “%s”,$i printf “\n”;}$awk –f awk5 file1 #execute the scriptIT-A in students 60 are thereA.Rangampet College Engg Vidyanikethan SreeTechnology Information of Deptprogramming Unix AdvancedExtended regular expressions:Regular expressions must be placed between slashes(/).The following example displays all of the lines that contain a “t” followed by an ‘e’, with any number of characters in between.$cat awk6 #look at the script /t. *e/ {print $0}$awk –f awk6 file1 #execute the scriptOutput:There are 60 students in IT-ASree Vidyanikethan Engg College A.RangampetDept of Information Technology

Condition ranges:A condition range may be two expressions separated by a comma. In this case ‘awk’ performs ‘action’ on every line from the first line that matches the first condition to the next line that satisfies the second condition.Ex :$cat awk7 #look at the awk script/Sree/ , /Dept/ {print $0}$awk –f awk7 file1 #execute the scriptOutput:Sree Vidyanikethan Engg College A.RangampetDept of Information TechnologyField separators:If the field separators are not the spaces, use ‘-F’ option to specify the separator characterEx :Assume that fields are separated by colons:$cat awk3 #look at the awk scriptNR > 1 && NR < 4 {print $1,$3,$NF}$cat float2 #look at the input fileWish: I : was : floating : in : blue : across : the : skyMy : imagination : is : strongAnd : I : often : visit : the : daysWhen : everything : seemed : so : clear Now : I : wonder : that : I’m doing : here : at : all$awk –F : -f awk3 float2 #execute the scriptOutput:My is strong And often daysBuilt-in functions:

Page 37: Linux Important Material

‘awk’ supports several built-in functions, including exp(), log(), sqrt(), int() and substr(). The substr(str,x,y) function returns the substring of ‘str’ from the xth character and extending y characters.

1.12 vivi (short for visual editor) displays a window of text. The window shows a certain portion of the edit buffer, and we can position the window to view any part of the buffer.Changes to the text take place at the cursor position. The editor allows us to move the cursor around in the edit buffer window easily. For instance, the cursor may be moved forward or backward a character, a word, a sentence or a paragraph or it may be moved vertically, up, or down the page.The visual editor commands have a uniform and consistent structure that makes the commands easier to learn. Many operations- for example, an operation in which yoy would change or delete text- may be combined in a natural way with the cursor motion directives to form a combined command.There are three editors: ed, ex and vi.The ed program is basically a line editor, which means that ed assigns line numbers to the lines in the file; every time you do something, you must tell ed which line or lines to do it to.An improved version of ed is ex, understands all the commands of ed.Compared to ed or ex the vi text editor, is better. It is a screen editor rather than line editor; it shows you as much of the file as it can fit on the screen.1.12.1 Disadvantages of vi:

1. There is no self-explanatory error messages.2. There is no online help for the programmer.3. There are three modes in which the editor works. Under each mode the same key

press creates a different effects. Hence meaning of several keys and their effects in each mode have to be memorized.

4. vi is case sensitive.Modes of operation:The vi program has three modes of operation :

1. Command mode: In this mode all the keys pressed by the user are interpreted to be editor commands. For example, if you hit a ‘h’ the cursor is moved one position to the left. In command mode the keys that are hit are not displayed on the screen.

2. Insert mode: This mode permits insertion of new text, editing of existing text or replacement of existing text. 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.

3. ex command mode: This mode permits us to give commands at the command line. The bottom line of the vi screen is called the command line. Vi uses the command line to display messages and commands. All commands entered in the ex command mode are displayed in the command line. This mode is so called because commands given in this mode are compatible with commands of the ex editor.

1.12.2 Invoking vi from the shell:$vi filenameTo invoke more than one file,$vi filename1 filename2 . . . . . .If file already exists, then contents are displayed on the screen, if not it is created.$vi letterWhen we type the vi command and the filename, vi clears the screen and displays a window in which we can enter and edit text. The _ (underscore) on the top line shows the cursor, when we enter the command. Every other line is marked with a ~ (tilde) the symbol for an empty line.

Page 38: Linux Important Material

If we have successfully entered vi, we are in command mode and vi waits for commands.To add text to the file letter:2 Press the ‘i’ key to enter the insert mode of vi. Now we can add text to the file.3 Press the Esc key to leave the insert mode and return to the command mode.4 To save this, hold down the shift key and press ‘Z’ twice (ZZ). At the bottom of the

screen, the file name and the number of lines and characters in the file are displayed. (or)

5 Instead of using ZZ to save and quit we can also use :w and :q commands for writing and quitting a file. The :w command writes the buffer to a file. The :q command leaves the editor and returns to the shell. These two can be combined as :wq.

1.12.3 Editing the text:To move the cursor on the screen, there are four keys:h : moves the cursor one character to the left.l : moves the cursor one character to the right.j : moves the cursor down one line.k : moves the cursor up one line.The j and k commands maintain the column position of the cursor.Instead of pressing l two times to move the cursor two spaces right, we can enter 2l. We can move as many spaces, or lines as we desire. If that place falls outside the current window that part will be scrolled. If that space does not exist vi will sound to bell.Adding text:To add the text at the end of the file, first position the cursor on the last character in the file by using cursor movement keys. Next, hit an ‘a’ (append) to switch from command mode to text-input mode.If we want to add the text at the middle, instead of at the end, then press ‘i’ (to insert text before the current cursor position). If characters are typed then they will be added to the left of the current cursor position. The vi editor will continue to accept the characters until Esc key is pressed.How to delete text:If we want to delete a character, move the cursor to that character and press the x.x: delete one characternx: delete ‘n’ characters, where ‘n’ is the number of characters you want to delete.vi adjusts the text so that no gap appears in place of the deleted string.Overwriting text:If we want to overwrite the existing text then first position the cursor (using h, j, k, l) at the character from where you want overwriting to begin. Next hit a ‘R’ (a capital R) to indicate that whatever you type next should overwrite the existing text at the current cursor position. Press Esc when you finish replacing text.To make undo for most recent change or deletion type an ‘u’. Capital ‘U’ undo all the changes to the current line since you moved the cursor to that line.Quitting vi:To save the document and quit press ‘ZZ’ in the command mode or ‘:wq’ in ex command mode. If you decide to quit and not save the changes you have made, type ‘:q!’ in ex command mode and then press Enter.Find and Replace:

COMMAND FUNCTION:s/str1/str2 Replaces first occurrence of str1 with str2 in current line.:s/str1/str2/g Replaces all occurrences of str1 with str2 in the current

line.:m,n s/str1/str2/g Replaces all occurrences of str1 with str2 from lines m to

n.:1,$ s/str1/str2/g Replaces all occurrences of str1 with str2 from the 1st line

Page 39: Linux Important Material

to the end of the file.:1,. s/str1/str2/g Replaces all the occurrences of str1 with str2 from the first

line to the current line.:.,$ s/str1/str2/g Replaces all the occurrences of str1 with str2 from the

current line to the end of the file.

Delete and Paste:

COMMAND FUNCTION“xdd Deletes current line and copies it to a buffer named x. (x

can be any alphabet).“xndd Deletes ‘n’ lines from current line and copies them to a

buffer named x.“xdw Deletes current word and copies it to a buffer named x.“xndw Deletes ‘n’ words from current cursor position and copies

to a buffer named x.“xp Pastes contents of buffer x at current cursor position.

Yank and Paste:

COMMAND FUNCTIONYw Yanks word from cursor position.Yy Yanks line from cursor position.y$ Yanks line from cursor position to the end of line.y0 Yanks line from cursor position to the beginning of the line.Lp Pastes last yanked buffer.Zp Pastes last but one yanked buffer.“xyy Yanks current line to a buffer named x (x can be any

alphabet).“xnyy Yanks ‘n’ lines from current line to a buffer named x.“xyw Yanks current word to a buffer named x.“xnyw Yanks ‘n’ words from current cursor position to a buffer

named ‘x’.“xp Pastes content of buffer ‘x’ at current cursor position.

Set commands::set nu : Set display of line numbers on.:set nonu : Set display of line numbers off(default).:set eb : Beep the speaker when an error occurs(default).:set noeb : Don’t beep the speaker when an error occurs.

Backup Utilities cpio, tar.

1.13.1 cpio : Copy Input-Output:The cpio command (copy input-output) can be used to copy files to and from a backup device. It uses standard input to take the list of file names, and then copies them with the contents and headers, into a single archive that is written to the standard output. This means that cpio can be used with redirection and piping. It uses two ‘key’ options, -o(output) and –i(input) either of which must be there in the command line. Other options have to be used with either of these key options.Backing up files:

Page 40: Linux Important Material

‘ls’ can be used to generate a list of filenames for ‘cpio’ to use as input. The ‘-o’ key option is used to create an archive, which can then be redirected to a device file. This is how we copy files in the current directory to a floppy:#ls | cpio –ov > /dev/rdsk/foarray.plcalendar276 blocksThe ‘-v’ option makes ‘cpio’ operate in the verbose mode, so that the filename is seen on the terminal when it is being copied. What ‘cpio’ needs as input is a list of files to be backed up, with one file in each line. If this list is available in a file, redirection can be used as,#cpio –o > /dev/rdsk/fo < file1Incremental backups:‘find’ can also produce a file list, so any files that satisfy it’s selection criteria can also be backed up. We will frequently need to use ‘find’ and ‘cpio’ in combination to back up selected files, for instance, those that have been modified.find . –type f –mtime -2 –print | cpio –ovB > /dev/rdsk/foSince the path list of ‘find’ is a dot, the files are backed up with their relative pathnames. However if it is a /, absolute pathnames are used.The ‘-B’ option sets the block size to 5120 bytes for input and output, which is 10 times the default size. For higher (or lower) sizes, the ‘-C’ option has to be used.#ls *. Pl | cpio –ovC 51200 > /dev/rdsk/foMulti-volume backups:When the created archive in the backup device is larger than the capacity of the device, ‘cpio’ prompts for inserting a new diskette into the drive:#find . –type f –print | cpio –ocB > /dev/rdsk/foReached end of the medium on output.If you want to go on, type devive/filename when ready/dev/fdo #device name entered here3672 blocksRestoring files:A complete archive or selected files can be restored with the ‘-i’ key option. To restore all the files that can be backed up with a previous ‘cpio’ command, the shell’s redirection operator (<) must be used to take input from the device.#cpio –iv < /dev/rdsk/foarray.plcalendar276 blocksTo restore sub-directories ‘-d’ option is used.‘cpio’ accepts a quoted wild-card pattern, so that multiple files fitting the pattern can be restored. Restoring only the shell script becomes quite easy.cpio –i “*.sh” < /dev/rdsk/foOther options:There are 3 important options that can be used with both the ‘-o’ and ‘-i’ modes:

The ‘-x’ (rename) option lets you rename each file before starting the copying process. The system presents each filename, and prompts you for a response. If you enter a filename, copying is done to that file; a null response leaves the file uncopied.

The ‘-f’ option, followed by an expression, causes ‘cpio’ to select all files except those in the expression:cpio –ivf “ *.c” < /dev/rdsk/fo # restores all except C programs.

The ‘-c’ option tells ‘cpio’ to use ASCII characters, rather than the binary format, for creating headers.

Displaying the archive:The ‘-t’ option displays the contents of the device without restoring the files. This option must be combined with the ‘-i’ key option:

Page 41: Linux Important Material

#cpio –itv < /dev/rdsk/fo100755 henry 605 oct 18 23:34:07 2005 file1.pl . . . . . . . . . . . . . . . . . . . . . . . .cpio non-key options (to be used with –i or –o)

OPTION SIGNIFICANCE-d Creates directories as when needed.-c Writes header information in ASCII characters for portability.-r Rename files in interactive manner.-t Lists files in archive.-u Overwrites newer file with older version.-v Verbose option – prints list of files that are being copied.-m Retains original file modification time.-f exp Copies all files except those in exp.-C size Sets input-output block size to size option.

1.13.2 tar : The tape archive program:The tar (tape archive) command has been in existence before the emergence of cpio. It not only creates archives on tapes, but supports floppies as well. Unlike cpio, tar does not write to the standard output, but creates an archive in the media. It is a versatile command with certain exclusive features not found in cpio. Some of them are:

It does not use standard input to obtain it’s file list. tar accepts file and directory names as arguments.

It copies one or more entire directory trees, i.e it operates recursively. It can create several versions of the same file in a single archive. It can append to an archive without overwriting the entire archive.

tar is used with a number of key options- the common ones are –c(copy), -x(extract), and –t(list). One and only one of these options has to be present. The ‘-f’ option additionally has to be used for specifying the device name.Backing up files:tar accepts directory and filenames directly on the command line. The –c key option is used to copy files to the backup device:#tar –cvf /dev/rdsk/fo /home/sales/SQL/* .sql a /home/sales/SQL/invoice -all.sql 1 tape blocks a /home/sales/SQL/load2 –all.sql 10 tape blocksThis backs up all SQL scripts with their absolute pathnames to the floppy diskette. The single character a before each pathname indicates that the file is appended. (when restoring files,the character ‘x’ is seen in this option). The verbose option (-v) shows the number of blocks used by each file.When files are copied in this way with absolute pathnames, the same restrictions apply; they can only be restored in the same directory. However, if you choose to keep the option open for installing the files in a different, then you should first “cd” to /home/sales/SQL, and then use a relative pathname:cd /home/sales/SQLtar –cvf /dev/fo . / *.sqlThe command will also execute faster if used with a block size of 18 (i.e 18*512 bytes):tar –cvfb /dev/rdsk/fo 18 *.sql Since both ‘-f’ and ‘-b’ each have to be followed by an argument, the first word (/dev/rdsk/fo) after option string –cvfb will denote the argument for –f, and the second (18) will line up with ‘-b’.

Page 42: Linux Important Material

The advantage of tar lies in that it can copy an entire directory tree with all it’s sub-directories. The current directory can backed up with or without the hidden files:tar –cvfb /dev/rdsk/fo 18 * #doesnot backup hidden files.tar –cvfb /dev/fdo 18 . #backs up hidden files.The files here are backed up with their relative pathnames, assuming that they all fit in one diskette.Multi-volume backup:For multi-volume diskette back ups, tar uses a special option(-k), followed by the volume size in kilo bytes.#tar –cvfkb /dev/rdsk/fo 1440 18 index1Volume ends at 1439k, blocking factor=18tar: large file index needs 2 extentstar: current device seek position=ok+++a index 1439k [extent #1 of 2]‘tar’ estimates that two 1440kB(the argument of –k) diskettes will be required. After the first volume is full, ‘tar’ prompts for a new volume:tar: please insert new volume, then press RETURNtar options:

OPTION SIGNIFICANCE(key options)-c Creates a new archive.-x Extracts files from archive.-t Lists contents of archive.-r Replaces files at end of archive.-u Like r, but only if files are newer than those in archive.

OPTION SIGNIFICANCE(NON-KEY OPTIONS)-v Verbose option – lists files in long format.-w Confirms from user about action to be taken.-f dvc Uses the pathname dvc as name of device instead of the default.-b n Uses blocking factor n, where n is restricted to 20.-m Changes modification time of file to time of extraction.-k num Multi-volume back up- sets size of volume to num kilobyte.-M Multi-volume back up.-z Compresses while copying.-Z Decompresses while extracting. Restoring files:Files are restored with ‘-x’ (extract) key option. When no file or directory name is specified, it restores all files from the back up device. The following command restores the files just backed up:#tar –xvfb /dev/rdsk/fo 18x /home/sales/SQL/invoice - all.sql 1 tape blocksx /home/sales/SQL/load2 - all.sql 10 tape blocksSelective extraction is also possible by providing one or more directory or filenames:tar –xv6 tune1 songUnlike cpio when files are extracted, the modification times of the files also remain unchanged. This can be overridden by the ‘-m’ option to reflect the system time at the time of extraction.Displaying the archive:

Page 43: Linux Important Material

Like in cpio, the ‘-t’ key option simply displays the contents of the device without restoring the files. When combined with the ‘-v’ option, they are displayed in a long format similar to the listing:#tar –tvf /dev/rdsk/forwxr _ xr _ x 203/50 472 Jun 4 09:35 1991 ./file1.shAppending to the archive:A file can be added to the archive with the ‘-u’ option. Since this is a copying operation, the ‘-c’ option can’t be used in combination with this option. The unequal thing is that an archive can contain several versions of the same file:#tar –uf /dev/rdsk/fo ./func.sh

Network Utilities: ftp, rlogin, telnet, arp.

1.14.1 ftp: Copying files between non-unix hosts. It is the file transfer protocol.$ftp source1 #open ftp connection to source1 Name : Password :ftp>ls #directory listing of remote host . . .who.cwriter.cftp > get writer.cftp > !ls #obtain directory of local hostftp > put who.cftp > quit #disconnectGoodbye$1.14.2 Distributing processing:Some hosts supply limited password less accounts with user ID’s like ‘user’ so those explorers can roam the network without causing any harm.Three utilities for distributing access are:

1. rlogin : Which allows you to log into a remote Unix host.2. rsh : Which allows you to execute a command on a remote Unix host, and3. telnet : Which allows you to execute commands on any remote host that has a telnet server.

Of these, ‘telnet’ is the most flexible, since there are other systems in addition to Unix that support telnet servers.rlogin : Remote logins:To logon to the remote host, we use rlogin.Syntax : rlogin –ec [-l useID] hostName‘rlogin’ attempts to log you into the remote host hostName.If you don’t supply a user ID by using the ‘-l’ option, your local user ID is used during the login process.If the remote host is not set as an equivalent of your local host in your “$Home/.rhost” file, your are asked for your password on the remote host.Once you are connected, your local shell goes to sleep the remote shell starts to execute. When you are finished with the remote login shell, terminate it in the normal fashion(ctrl D) and your local shell will then awaken.There are a few special “escape commands” each is preceded by the escape character, which is a tilde (~) by default, you may change this escape character by following the ‘-e’ option with the preferred escape character.Escape commands:

Page 44: Linux Important Material

SEQUENCE MEANING~. Disconnect immediately from remote host.~susp Suspend remote login session. Restart remote login using ‘fg’.~d susp Suspend input half of remote login session, but still echo output from

login session to your local terminal. Restart remote login using ‘fg’.

Ex : $rlogin user2 #remote login Last login: Tue Dec 20 17:23:51 from Varun user2 % date Wed Dec 21 18:50:47 CDT 2005 user2 % ^D #terminate the remote login shell connection closed. $- #back home again at VarunHere, we have logged into remote host ‘user2’ from local host ‘Varun’.Syntax : ftp –n [hostname]‘ftp’ allows you to manipulate files and directories on your local host and a remote host.1.14.3 telnet : Remote connection:‘telnet’ allows you to communicate with any remote host on the Internet that has a ‘telnet’ server.Syntax : telnet [host[port]]telnet establishes a two way connection with a remote port. If you supply a host name, but not a port specifier, you are automatically connected to a telnet server on the specified host, which typically allows you to log into the remote machine. If you don’t even supply a host name, ‘telnet’ goes directly into command mode (like ftp).To enter command mode after you have established a connection, press ctrl-] which is the telnet escape sequence. This causes the command mode prompt to be displayed. The following commands, among others are accepted:

COMMAND MEANINGClose Close current connection.Open host[port] Connect to ‘host’ with optional port specifier.Quit Exit telnet.Z Suspend ‘telnet’.! Print summary of ‘telnet’ commands.

1. Ex : $telnet #start telnet telnet > ? #get help commands may be abbreviated. Commands are: close close current connection. z suspend telnet.

quit exit telnet. . . . telnet > open user3 #get a login shell from user3

- - - - - - login: 12A4 #user ID password : * * * *

. . .user3 % dateTue Dec 20 17:23:24 CDT 2005

Page 45: Linux Important Material

user3 % ^Dconnection closed by foreign host.

2. Ex : $telnet user3 login: . . .3. Ex : $telnet user3 13 [port 13 is for time and day] . . . Tue Dec 20 17:26:32 2005 Connection closed by the foreign host.Port 7 echoes everything that you type back to your terminal and is sometimes known as ‘ping-port’.$telnet user3 7 . . hi hi ^ ]

telnet >quit connection closed‘telnet’ accepts numeric internet address as well as symbolic names.$telnet 129.110.43.128 7 #user3’s numeric address

1.14.4 arp : Address Resolution Protocol:

The arp command is used to manipulate system arp cache.

$arp options hostname

Options:-d - deletes hostname-a - displays entry of given host-n - shows numerical address

In a networked system when a packet arrives at router machine then the IP address to Ethernet address mapping is needed. This is achieved by arp protocol. Theses mappings are stored in arp cache such that next time another packet arrives the same ip address then its Ethernet or physical address is calculated by carrying out a lookup operation on the arp cache. With arp command we can modify, view, delete the entries of this cache.

Page 46: Linux Important Material