1

Unix and Shell Programming
1. Overview of Operating System Introduction, OS and its services, Character vs GUI, Types of OS , Unix family, Brief History of the Unix Operating System –Introduction, Features of Unix, Historical development, variations in Unix systems, Unix Architecture. 2. Understanding the Unix command Locating commands, Internal and External commands, Command Structure, Flexibility of command usuage, man,knowing your machine. General Purpose Utilities- Cal, date, echo, printf, bc, script, passwd, who, uname, tty, sty. The File System: File, parent-child relationship, the HOME variable, pwd, cd, mkdir, rmdir, absolute pathnames, relative pathnames, ls, the Unix file system. Handling ordinary files – cat, cp, rm, mv, more, lp, wc, od, cmp, diff, dos2unix and unix2dos, compressing and archiving files, gzip and gunzip, tar, zip and unzip, ls-l, chmod, File Systems and Inodes, Hardlinks, Symbolic links, umask, find. 3. The vi editor vi basics, input mode, saving text and quitting, navigation, editing text, undoing last editing instructions,repeating the last command, searching for a pattern, substitution. Advanced vi – operators, the ex Mode, storing multiple text sections, undoing multiple line deletions, entering control characters, searching for a character, customizing vi. 4. The Process – Basics, ps, system processes,process creation, Internal and external commands, running jobs in background, nice, killing process with signals, job control, at and batch, cron, time, Environment variables, aliases, command history. Communication and Email – finger, talk, mesg, email basics, mailx, pine,ifconfig, ping,netstat, ftp,telnet. Simple filters – pr, head, tail, cut, paste, sort, uniq, tr, grep, ERE and egrep, sed. Awk –awk filtering, splitting line into fields, printf, the comparison operator, number processing, variables, storing awk programs in a file,The BEGIN and END Sections, Built in variables, arrays, functions, control flow, looping with for, looping with while. 5. Shell Programming The Shell – The Shell’s Interpretive cycle, Shell offerings, Pattern Matching, Redirection, Two special files, pipes, tee, command substitution, shell variables. Basic Shell Programming: Shell scripts, Making Scripts Interactive, Using Command line arguments, exit and Exit status of a command, The logical operators && and ||,the if conditional, using test and [ ] to evaluate

2 expressions, the case conditional, expr, $0, while, for, set and shift, the here document, trap, debugging shell scripts with set –x. Advanced shell programming- Shells and Subshells, export, Running script in the current shell, let, arrays(korn and bash), String handling, Conditional Parameter Substitution, Shell functions, eval, the exec statement. 6. Perl Features of perl, chop(),variables and operators, String Handling function, Specifying filenames in command line, $_,$.,Range operator, Lists and arrays, foreach, split, join, dec2bin, grep, associate arrays, regular expressions and substitution, file handling, file tests, subroutines. Text Books 1. Unix concepts and applications – Sumitabha Das 2. Unix the Text book –Syed Mansoor Sarwar, Robert Koretsky,Syed Aqeel Sarwar. 3. Unix Shell Programming – Yashavant Kanetkar History of Unix  1969  First UNIX at Bell Labs  The MULTICS  Kernighan, Ritchie, Thompson  1970’s  Bell Labs makes UNIX freeware  Berkeley UNIX (BSD)  Bill Joy vi editor, C Shell  1980’s  System V release 4  TCP/IP  Sun Microsystems Solaris  Microsoft Xenix, SCO  MIT X-Windows  1990’s  GNU, LINUX  Stallman, Torvalds

1. (a) Explain the architecture of UNIX operating system. 08 Ans: In UNIX the division of labour is between two agencies-the kernal and the shell.the kernal interacts with the machine’s hardware, the shell with the user. kernal:-the kernal is the core of the operating system-a collection of routines mostly written in c.these routines communicate with the hardware directly.it is that part of the unix system that is loaded into

3 memory when the system is booted.user progams(the applications) that need to communicate with the hardware(like the hard disk or the terminal) use the services of the kernal,which performs the job on the user’s behalf. These programs access the kernal through a set of functions called system calls. kernal also manages the system’s memory, schedules processes, decides their priorities. Shell:-it is actually the interface between the user and the kernal.even though there’s only one kernal running on the system, there could be several shells in action-one for each user who is logged in. System calls: these are the functions to communicate with the kernal.often the same system call can access both a file and a device.these system calls are built into the kernal, and interaction through them represents an efficient means of communication with the system.

1. (b) Explain POSIX

02

Ans: the POSIX (Portable Operating System Interface for Computer Environments) standard refers to a group of related standards for operating systems in general, but was based on UNIX because UNIX was supposedly free from bias on account of its vendor-neutrality.two of the most –cited standards

2.There are general-purpose tools. and each user is allotted a segment.unix provides a vast array of text manipulation tools that can edit these files without using an editor. 2. .POSIX.the computer breaks up a unit of time into several segments. UNIX: a multitasking system A single user can also run multiple tasks concurrently. but the kernal by itself can’t do anything that can benefit the user.this process goes on till the circle has turned full-circle and the first user’s job is taken up once again. the machine will be doing the job of a single user. The UNIX Toolkit Unix represents the kernal. object code or a directory structure.POSIX.so at any point in time.2. which is the name given to a file when it is executed as a program.4 from the POSIX family are known as POSIX. 3. the kernal is designed to handle a user’s multiple needs. we need to use the host of applications that are shipped with every unix system. The File and Process A file to UNIX is just an array of bytes and can contain virtually anything-text. the resources are actually shared between all users. the previous job is kept in abeyance and the next user’s job is taken up.1 and POSIX.the moment the allocated time expires.1 specifies the c application program interface-the system calls.text manipulation utilities(called filters). The second entity is the process.block approach Many UNIX tools are designed with the requirement that the output of one tool be used as input to another.To properly exploit the power of unix.compilers and interpreters.like files.2 deals with the shell and utilities.networked applications and system administration tools. 4. (a) Explain the salient features of the unix operating system 08 Ans: the features of the UNIX operating system are: 1. The building. processes also belong to a separate hierarchical tree structure. UNIX:a multiuser system in unix. 5.

2. Programming Facility The UNIX shell is also a programming language. (b) Explain tput. thus making it available for the next user.5 6. loops and variables.unix features elaborate pattern matching schemes that use several characters from this matacharacter set. Pattern matching The *(known as a metacharacter) is a special character used by the system to indicate that it can match a number of filenames. Documentation The principle on-line help facility available is the man command. which remains the most important reference for commands and their configuration files. (a) explain internal and external commands with example 04 .these features are used to design shell scripts-programs that also include UNIX commands in their syntax 8. like control structures. exit commands Ans: tput clear: clearing the screen tput command to clear the screen tput clear 02 The execution of this vanishes whatever was typed and the prompt and cursor are positioned at the top-left corner of the screen. that establish it as a powerful programming language in its own right. 3.It has all the necessary ingredients. Exit: this command suspends the session $ exit login: The login: message confirms that the session has been terminated. 7.

There’s a separate section EXIT STATUS which lists possible error conditions and their numeric representation.these built-in commands.the SYNOPSIS follows certain conventions and rules which every user must understand:  If a command argument is enclosed in rectangular brackets. when we type echo. are called as an external command. SYNOPSIS and DESCRIPTION) are generally seen in all man pages. tty commands with example 10 Ans: cal: THE CALENDER The cal command is used to see the calender of any specific month or a complete year. Explain cal. For ex: ls command is an external command Echo isn’t an external command in the sense that.6 Ans: the commands having independent existence in the /bin directory (or /usr/bin). it will execute it from its own set of built-in commands that are not stored as separate files. (b) Explain the man documentation 06 ans: unix offers an on-line help facility in the man command. the argument is required. SYNOPSIS shows the syntax used by the command and DESCRIPTION (often the largest section) provides a detailed description. 3. of which echo is a member are known as internal commands.Every command doesn’t have all sections but the first three( NAME. The syntax also tells us that when cal is used with arguments. then it is optional.To see the calender for the month of March 2003. NAME presents a one-line introduction to the command.  If there is a | character in any of these areas. echo. All options used by the command are listed in the OPTIONS section.  The ellipsis (a set of three dots) implies that there can be more instances of the preceding word. you need two arguments: $ Cal 03 2003 March 2003 S M Tu W Th F S . the month is optional but the year is not. the shell won’t look in its PATH to locate it (even if it is there in /bin). it means that only one of the options shown on either side of the pipe can be used. printf.the facility is totally accurate and takes into account the leap year adjustments that took place in the year $ Cal It displays the calender of the current month. 4.A man page is divided into a number of compulsory and optional sections. bc. otherwise.Rather.

$ echo “Enter filename:\c” Enter filename: $ _ This is how echo is used in a shell script to accept input from the terminal.7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Echo: displaying a message Echo is used in two ways  to display a message  to evaluate shell variables echo interprets certain strings known as escape sequences.printf uses many of the formats used by C’s printf function. where can’t exceed 0377(decimal value 255) printf: The command in its simplest form can be used in the same way as echo: $ Printf “No filename entered\n” No filename entered $ _ printf also accepts all escape sequences used by echo. Escape sequence used by echo \a bell \b backspace \c no newline (cursor in same line) \f formfeed \n newline \r carriage return \t tab \v vertical tab \ Backslash ###BOT_TEXT###n ASCII character represented by the octal value n. %s %30s %d %6d %o %x %f string as above but printed in a space 30 characters wide decimal integer as above but printed in a space 6 characters wide octal integer hexadecimal integer floating point number . An escape sequence is generally a two character-string beginning with a \ (backslash).

42 bc is quite useful in converting numbers from one base to another ibase=2 11001010 202 The reverse is also possible obase=2 14 1110 bc also handles hexadecimal numbers obase=16 14 E tty: KNOWING YOUR TERMINAL This command tells the filename of the terminal you are using $tty /dev/pts/10 The terminal filename is 10 resident in the pts directory. the sacle have to be set to the number of digits of precision Scale=2 17/7 2.8 bc: bc belongs to a family of commands that expect input from the keyboard when used without an rgument.the directory in turn is under the /dev directory. . 2^32 144 4294967296 .output in decimal-base 10 . multiple calculation in the same line To enable floating-point computation.key in the following arithmetic expression and then use [Ctrl-d] to quit bc: $ bc 12 + 5 17 [Ctrl-d] $ _ 12*12. Tty is used in a shell script to control the behaviour of the script depending on the terminal it is invoked from.

Explain date. uname. sttty. respectively D-the date in the format mm/dd/yy T-the time in the format hh:mm:ss Uname: the command displays certain features of the operating system running on your machine.9 5. M and S-the hour. By default. minute and second. using the format +%m: $ date +%m 12 Or the month name: $ date +%h Dec or you can combine them in one command: $ date +”%h %m” Dec 12 Other format specifiers are d-the day of the month y-the last two digit of the year H.8 The implementation name of this operating system can be obtained with the –s option $ uname -s . it displays the name of the operating system: $ uname SunOS The Current Release and Implementation Name (-r and –s): the –r option is used to find the version of your operating system $ uname -r 5. who. which shows the date and time to the nearest second: $ Date Tue Dec 10 18:52:26 IST 2002 We can print only month. script commands with options and example 10 Ans: date: we can display the current date with the date command.

. (a) explain the different types of files supported in UNIX 06 Ans: the file is a container of information or a sequenceof charactersunix treats directories and devices as files as well. then it must have aname (called hostname).10 SunOS The Machine Name (-n): if your machine is connected to a network.heavens.heavens. $ script Script started. file is typescript $ _ The prompt returns and all your keystrokes that you noe enter here get recorded in the file typescript. the third. [Ctrl-h] erase a character and [Ctrl-u] kills a character.the setting signifies [Ctrl-d] interrupts a program.the last column shows the machine name from where the user logged in.com) script: RECORDING YOUR SESSION This command lets you record your login session in a file.-n option tells the hostname $ uname -n mercury stty: this command displays and changes settings The –a option displays the current settings.heavens. To know the user who invoked the who command use the arguments am and I with who: $ who am i kumar pts/10 Aug 1 07:56 (pc123. (-H) option prints the column header.com) The first column shows the usernames (or user-id). Script –a Appends to it or uses a different log file. 6. fourth and fifth columns show the date and time of logging in. the second column shows the device names of their respective terminals.the who command displays an informative listing of these users $ who root kumar sharma console pts/10 pts/6 Aug 1 07:51 (:0) Aug 1 07:56 (pc123.com) Aug 1 02:10 (pc125. Who:WHO ARE THE USERS? UNIX maintains an account of all user who are logged on to the system.

and displays a four-columnar output.must have a parent.text file:a text file contains only printable characters.wc.the implicit feature of every unix file system is that there is a top. and the object code and excutables that you produce by compiling C programs are also binary files.  Directory file:a directory contains no data.depending on the options used.binary file:a binary file contains both printable and unprintable characters that cover the entire ASCII range.more 10 Ans: pwd: pwd command tells you your current directory $ pwd /home/kumar pwd displays the absolute pathname. (b) explain the UNIX file system tree 04 Ans: the file system in unix is a collection of all the related files (ordinary. Most UNIX commands are binary files. wc:wc command counts lines.a unique identification number for the file or directory.these subdirectories.  Device file: Device filenames are generally found inside a single directory structure. It takes one or more filenames as its arguments. sounds and video files are binary files as well. . 6.every file apart from the root. 7.rm.explain the following commands with options and suitable example pwd. have more subdirectories and other files under them. in turn . /dev.all C and Java program sources.the kernal identifies a device from its attributes and then uses them to operate the device. A directory file contains an enty for every file and subdirectory that it houses.it is advantageous to treat devices as files as some of the commands used to access an ordinary file also work with device files.and characters. shell and per1 scripts are text files.11 Files can be divided into three categories  ordinary file:an ordinary file itself can be divided into two types: . but keeps some details of the files and subdirectories that it contains. The filename .and it should be possible to trace the ultimate parentage of a file to root. directory and device files) organized in a hierarchical structure.root is actually a directory.words. Pictures.each entry has two components: . The root directory(/) has a number of subdirectories under it. .this top is called root and is represented by a / (frontslash). which serves as the reference point for all files.cp.

wc offers three options to make a specific count.20 words and 103 characters. the syntax requires at least two filename to be specified in the command line.the following command deletes the first three chapter of the book: rm chap01 chap02 chap03 Interactive deletion (-i): the –i option makes the command ask the user for confirmation before removing each file. Copying directory structure (-R): it’s possible to copy an entire directory structure with the –R option. Interactive copying (-i): the –i option warns the user before overwriting the destination file.at each step it deletes everything it finds.the –l option counts only the number of lines. cp -R progs newprogs rm: files can be deleted with rm . while the –w and –c options counts words and characters respectively: $ wc -l infile 3 infile $ wc -w infile 20 infile $ wc -c infile 103 infile when used with multiple filenames.as well as a total count. Recursive deletion (-r or –R): with the –r (or –R) option.the first is copied to the second Cp chap01 until If the destination file(until) doesn’t exist. $ cp -i chap01 until cp:overwrite until (yes/no) ? y A y at this prompt overwrites the file. wc produce a line for each file.it can delete more than file with a single invocation. When both are ordinary files. rm performs a tree walk – a through recursive search for all subdirectories and files within these subdirectories. . cp: the cp command copies a file or a group of files. it will first be created before copying takes place.it creates an exact image of the file on disk with a different name.12 $ wc infile 3 20 103 infile wc counts 3 lines.

ls with –a option lists all hidden files all with the other files.13 Forcing removal (-f): rm will prompt you for removal if a file is write-protected. (a) explain the use of ls command with –x. The relative pathname uses either the current or parent directory as reference and specifies the path relative to it. argument and option? 04 Ans: If the first character of a pathname is a /.at the bottom of the screen. the file‘s location must be determined with respect to root. 8. the dot that repeats the last command you used 8. Such a pathname is called an absolute pathname. Aruments are the additional words used along with the commands UNIX command use a filename as argument so the command can take input from the file.lst calender helpdir cptodos.-F. (a single dot). (b) explain absolute pathname.one page at a time.10f can be used to scroll forward by 10 pages & 30b for scrolling back 30 pages.lst TOC.you’ll also see the filename and the percentage of the file that has been viewed. .sh progs ls with –F option is used to identify directories and executable files. More chap01 The contents of chap01 is displayed on the screen.This represents the current directory.. More has a repeat command.a relative pathnames uses one of these cryptic symbols: . ls with –r option sorts filenames in reverse order.-a & -r options 04 Ans: ls command is used to list the names of the files available in the directory ls with –x option is used to display filenames in multiple column $ ls -x 08_packets. More: the more command displays its output a page at a time. relative pathname. .(two dots)-this represents the parent directory. --more—(17%) to move forward one page.use f or spacebar & b to move back one page.html dept.the –f option overrides this minor protection also.sh emp.

In this chapter. ls -l note -l is an argument to ls by defination but it’s a special argument known as an option. Sometimes we combine this option with other options for displaying other attributes. we discuss. This does not mean that there are so many copies of the file.the home directory is determined by the system administrator at the time of opening a user account. (c) Explain file command and home variable? 02 Ans: file command is used to determaine the type of file (regular. directory or device) $ file archive. For ex. File is .PDF documents and even empty files.the shell variable HOME knows your current directory.14 Options are special type of argument that’s mostly used with a – sign. A file has a number of attributes (properties) that are stored in the inode. The output in UNIX lingo is often referred to as the listing. It lists seven attributes of all files in the current directory and they are: • • • • • • • File type and Permissions Links Ownership Group ownership File size Last Modification date and time File name The file type and its permissions are associated with each file. Basic File Attributes The UNIX file system allows the user to access other files not belonging to them and without infringing on security.compressed files. or ordering the list in a different sequence. HOME variable: home directory is created by the system when a user account is opened. 8. and can distinguish between shell programs.c source and object code. Links indicate the number of file names maintained by the system.zip archive.it also identifies DOS executables. ls look up the file’s inode to fetch its attributes.zip: ZIP archive File recognizes text files. • • • • ls –l to display file attributes (properties) Listing of a specific directory Ownership and group ownership Different file permissions Listing File Attributes ls command is used to obtain a list of all filenames in the current directory.

Every user is attached to a group owner. use ls –ld with the directory name. For example. but the privileges of the group are set by the owner of the file and not by the group members. Last modification time is the next field. you become its owner. the modification time remains unchanged. Note that simply using ls –d will not list all subdirectories in the current directory. It is displayed in the following format: y Filetype owner (rwx) groupowner (rwx) others (rwx) For Example: . he has to assign these parameters to the user: The user-id (UID) – both its name and numeric representation The group-id (GID) – both its name and numeric representation File Permissions UNIX follows a three-tiered file protection system that determines a file’s access rights. ls –ld helpdir progs drwxr-xr-x 2 kumar metal drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir 512 may 9 09:57 progs Directories are easily identified in the listing by the first character of the first column. Strange though it may seem. If you change only the permissions or ownership of the file. it displays the file name. File Ownership When you create a file.sh helpdir progs Listing Directory Attributes ls -d will not list all subdirectories in the current directory For example.lst genie. $ ls –l total 72 -rw-r--r--rw-r--r--rw-rw-rw-rw-r--r-drwxr-xr-x drwxr-xr-x 1 1 1 1 2 2 kumar kumar kumar kumar kumar kumar metal 19514 may 10 13:45 metal 4174 may 10 15:01 metal 84 feb 12 12:30 metal 9156 mar 12 1999 metal 512 may 9 10:31 metal 512 may 9 09:57 chap01 chap02 dept. Every owner is attached to a group owner. which here shows a d. File size in bytes is displayed. Several users may belong to a single group. When the system administrator creates a user account. In the last field. To see the attributes of a directory rather than the files contained in it.15 created by the owner. The significance of the attributes of a directory differs a good deal from an ordinary file. ls has no option to list only directories.

Faulty file permission is a sure recipe for disaster Changing File Permissions A file or a directory is created with a default set of permissions. You can set different permissions for the three categories of users – owner. we can change the file permissions and allow the owner to execute his file. This set of permissions is applicable to others. writable and executable by the owner of the file.write x . The third group has the write and execute bits absent.all (ugo) operation + assign . Let us assume that the file permission for the created file is -rw-r--r--. write. Its syntax is: chmod category operation permission filename(s) chmod takes an expression as its argument which contains: user category (user. Using chmod command.user g . -rw-r--r-1 kumar metal 1906 sep chmod u+x xstart 23:38 xstart .others a .read w .group o . which indicates the absence of write permission by the group owner of the file.16 -rwxr-xr-.1 kumar metal 20500 may 10 19:21 chap02 rwx owner/user r-x group owner r-others The first group has all three permissions. group and others. The second group has a hyphen in the middle slot. execute) Category u .execute Let us discuss some examples: Initially. The command can be used in two ways: In a relative manner by specifying the changes to the current permissions In an absolute manner by specifying the final permissions Relative Permissions chmod only changes the permissions specified in the command line and leaves the other permissions unchanged. It’s important that you understand them because a little learning here can be a dangerous thing. which can be determined by umask. The file is readable.remove = absolute permission r . others) operation to be performed (assign or remove a permission) type of permission (read. group.

chmod ugo+x xstart chmod a+x xstart chmod +x xstart -rwxr-xr-x 1 or or 23:38 xstart kumar metal 1906 sep chmod accepts multiple file names in command line chmod u+x note note1 note3 Let initially. The most significant digit represents user and the least one represents others. We can set all nine permissions explicitly. -rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart chmod go-r xstart Then. For each category. this is how the permission can be represented: • • • Read permission – 4 (octal 100) Write permission – 2 (octal 010) Execute permission – 1 (octal 001) Permissions ----x -w-wx r-r-x rwrwx Significance no permissions execute only write only write and execute read only read and execute read and write read. we add octal digits. write and execute Octal 0 1 2 3 4 5 6 7 We have three categories and three permissions for each category. so three octal digits can describe a file’s permissions completely. A string of three octal digits is used as an expression. If we represent the permissions of each category by one octal digit. it becomes -rwx--x--x 1 kumar metal 1906 sep 23:38 xstart Absolute Permissions Here. The permission can be represented by one octal digit for each category. we need not to know the current file permissions. other permissions remain unchanged.17 -rwxr--r-1 kumar metal 1906 sep 23:38 xstart The command assigns (+) execute (x) permission to the user (u). . chmod can use this three-digit string as the expression.

we have. chmod a+rwx xstart chmod 777 xstart -rwxrwxrwx The UNIX system by default. chmod a+rw xstart Using absolute permission. Only owner can change the file permissions. read and write permissions for the group and only execute permission to the others. User can not change other user’s file’s permissions. but still we can delete a file. 000 signifies absence of all permissions for all categories.18 Using relative permission. chmod 666 xstart chmod 644 xstart chmod 761 xstart will assign all permissions to the owner. 777 signify all permissions for all categories. directory permissions also play a very vital role here We can use chmod Recursively. Hence. It is the directory permissions that determine whether a file can be deleted or not. chmod -R a+x shell_scripts or . But the system administrator can do anything. we have. never allows this situation as you can never have a secure system. go-r xstart chmod 000 xstart ---------This is simply useless but still the user can delete this file On the other hand. The Security Implications Let the default permission for the file xstart is -rw-r--r-chmod u-rw. but still we can prevent a file from being deleted.

19 This makes all the files and subdirectories found in the shell_scripts directory. only system administrator can use chown On other systems. Directory Permissions It is possible that a file cannot be accessed even though it has read permission. and can be removed even when it is write protected. you will appreciate that the * doesn’t match filenames beginning with a dot. there are two commands meant to change the ownership of a file or directory. rwxr-xr-x (755) A directory must never be writable by group and others Example: mkdir c_progs ls –ld c_progs drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs If a directory has write permission for group and others also. ls -l note -rwxr----x 1 sharma metal 347 may 10 20:30 note . The default permissions of a directory are. The dot is generally a safer but note that both commands change the permissions of directories also. so use su command ls -l note -rwxr----x 1 kumar metal 347 may 10 20:30 note chown sharma note. Let kumar be the owner and metal be the group owner. you must not make directories universally writable unless you have definite reasons to do so. executable by all users. If sharma copies a file of kumar. on BSD and AT&T systems. be assured that every user can remove every file in the directory. As a rule. only the owner can change both chown Changing ownership requires superuser permission. When you know the shell meta characters well. then sharma will become its owner and he can manipulate the attributes chown changing file owner and chgrp changing group owner On BSD. Changing File Ownership Usually.

But he can copy the file to his own directory. with a separate file system in each partition. After we complete the first round of discussions related to files. This chapter also introduces the concepts of file system. accessed by the inode number. The inode contains the following attributes of a file: • • • • • File type File permissions Number of links The UID of the owner The GID of the group owner . n partitions = n file systems = n separate root directories All attributes of a file except its name and contents are available in a table – inode (index node). size. Kumar can no longer edit note since there is no write privilege for group and others. ownership and group ownership and different file permissions. we will take up the other file attributes. ls –l dept.lst. the user file permissions that previously applied to Kumar now apply to sharma.lst -rw-r--r-1 kumar metal 139 jun 8 16:43 dept. links. and in this chapter. File Systems and inodes The hard disk is split into distinct partitions. we look at most of the remaining ones. He can not get back the ownership either. A file also has properties related to its time stamps and links. ls –l dept. owner. chgrp This command changes the file’s group owner. MORE FILE ATTRIBUTES Apart from permissions and ownership. Every file system has a directory structure headed by root.lst In this chapter we considered two important file attributes – permissions and ownership. the lookup table that contained almost all file attributes.20 Once ownership of the file has been given away to sharma. Though a detailed treatment of the file systems is taken up later. Basic file attributes has helped us to know about .lst chgrp dba dept. date and the file name. Thus. It is important to know how these attributes are interpreted when applied to a directory or a device. listing of a specific directory. ls –l provides attributes like – permissions. knowledge of its basics is essential to our understanding of the significance of some of the file attributes.lst -rw-r--r-1 kumar dba 139 jun 8 16:43 dept. group owner. a UNIX file has several other attributes. in which case he becomes the owner of the copy. It also looks at the inode.ls –l to display file attributes (properties). No superuser permission is required.

The command can create both a hard link and a soft link and has syntax similar to the one used by cp. neither the name of the file nor the inode number is stored in the inode. The following command links emp.sh 478274 -rwxr-xr-.2 kumar metal 163 Jul 13 21:36 restore.lst 29518 -rwxr-xr-x 2 kumar metal 915 may 4 09:58 employee .21 • • • • • File size in bytes Date and time of last modification Date and time of last access Date and time of last change of the inode An array of pointers that keep track of all disk blocks used by the file Please note that.sh 478274 -rwxr-xr-. To know inode number of a file: ls -il tulec05 9059 -rw-r--r-.sh All attributes seem to be identical. This count is normally 1.1 kumar metal 51813 Jan 31 11:15 tulec05 Where. 9059 is the inode number and no other file can have the same inode number in the same file system.sh -rwxr-xr-.sh restore. but the files could still be copies.2 kumar metal163 jul 13 21:36 restore. Hard Links The link count is displayed in the second column of the listing.2 kumar metal163 jul 13 21:36 backup.lst with employee: ln emp. ls -li backup. It’s the link count that seems to suggest that the files are linked to each other.lst employee The –i option to ls shows that they have the same inode number.lst employee 29518 -rwxr-xr-x 2 kumar metal 915 may 4 09:58 emp. but the following files have two links. -rwxr-xr-.2 kumar metal 163 Jull 13 21:36 backup.sh ln: Creating Hard Links A file is linked with the ln command which takes two filenames as arguments (cp command). But this can only be confirmed by using the –i option to ls. meaning that they are actually one end the same file: ls -li emp.

ln -s note note. Size of symbolic link is only 4 bytes.dat as: ln employee emp. your existing programs will continue to find foo. You can increase the number of links by adding the third file name emp. which is normally one for unlinked files. It’s important that this time we indeed have two files. but simply provides the pathname of the file that actually has the contents. With this link available. But if we remove note.sym 9948 -rw-r--r-. A file’s name is available to a C program and to a shell script. Removing note. a symbolic link doesn’t have the file’s contents. and they are not identical. it is the length of the pathname of note. A file is considered to be completely removed from the file system when its link count drops to zero.dat 29518 -rwxr-xr-x 3 kumar metal 915 may 4 09:58 emp. A single file with two links can have its program logic make it behave in two different ways depending on the name by which it is called. -> indicates note.sym ls -li note note.22 The link count. we don’t need to maintain two programs as two separate disk files if there is very little difference between them.dat . we would lose the file containing the data. We can’t have two linked filenames in two file systems and we can’t link a directory even within the same file system.sym would point to a nonexistent file and become a dangling symbolic link. especially when they exist in different directories. Use the –f option to force the removal of the existing link before creation of the new one Where to use Hard Links ln data/ foo.sym ->note Where. Links provide some protection against accidental deletion. . In that case. is shown to be two.sym contains the pathname for the filename note.txt in the input_files directory. ls -l emp* 29518 -rwxr-xr-x 3 kumar metal 915 may 4 09:58 emp. but then the destination filename must be a directory.1 kumar group 80 feb 16 14:52 note 9952 lrwxrwxrwx 1 kumar group 4 feb16 15:07note. It is more convenient to do this that modifies all programs to point to the new path. Because of links. Symbolic Links Unlike the hard linked. ln returns an error when the destination file exists. l indicate symbolic link file category. note.lst 29518 -rwxr-xr-x 3 kumar metal 915 may 4 09:58 employee You can link multiple files. This can be solved by using symbolic links (soft links).txt input_files It creates link in directory input_files.sym won’t affect us much because we can easily recreate the link.

Consider removing the read permission first from the directory progs. The higher the number of files. Since ls reads the directory to display filenames. ls –ld progs . The Directory A directory has its own permissions. If you have to link all filenames in a directory to another directory. the pathname is stored in the symbolic link and occupies space on disk. if a directory’s read permission is removed. The permissions of a directory also impact the security of its files. any user could destroy the integrity of the file system. The user has all permissions. the larger the directory size. Permission acquires a different meaning when the term is applied to a directory. and group and others have read and execute permissions only. The significance of the file attributes change a great deal when applied to a directory. Only the kernel can do that. Linux uses a fast symbolic link which stores the pathname in the inode itself provided it doesn’t exceed 60 characters. If that were possible.23 Symbolic links can also be used with relative pathnames. owners and links. a symbolic link has a separate directory entry with its own inode number. we must know what permissions for a directory really mean. To try that out. the size of a directory is in no way related to the size of files that exists in the directory. restore the read permission and remove the write permission from the directory before you try to copy a file to it. but rather to the number of files housed by it. they can also span multiple file systems and also link directories. it makes sense to simply link the directories. Read permission Read permission for a directory means that the list of filenames stored in that directory is accessible. For example. ls -l -d progs drwxr-xr-x 2 kumar metal 320 may 9 09:57 progs The default permissions are different from those of ordinary files. chmod 555 progs . Write permission for a directory implies that you are permitted to create or remove files in it. To understand how that can happen. In most cases. ls -ld progs drwxr-xr-x 2 kumar metal 128 jun 18 22:41 progs chmod -r progs . This means that rm can remove a symbolic link even if its points to a directory. ls progs progs: permission denied Write permission We can’t write to a directory file. Unlike hard links. ls wont work. Like other files. However. A symbolic link has an inode number separate from the file that it points to.

so the cd command won’t work if the search permission for the directory is turned off. changing system wide default permission settings is possible using chmod but not by umask MODIFICATION AND ACCESS TIMES • • A UNIX file has three time stamps associated with it. A directory has to be searched for the next directory.2 kumar metal 128 jun 18 22:41 progs cd progs permission denied to search and execute it umask: DEFAULT FILE AND DIRECTORY PERMISSIONS When we create files and directories. $ umask 022 This becomes 644 (666-022) for ordinary files and 755 (777-022) for directories umask 000. That’s why the execute privilege of a directory is often referred to as the search permission. two are: Time of last file modification ls -l Time of last access ls –lu .lst progs cp: cannot create progs/emp. Changing a file doesn't modify its directory entry Execute permission If a single directory in the pathname doesn’t have execute permission. Note that.24 dr-xr-xr-x 2 kumar metal 128 jun 18 22:41 progs cp emp. Among them. we are not subtracting anything and the default permissions will remain unchanged. rw-rw-rw.(octal 666) for regular files rwxrwxrwx (octal 777) for directories The default is transformed by subtracting the user mask from it to remove one or more permissions. chmod 666 progs . This indicates.lst: permission denied • • The write permission for a directory determines whether we can create or remove files in it because these actions modify the directory Whether we can modify a file depends on whether the file itself has write permission. We can evaluate the current value of the mask by using umask without arguments. then it can’t be searched for the name of the next directory. ls –ld progs drw-rw-rw. The UNIX system has the following default permissions for all files and directories. the permissions assigned to them depend on the system’s default setting.

lst -rw-r--r-.1 kumar metal 870 mar 16 14:30 emp.lst (without options and expression) Then. How ever. Knowledge of file‘s modification and access times is extremely important for the system administrator. and then takes some action on the selected files.lst touch -a 01261650 emp. touch options expression filename(s) touch emp.lst find : locating files It recursively examines a directory tree to look for files matching some criteria.lst . then you should look up the cryptic find documentation. Many of the tools used by them look at these time stamps to decide whether a particular file will participate in a backup or not. The –m and –a options change the modification and access times. and if you have ever wondered why UNIX is hated by many. we have.1 kumar metal 870 mar 16 14:30 emp. hour and minute). both times are set to the current time and creates the file.lst It is possible to change the two times individually. ls -lu emp. if it doesn’t exist. It has a difficult command line. respectively: touch command (with options and expression) -m for changing modification time -a for changing access time touch -m 02281030 emp. ls -l emp.1 kumar metal 870 jan 26 16:50 emp.lst . touch command (without options but with expression) can be used. find is easily tamed if you break up its arguments into three components: .25 The access time is displayed when ls -l is combined with the -u option. TOUCH COMMAND – changing the time stamps To set the modification and access times to predefined values.lst -rw-r--r-. The expression consists of MMDDhhmm (month.lst -rw-r--r-.lst . day.1 kumar metal 870 feb 28 10:30 emp. touch 03161430 emp. ls -l emp.lst -rw-r--r-.lst ls -lu emp.

This line is also used by the system to display messages. You will have to be in this mode to copy and delete text For. If a word has been misspelled. we invoke. This makes the command difficult to use initially. Invoke the execute mode from the command mode by entering a: which shows up in the last line. the file doesn’t exist.26 find path_list selecton_criteria action where. The last line is reserved for commands that you can enter to act on text. To enter text. use ctrl-w to erase the entire word. The vi Editor To write and edit some programs and scripts. vi <filename> In all probability. Now press esc key to revert to command mode. you must switch to the execute mode (the last line mode). vi uses 24 out of 25 lines that are normally available in the terminal. Bram Moolenaar improved vi editor and called it as vim (vi improved) on Linux OS. Actually. They are non-existent lines. and you are in this mode ready to input text. the cursor is positioned on the last character of the last line. • Recursively examines all files specified in path_list • It then matches each file for one or more selection-criteria • It takes some action on those selected files The path_list comprises one or more subdirectories separated by white space. text editing. the text entered has not been saved on disk but exists in some temporary storage called a buffer. The Repeat Factor . Subsequent key depressions will then show up on the screen as text input. you must switch to the input mode. but it is a program that every user must master since it lets him make file selection under practically any condition. First press the key i. After text entry is complete. and vi presents you a full screen with the filename shown at the bottom with the qualifier. After the command is run. This is known as current line and the character where the cursor is stationed is the current cursor position. This mode is used to handle files and perform substitution. This is the default mode of the editor where every key pressed is interpreted as a command to run on text. UNIX provides vi editor for BSD system – created by Bill Joy. A beep in vi indicates that a key has been pressed unnecessarily. vi Basics To add some text to a file. To save the entered text. using most of the keys of the keyboard. The cursor is positioned at the top and all remaining lines of the screen show a ~. This is the command mode. we require editors. There can also be a host of selection_criteria that you use to match a file. and multiple actions to dispose of the file. you are back to the default command mode. Press it again and you will hear a beep. This is the mode where you can pass commands to act on text.

I and A behave same as i and a. From time to time. we actually mean saving this buffer. press Esc u To clear the screen in command mode. To undo whenever you make a mistake. you should save your work by writing the buffer contents to disk to keep the disk file current. but only a copy of it that is placed in a buffer. 10k moves cursor 10 lines up. etc will appear in the last line. Command mode command k moves the cursor one line up. text. Pressing ‘i’ changes the mode from command to input mode. Some of the save and exit commands of the ex mode is: FUNCTION inserts text appends text inserts at beginning of line appends text at end of line opens line below opens line above replaces a single character replaces with a text replaces entire line . When we talk of saving a file. the original file is not distributed as such. :set showmode Messages like INSERT MODE.vi commands are case-sensitive Avoid using the PC navigation keys Input Mode – Entering and Replacing Text It is possible to display the mode in which is user is in by typing. CHANGE MODE. To append text to the right of the cursor position. A appends text at end of line. o opens a new line below the current line • • • • r<letter> replacing a single character s<text/word> replacing text with s R<text/word> replacing text with R Press esc key to switch to command mode after you have keyed in text Some of the input mode commands are: COMMAND i a I A o O r s S Saving Text and Quitting – The ex Mode When you edit a file using vi. REPLACE MODE. You may also need to quit vi after or without saving the buffer. press ctrl-l Don’t use (caps lock) .27 vi provides repeat factor in command and input mode commands. but at line extremes I inserts text at the beginning of line. we use a.

28 Command :W :x :wq :w <filename> :w! <filename> :q :q! :sh :recover Navigation A command mode command doesn’t show up on screen but simply performs a function. To move the cursor in four directions. and w is allowed moves back to beginning of word moves forward to end of word moves forward to beginning word moves cursor up moves cursor down moves cursor left moves cursor right Action saves file and remains in editing mode saves and quits editing mode saves and quits editing mode save as save as. you can use the wordnavigation commands to go there directly. There are three basic commands: b e w Example. vi understands a word as a navigation unit which can be defined in two ways. k j h l Word Navigation Moving by one character is not always enough. You will often need to move faster along a line. e. To move to the first character of a line 0 or | 30| moves cursor to column 30 $ moves to the end of the current line The use of these commands along with b. If your cursor is a number of words away from your desired position. 5b takes the cursor 5 words back 3w takes the cursor 3 words forward Moving to Line Extremes Moving to the beginning or end of a line is a common requirement. depending on the key pressed. but overwrites existing file quits editing mode quits editing mode by rejecting changes made escapes to UNIX shell recovers file from a crash .

They use operators.29 Scrolling Faster movement can be achieved by scrolling text in the window using the control keys. deletes a single character delete entire line copy entire line deletes the current line and five lines below delete yank (copy) to know the current line number goes to line number 40 goes to line number 1 goes to end of file . The two commands for scrolling a page at a time are ctrl-f ctrl-b 10ctrl-f ctrl-d ctrl-u scrolls forward scrolls backward scroll 10 pages and navigate faster scrolls half page forward scrolls half page backward The repeat factor can also be used here. p and P place text on right and left only when you delete parts of lines. Absolute Movement The editor displays the total number of lines in the last line Ctrl-g 40G 1G G Editing Text The editing facilitates in vi are very elaborate and invoke the use of operators. d y Deleting Text x dd yy 6dd Moving Text Moving text (p) puts the text at the new location. But the same keys get associated with “below” and “above” when you delete complete lines Copying Text Copying text (y and p) is achieved as. such as.

in which case use ctrl-r to redo your undone actions. and not n. The direction depends on the search command used. In that case. N will repeat the search in the forward direction. The function of U remains the same. repeated use of this key progressively undoes your previous actions. word . type. we use To discard all changes made to the current line. You could even have the original file in front of you. we use u U vim (LINUX) lets you undo and redo multiple editing instructions. You may overshoot the desired mark when you keep u pressed. undoing with 10u can be completely reversed with 10ctrl-r. Repeating the Last Command The . Further.30 yy 10yy copies current line copies current line & 9 lines below Joining Lines J 4J to join the current line and the line following it joins following 3 lines with current line Undoing Last Editing Instructions In command mode. where n is set to 1000 by default. then n also follows the same direction. to undo the last change made. u behaves differently here. The undoing limit is set by the execute mode command: set undolevels=n. Further 10u reverses your last 10 editing actions. (dot) Searching for a Pattern / search forward ? search backward /printf The search begins forward to position the cursor on the first instance of the ?pattern Searches backward for the most previous instance of the pattern Repeating the Last Pattern Search n repeats search in same direction of original search n doesn’t necessarily repeat a search in the forward direction. If you used? printf to search in the reverse direction in the first place. (dot) command is used for repeating the last instruction in both editing and command mode commands For example: 2dd deletes 2 lines from current line and to repeat this operation.

The ex mode is also used for substitution.s/director/member/g :$s/director/member/g can also use % instead of 1.$s/director/member/gc Each line is selected in turn. waiting for your response. followed by a sequence of carets in the next line. Its syntax is.10s/director/member/g :. Both search and replace operations also use regular expressions for matching multiple patterns.$ deletes unsigned everywhere in lines 1 to 50 substitute lines 3 through 10 only the current line only the last line Interactive substitution: sometimes you may like to selectively replace a string.31 Search and repeat commands Command /pat ?pat n N Function searches forward for pattern pat searches backward for pattern pat repeats search in same direction along which previous search was made repeats search in direction opposite to that along which previous search was made Substitution – search and replace We can perform search and replace in execute mode using :s. vi Editor Information To enter vi: To enter vi from the command line. use any of the forms below: vi file Invoke vi editor on file vi file1 file2 view file vi +n file vi -r file Invoke vi editor on files sequentially Invoke vi editor on file in read only mode Invoke vi editor. just below the pattern that requires substitution.50s/unsigned//g :3. editing starts on line n Recover changes made to file after a system crash . :address/source_pattern/target_pattern/flags :1. In that case. add the c parameter as the flag at the end: :1. The cursor is positioned at the end of this caret sequence.$s/director/member/g :1.

file The name of a file that is affected by the command. One or n lines previous. Address Symbols In ex command syntax. options may be any of the following: ! Indicates a variant form of the command. % stands for current file.$. d3 deletes three lines beginning with the current line. same as 1.y All lines in the file.32 ex Commands To enter an ex command from vi: To enter an ex command from vi. with current line reset to x. use this form: :[address] command [options] address is the line number or range of lines that are the object of command. Absolute line number n. 0 . Top of file. n lines before x. Previous mark.$ x. Lines x through y. Lines x through y. 3d deletes line 3. the current line is the object of the command. n $ % x-n x+n -[n] +[n] 'x '' /pat / or ?pat Ahead or back to line matching pat. count cannot precede the command. ? Option Symbols In ex command syntax. n lines after x. All lines. Last line. If no address is given. Line marked with x. Current line.y x. . t because a number preceding an ex command is treated as a line address. coun The number of times the command is to be repeated. address can be specified by any of the following: 1. One or n lines ahead. # stands for previous file. overriding the normal behavior.

if autoindent was enabled. replace the current argument list with filelist and begin editing on the first file. Append text at specified address. Add a ! to switch the autoindent setting during input of text. quit[!] Terminate current editing session. or at present address if none is specified. or the sequence #n. If no filename is given. With the +n argument. Use ! to discard changes made since the lsat save. The command t is a synonym for copy. from named buffer specified by char. to the line specified by address. a single lowercase letter. n[!] [[+command]filelist] Edit the next file from the command-line argument list. or at present address if none is specified. [address] m destination Move the lines specified by address to the destination address. ! disables it. If buffer is specified. Use args to list these files. e[!] [+n] [filename] Begin editing on filename. [address] co destination Copy the lines included in address to the specified destination address. Add a ! to switch the autoindent setting that will be used during input. [address] pu [char] Restore previously deleted or yanked lines. [address] ma char Mark the specified line with char. representing a function key on the keyboard. if char is not specified. bring in another copy of the curent file.33 Commands append [address] a[!] text . Return later to the line with 'x. [address] i[!] text . Add a ! to edit the new file even if the current file has not been saved since the last change. the last deleted or yanked text is restored. If the editing session includes additional files in the argument list that have not yet been copy delete edit insert map mark move next put quit . if command is given (containing no spaces). map char commands Define a macro named char in visual mode with the specified sequence of commands. execute command after editing the first such file. begin editing on line n. Insert text at line before the specified address. [address] d [buffer] Delete the lines included in address. save or append the text to the named buffer. char is usually a single character. If filelist is provided. That is.

[address] z [type] [count] Print a window of text with line specified by address at the top. command and insert. type can be one of: + Place specified line at the top of the window (default). [address] w[!] [[>>]filename] Write lines specified by address to filename. or if no char is specified place in general buffer. If filename is not specified. .Place specified line at bottom of the window. Insert mode (text entry) is terminated by <escape>. the current filename is used. In vi. write contents to the end of the specified filename. [address] r !command Read in the output of command into the text after the line specified by address. if >> filename is used. If address is specified. undo write u Reverse the changes made by the last editing command. Add a ! to force the editor to write over any current contents of filename. count specifies the number of lines to be displayed. An option of g substitutes all instances of pattern on the line. . and replace the lines with the output. If filename is also omitted. commands have the following general form: [n] operator [m] object . apply the lines contained in address as standard input to command. Place specified line in the center of the window. save the contents of the buffer to the current filename. quit by typing q! or by typing q twice. [address] !command Execute command in a shell. repeat last substitution. ^ Print the previous window. If pattern and repl are omitted. = Place specified line in the center of the window and leave the current line at this line.34 accessed. yank z ! vi Commands vi has two "modes". read [address] r filename Copy the text of filename at the specified address. An option of c prompts for confirmation before change. <escape> in command mode is ignored. which toggles back to command mode. or full contents of buffer if address is not specified. read substitute [address] s [/pattern/repl/] [options] Replace each instance of pattern on the specified lines with repl. [address] ya [char] [count] Place lines specified by address in named buffer indicated by char.

previous paragraph Beginning of next. sentence paragrap Is up to next blank line or paragraph macro defined by para=option. h section Is up to next section heading defined by sect=option.l Text: w. W.35 The basic editing operators are: c Begin a change. Movement Commands Character: h. yy. !. Otherwise.j. d} Delete up to next paragraph. E ). A capitalized object is a variant form that recognizes only blank spaces. If the current line is the object of the operation.. the editing operators act on objects specified by cursor-movement commands or patternmatching commands. then the operator is the same as the object: cc. or the number of objects the operation is performed on. ( }. 5yy Copy the next five lines.[[ Forward. If both n and m are specified. B e. b. y]] Copy up to the next section. right (direction arrows) . the effect is n x m. previous section Left. n and m are the number of times the operation is performed. y Begin a yank (or copy). An object can represent any of the following text blocks: word Includes characters up to a space or punctuation mark. Is up to .{ ]]. d^ Delete back to beginning of line. Examples 2cw Change the next two words. d Begin a deletion. ? followed by two spaces. dd. previous sentence Beginning of next. up. backward by word End of word Beginning of next. down.k.

? Fx Fx Tx Tx . Line number: Ctrl-G nG Display current line number and file name Move to line number n Search forward for pattern Search backward for pattern Repeat last search in same. Ctrl-U Ctrl-E. top of window Reposition line with cursor: to top of screen Reposition line with cursor: to middle of screen Reposition line with cursor: to bottom of screen Redraw screen (without scrolling) First. Ctrl-Y z<Return> z. . opposite direction Repeat previous search forward. backward Search forward for character x in current line Search backward for character x in current line Search forward for character before x in current line Search backward for character after x in current line Repeat previous current-line search Repeat previous current-line search in opposite direction Scroll forward one screen Scroll backward one screen Scroll down. last position of current line First character of current line (ignore spaces) First character of next. up one-half screen Show one more line at bottom. n| H M L nH nL Screens: Ctrl-F Ctrl-B Ctrl-D. previous line Column n of current line Top line of screen Middle line of screen Last line of screen n (number) of lines after top line n (number) of lines before last line . N /. zCtrl-L.36 Lines: 0. Ctrl-R Searches: /pattern ?pattern n. $ ^ +.

A o. P "np Delete character Delete character before cursor Delete word Delete current line Delete to end of line Put deleted text after. above cursor Yank: . O Change: R Cw Cc C R S S Delete. after cursor Insert text at beginning. a I. end of line Open new line for text below. move: X X Dw Dd D p. before cursor Put text from delete buffer n after cursor (for last nine deletions) Change character (no <escape> required) Change word Change currnt line Change to end of line Type of characters Delete character and substitute text Delete current line and substitute text Insert text before.37 G :n Marking position: Mx `x `` 'x '' Mark current position as x Move cursor to x (grave character) Return to previous mark or context (two grave characters) Move to beginning of line containing mark x (single quote) Return to beginning of line containing previous mark (two single quotes) Move to last line in file Move to line number n Editing Commands Insert: i.

new Quit file Quit file (overriding protection) Quit vi and invoke ex Edit file2 without leaving vi Edit next file Return to version of current file at time of last write (save) Edit alternate file Current filename Alternate filename . 60w >> file Write from line 30 through line 60 and append to file Write current buffer named file as file.new :q :q! Q :e file2 :n :e! :e# % # Write (save) and quit file Write (save) and quit file Write (save) and quit file Write (save) file Write (save) file (overriding protection) Write from line 30 through 60 as newfile :30. before cursor Put text from buffer a before cursor Exit Commands ZZ :x :wq :w :w! :30. P "aP Other commands: . 60w newfile :w %.38 Yw Yy "ayy p. u. restore current line Join two lines Yank (copy) word Yank current line Yank current line into named buffer a Put yanked text after. U J Repeat last edit command Undo last edit.