0% found this document useful (0 votes)
3 views184 pages

System Programing

The document outlines the course objectives and content for Systems Programming (CSIWZC327), focusing on UNIX commands, system calls, and file management. It provides a brief history of UNIX, its architecture, and the roles of operating systems in managing hardware and software resources. Additionally, it introduces basic UNIX commands and their usage, emphasizing the importance of understanding the command structure and file organization.

Uploaded by

imstiwari9630
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
3 views184 pages

System Programing

The document outlines the course objectives and content for Systems Programming (CSIWZC327), focusing on UNIX commands, system calls, and file management. It provides a brief history of UNIX, its architecture, and the roles of operating systems in managing hardware and software resources. Additionally, it introduces basic UNIX commands and their usage, emphasizing the importance of understanding the command structure and file organization.

Uploaded by

imstiwari9630
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
CSIWZC327, Systems Programming Eta Lecture No. 1 Systems Programming . # Course No(s): csiw zc327 Systems Programming!) ial Introduction to Systems No Course Objective eee ~ CO1 To provide understanding of the files, directory and file TL Richard Blom, Christine Bees, “Linus Command Line ad ean eas eee eee c02 To provide insight into UNIX commands and Shell ‘12 Maurice, Bach, “The Design ofthe Unix Operating Ssstem", PHT, ay ua C03 To provide understanding of the System calls and its TS Brian. W. Kernighan and Rob Pike, “The UNIX Programs i ~ srommning usage with C programming. jor inna errata) C04 To provide understanding of assemblers, linkers and Ts Leland, Reck “An Ttreduet Ss ystems Programming” 3rd loaders IMP Notetoser == > tis important to know that just login to the session does not ‘Quarantee the attendance. Start > Once you Join the session, continue tl the end to consider a you as present in the class, a ET deere Recording =~ wecaeererem on ee anecoar eenrinee ag eee IMP Note to Students seers ce a ae ree =—— What is UNIX ? == ‘> What is an Operating System (08)? Functions of an 0S UNIX Operating System UNIX Architecture, Feature of UNIX, 4 A Briet History of UNIX, Command Structure and Usage ‘#Basic UNIX commands — passwd, date, who, whoam mail, write, man, pwd, Is. 1 Setens remem “VES $i Sanaa GTS speneoganming Vad 6am no ac Little History of Unix “The UNEK operaing ster asbom inte ee 12005 "oxpnaty ben as aonaimanrcetiadby Kan Thompson of Libs, ends sce gun obocrme homo wikely wd cpraing stom, Ken Tharpson iriess an super Ruck Cana, Daughcoy, Jee Osan avons Pia, wee a sal general purpose tmoshaing system PDP 2s MULTICS (MULT eed Ineraion and Comeutng Serves). | Richie aloo designe and wrote a compiler forthe C Programming lnguege "in 1873, Ritchie and Thompson rerota the UND KemalinC breaking the tadtin that ‘system softwar writen in Assembly kevelanguage on PDP-11 machine. [Around 1974 was icenaed to Universes for “Educational purposes” and afew years later became avaabe for commercial purpose. Little History of Unix | 11979, he seventh ein” (V7) version of Unix was released grander ofall extant Unicom. Atri pont the history of Unk becomes somewhat convoluted. ‘Shc then thas spread word wide, wih tans of thousands of systems instal, tom "Mero Computers to Main Frames. Inthe tme since UNDX was frst developed, t wesinamed as, og LUNCS (UN retin end Comping Save) er haroedo UNOCandthasoxne euch mary cet tad eneratcnsendoven mutsons. 1 Stems regain Fn eb Une oe TTS + An Operating syste (0S) A program that acts ss anintsmeciary betwen a user of ‘computer andthe computer hana. * Operating sytem gots: ~ acute user prog anemia shing wer eters ek, What is QS ? ~ Mie tacoma jst crite \n simple ters, on oping syste is 2 manager kmanages lhe vale resources on comput om he CPU, te memory tar eek sees. | re Pe siecltrdran Tooting ramccrtck at eps computers eemyeng cong ogee Senipeens ‘tens oacewine VED Seon Pagan VEO a aaa 1, Hardware — provides basic computing resources (CPU, ‘memory, IO devices). 2. Operating system — controls and coordinates the use of the hhardware among the various application programs for the 3. Applications programs ~ define the ways in which the system resources are used to solve the computing problems of the users (compilers, database systems, video games, business programs). 4, Users (people, machines, other computers), Computer System Components 2 syaenaPopiing "VEE ‘iar ann ec DIT Abstract View of System | Operating System is Responsible + Resource allocator — manages and allocates resources. * Control program — controls the execution of user programs and operations of I/O devices . + Kemel - the one program running at all times (all else being application programs). “ [a Sa —<— Se Common Services of an OS Fiaheee Menagerie ~ Process Management + A process is a program in execution. A process : needs certain resources, including CPU time, Main Memory Management memory, files, and VO devices, to accomplish its File Management wast Sarandary/Starageseanagement + The operating system is responsible for the following W/O Subsystem Management activites in connection with process management. ~ Process creation and deletion = process suspension and resumption = Provision of mechanisms fr: + process syncronization + process communication * 15 etene regan VIS $F oan mcvaaT OGTR Sytem Papaaing V0 wish no aac TASTES + Memory is a large array of words or bytes, each with its own address. It is a repository of quickly accessible data shared by the CPU and /O devices. + Main memory is a volatile storage device. contents in the case of system failure. + The operating system is responsible for the following activities in connections with memory management: ~ Keep track of which parts of memory are curenty Deing used and by whom, = Decide which processes to load when memory space becomes avaliable ~ AAlocate and deaocate memory space as needed Main-Memory Management It loses ite ” sen AEE VST a a TT Seconda storage Manageme a + Since main memory (primary storage) is volatile and too ‘small to accommodate all data and programs Permanently, the computer system must provide ‘secondary storage to back up main memory. + Most modem computer systems use disks as the principle or-line storage medium, for both programs and data. + The operating system is responsible for the following activities in connection with disk management ~ Free space management = Storage allocation ® seten epamilag VE) $s pn en etd oa TTT + A file is a collection of related information defined by its creator. Commonly, files represent programs (both source and object forms) and data, + The operating system is responsible for the following activities in connections with file management: ~ File creation and deletion = Directory creation and deletion. ‘= Support of primitives for manipulating files and directories. ~ Mapping files onto secondary storage. File Management 18 yen gan VE a a TT V0 Subsystem Management | + Keeping track of status of the V/O devices * Deciding who gets the device, for how long and when + Allocating a device to a process and de-allocation » ‘Sten: Pegaenlee VE. $s ain aoe NTT MB unixarchitecure “~TThke main concept tat unis alle versions of Un Be ToTewing four basics ‘erm ~The keret is the hear ofthe operating system, interacts wah the The UNIX Operating System + Simple and yet powerful / Sy Seer + Amuitiuser, multitasking, portable operating te management ein “Shel Theat nthe willy fat poceses your requests nen you ype a comma at outa, eset ert te + Interaction with the system via a command commun ate roman fat el “" interpreter shell Ex ¢ Shel, Bourne Sal and Kom Shel ae he most tau hel hich ae vat wh mow che Une ara on the lighter side, oman Soar ea pero UNIX s simple. But, you need fo be a genus understand Can make use on your dy to day acts. Ex ep:m, ca and grep, et. ae ew examples of commands and utes ts simplicity? “Fes and Diectones ~ Al the data of Unix is organized int les, ‘A fics are then organized ito directories. Those directories are futher 24 organized hina Hee-tke Stucke cal the Nlesysiem. Spiers reemanEe VD $a ca Sptems Mopar VSO BF ei TOE Unix Architectur = Unix Architecture | “Tho block diagram of tho UNIX OS kernel relationships with each other ‘Tho system call and library interface mac the border between user programs and th omnel *System calls look ike ordinary function & in C programs, sLibraries map these funtion cals to the sett] primitives needed to enter the operating systom / *Assembly language programs may ivok system calls directly without a system call 4 leary. = Programs frequenty use other Rbravies suet as the standard UO library to provide @ etre spit yen ¥ 1 "RUNDX command san action request gen tothe UNX sel foreseen [AYUNIX commands apply an actin ora series of actions fo some input data and ‘ate some output at, ‘AULUNIX commands are c2se sonst. “The command formats $ verb Toptions} [arguments] hire ‘eri the command name ‘options moa how the action app ae _rguments provide atonal intonation to he command Command Structure “Thee are afew commands tke portal dont take any arguments, some commands (ke who, I) that may or may ot take any arguments and Some commands (ike eu) at eompulsony take argues 25 Flexibility of Command Usage . B ‘command can offen be erred in more than cne ay ‘Fass ues, you can reset he number keystrokes oa miu, Examples: (Conbnng Commands So chap} te =L chapt Command ine that overlows LUNDK et you ype commande without wating fora prompt, The commands ‘ae passe shi fer erpetaton ater he pres conan has ‘competed. a tens earn =VIS $s a ns oa na TOI Internal and External command: “The commands tha are baitn as part the sel are calle inte commands Ezangle echo command ‘A program efile having an independent existence inthe in rectoy (rhs), feealed ns an exter command Example 5, we, ea bo, more PATH variable: Cortans the sequonce of doctors that te shell searchos took for Senmand When you issue a command, ‘Te shel anes tie3buitn command so, t execs ect Iota buitan, searches the aswoasted le he cece spestedin PATH AT found, teserides thee (Onset tignes a eer message 2 pte nL VIO a ao UNIX offers an onine help facty inte man command ‘man displays the documentation a tho spectied command. man pages Example 5 man wo displays help on we command ‘man uses a pager program, which displays this documentation one page ata time ‘man is configured to be used with a specific pager. Two avaiable pagers are more, aBorkoley pager, as an alfomative tothe ATAT pg command 7 standard pager on Linux systems, also ‘modoled aftr vi editor and is more powerful than more sysene eganmieVE $s ain eo TTT Asample man Page UNIX files and directoricgen "TA data Unis rganizod ito fies “All fles ate organized into directories. These Greclories are organized inio @ tree-ike structure called the: Fiesysiem sin Unix, there aro tee basic types of fies ~ Orsinary Files — An ordinary le alo on the system that contains data Gea pra ‘Wetrachn nts ona you Took at sworn wh oiainary hres “Directories ~ Directories store both special and ordinary files “For users famibar with Windows or Mac OS, Unix directones. are equivalent to folders “Special Fes — Some, special fies provide access to, hardware Such as ilar dives, CD-ROM dives, modems, and Ethernet adaptors, sy2ers eran "Vo i Fa a unaware TET “Terminal settings directly impact keyboard operation, and you should know hich Keys to press when things dont quite work 3s expected. When things go wrong Tpeookeor [Fenton (oat swat [te oDoae —| tere comm cut ay Termine lp aba r+ progam a pec Hs pe | ica Sige uring ee peta its beard ena etme sir nga anes ye (mad Kil comodo wet meng en ie realy pogo bt ene cre RE nang ery henge (ots Sends pots eae hl prop we rem [ats sene [Reser emia nena 0 ‘TAile i a set oF data that fas a name, The Information can be an ordinary text, a user-written computer program, results of a ‘computation, a picture, and so on. Directories and Files + The fle name may consist of ordinary characters, digits and special okens like the underscore, the forward slash (). It 's permitted to use special tokens like the ampersand (8) in & flename, + Unix organizes files in a tree-tke hierarchical structure, with the root arectory, Indicated + bya forward slash (), atthe top ofthe tree. See the Figure below, Inwhich par of the hierarchy of fles and directors on the ‘computer Is shown. 6 ee a oe UNIX files and directonee ea Regular file | Bw om + Itmay be text or binary file + Both the files are executable provided execution rights write - Opens a line of communication to send messages to other users on the system in real time, pwd - Prints the current working directory Relative & Absolute path "cd - Changes directories The difference between relative and absolute paths mkdir - Creates a directory "rmdir - Removes a directory (assuming it is empty) ‘Is - Lists the contents of a directory "> cat - Shows the contents of a file, all at once "Pr - Displays the specified fles on the standard output _s write Command pwd Command [> The write command enables message sending over the system in real time, I provides conversation-ike communication with another loggedn User. Each user altemataly sends and recsives short messages from the ‘ther workstation. Long messages can be sent by putting the complete message ina fle and then redracting that fle as Input to the write command, Ex: To write a message to a user who is logged in, enter: write usemame Welcome to Systems Programming press the Cir-D key sequence to terminate the write command mode. ‘Appath is a unique location to a file or a folder in a file system. Relative & Absolute path ‘An absolute path is defined as the specifying the location of a file or directory from the root directory()). ‘An absolute path is defined as specifying the location of a fle or directory from the root dlrectory(). In other words, we can ‘say that an absolute path Is a complete path from start of actual fle system from /directory. + pwd - print working directory + Atany time you can determine where you are in the file system hierarchy with the pwd, print working directory, Eg. $ pwd Jhomerfrank/sre pte eg ao a Absolute path ‘To write an absolute path-name: Start atthe root directory (/) and work down, White a slash (/) after every directory name Ex Scat datalst will work only if the fle “datalst” exists in your current directory. However, if this fle is not present in your working directory and is present somewhere else say in JhomeNWiproWASE, then this ‘command wil work onty ifyou will use itlike shown below: cat homeNWiproWASEidatalst Sptems romenning V0 ish ano inca Do Relative path is defied asthe pt relate fo the present working recto). stats at your current rectory and never starts witha. Relative path ‘UN ofes a shortcut inthe relive pathname that uses ether the cuent or parent rectory as reference and species the path relative to RA restive pathname uses one ofthese cryptic symbols: ¥—_{asinge ots represents the cent crectry. fw dos this represents the parent rectory. * Yay gato ou tame nec by moka ab. Sree, Tg done Tifortetve palvtinearecy nS examples kc path Creates decry path under curent directory Inka pan cbs doe Croates too drecones under curent rectory mkdir - make a directory Tpkai, wo wipprogs wipldata Creates a dreciry toe wih wip as 0 Socloty undehe ent decry and props and dat as subectones ‘indore + Note the order of speciving arguments in example 3. The parent directory stguld be specie fst, folowed by the subarectores to be costed +The system may reuse to create a directory duet the felling reasons: 4 The directory aloady outs. 2. There may bean onary fie by the same name inthe curent ‘recon. 5. Tha permission set fore currant actor don't pesmi the esation ‘ot "iis and ortanes by tho user os i cd command "Your can change to anew directory with the Ca, Change directory, command. ‘ed ill accept both absolute and relative path names. ‘Syntax ‘ed [directory] Examples ‘ed changes to user's home directory ‘ed! changes directory to the eystem's root ‘ed... goes up one directory level ‘ed -/.. goes up two directory levels. ‘ed Mullpathinamertrom/root changes directory to absolute path named {note the leading slash) a pte eg a an a rmdir - remove directory Ma A diractory needs to be empty before you can remove it. fits not, you need to remove the fles fst. ‘Also, you can't remove a directory if tis your present working directory; you must frst change out ofthat directory. You cannot remove a subdirectory unless you are placed in a directory whichis hierarchially above the one you have chosen to Eo. ‘mdi path Directory must be empty ‘dle wip wiplprogs wipidata Shows error as wip is not empty. However rmdir slenty deletes the lower level subdirectories progs and data, A simple description of the UNIX system is this: Types of files + ‘Ona UNIX system, everything is a file; i something is not a file, itis a process.” + AUNIX system makes no difference between a file and a directory, since a directory is just a file containing names of other files. Programs, services, texts, images, and so forth, are all files. LISTING FILE ATTRIBUTES = 1s provides attributes ike ‘Owner ‘Group owner File name Is command Listing Files To list the files and directories stored in the current Groctry, use the folowing command 1s—Ito display Me attributes (properties) “Listing ofa specific directory (Ownership and group ownership Different file permissions vvvy Spe Fagan =VSS $$ Sr Seo aes ET 1 kaumar metal 19514 may 10 13:45 chap01 1 kumar metal 4174 may 10 15:01 chap02 averwiw- I kumar metal 84 feb 12. 12:[Link] -et— 1 kumar metal 9156 mar 12 1999 genie-sh drwar-arx 2 launar metal 512 may 9 10:31 belpdir drwar-xrx 2 kumar metal 512 may 9 09:57progs Is-Lprovides attributes ike 1. The filetype and permissions associated with each fle 2. ‘The number of file names maintained by the system. ‘This does not mean that there are two copies of the file 3. File created by the owner 4, Every users attached to.a group owner LISTING DIRECTORY ane |b-4d helpairprogs drwar-xrx 2 kumar metal 512 may 9 10:31 helpdir dewsr-xr-x 2 kumar metal 512 may 9 09:57 progs Note: 1s-d_ will not list all subdirectories in the current directory 5. File size in bytes 6. Last modification time. ITyou change only the permissions, ‘or ownership of the file the modification time remains ‘unchanged 7. Displays file narme UNIX files and directorieganeaaay “Listing Fes: $16 «Prefix & Description - Regular fie, such as an ASCII text fie, binary executable, or hard link - b - Block special file. Block inputfoutput device file such as a physical hard driv. = © = Character special file, Raw inputloutput davice fle such asa physical hard drive. = 4 - Disco tht contains a isting of eter les and irectories. =| “Symbolic link fe, Links en any regula file ‘Named pipe. A mechanism for interprocess communications ‘Socket used for interprocess communication Sptens eee VIO a Daa RTT UNIX files and dii rectorieg ane ‘Metacharacters “Motacharactors havo a special moaning in Unix “For oxamplo, "and ? are motacharacters. We use * to match 0 or more characters, 2 question mark (?) matches with ‘a single charactor. $[Link]" doc ~Dispays alle files, the names of which start win ch and end wihdoc ~ “CHT doe [Link] che doe e3-2.d0e “ENU4% doe, eMO40 doe eh. doe [Link] “ch-2 doc ch02-1 dor c “Here, * works as meta charactor which matches with any character. “you want io display alle fles ending wath just doe, then you can use the folowing command — “$15 “doc UNIX files and dit rectoricgen “Creating Files: You can use the vi editor to create ordinary fies on ary Unix system “8 vifiloname ‘The above command wil open a fle wih the given tlename, ‘Now, press the Key 10 come into the eat mode Once you aro inthe adit mod, you can start wniting your eontont in the fle ‘as in tno Tolowang progam — This sun le. tented forthe st be. tm gong to save this contnt nti Once you are done with the program, fllow these steps ~ “Press the key ex fo come out of the ed mode “Press two keys Shift 22 together / Wo come out ofthe file completely Display Contents of a File _ +S cal flonamo, sons reaming "Vo i Fa anh unaroar TET UNIX files and directoriega may aan le sone, the first character of which isthe dot or the period {Ua ogra (nluing te shell) sc most of thse eso strc ‘Some common of the hidden fies inelude the files ~ gulf Bote SS sein See = ohne The Kom shel (ksh) inialization script ~eshie ~ The C shell (es) sitilization serge “To list the invisible files, specify the -a ‘Shea : fie es |i et emai Z Fons ens ‘pub. were Senace tin nl eel work Teme chor tind) real? ‘Single dot () ~ This represents the current direstory. ‘Double dot (.) ~ This fepresents the parent directory, ‘command in Linus usec for umching the ed et etor which sa nebo tert ctor witha minima nterface whch makes lees complex for woring on text les Le ering. cing. spaying and manipusting fs is the oldest etorbultin th Unc tis sucoseded by viand emacs tet dior. ‘Syptac edopsins fe) ed command whoutary opton:twilaunch the eer wt an emybuterfor sto wa hie simartthe oer visual or command ne based dtr when, Ivohad wihout a fhrare. The simplest way to start the ad tat er by ‘yping edo the ein, ed command [Ex 1: Here we have not specified the filename so this will ersate an ‘empty buffer for us to wre. [Ex 2: To Enter into insert mode press “2” and when you are done ting stop ity *" (dot. [Ex3: To view, the laste enter “pinto the ed command prompt. ‘4: To printllthe lines that we Insertedin the buffer by using p’. [Ex5: To save these Ines into a le write“ flename]’, [EX6: Type “W/ ined command promptto write the data into the le ‘and see how many bytes are written, ed command frequently used cat commande 1) Toviewa single fle Scat flename Output: Itwill show content of given filename 2)To view multiple files ‘Scatfilet fez ‘Output: This willshow the content of filet and fle? 3) To view contents of a fle preceding with ine numbers. ‘Scat-n filename Output: twill show content with fine number cat : displaying and creating eee ccat{concatenate) command is very frequently used in Linux. It reads data from the fle and gives their content as output. It helps us to create, view, concatenate file To deplay the contents ofthe sal les Scat opt st — deptst should be present in the current directory at also accepts more than one argument Scat fot fied ‘The contents of Mla? is shown immediately er tlo1. eat concatenates tho ‘es, hence ts name. Options with cat (-v and -n) (not POSIX rec.) v= displays non printable charactors Senin sere} uit Theconertv be cone desnson e 9\Catcan appre te certs fone obo ae of ancta , scat >> uit Warps cnn ane fio end fence he 7)Catcarmendcan dea carton oxrso adr ng uc cone ‘Sactknane Cup Wi ply cote nor acer Sptene ogncing "VIS + pr displays the specified files on the standard output in a paginated form, pr formats the given files into single~ column 66-line pages. + Each page has a S-line header. A S-line trailer consists, of blank lines. + pr command adds suitable headers, footers and formatted text ‘+ pr adds five lines of margin at the top and bottom The header shows the date and time of last modification of the file along with the filename and page number pr command pr options -k prints k (integer) columns -tto suppress the header and footer -hto have a header of user's choice -d double spaces input -nwill number each line and helps in debugging -on offsets the lines by n spaces and increases left margin of page sytens rgsrine VSO pr command pr : paginating files We know that, cat deptist O'laccounts\6213, 2Iprogs|6423 i O4lpersonnel!2365, O5|production S876 (6|sales|1006 The UNIX Pipe (|) "peaks 2 a vay of connecting the output fom one command nee Ea eo eet os ean to lnk con eof ne tat vos Oberle ke MDE Sop ed mn igirraionte dh cent uber fuses logged ont the cent stm, ~The who command vil gve us ine by ine ouput of al the + Wecoudlthen use fhe wc {to count the numberof ines. + ho wed IMP Note to Self CSIWZC327, Systems Programming BITS Pilani i 4 Lecture No. 3 IMP Note to Self Start Recording Agenda > Files and directories in UNIX, file syste > Basics of UNIX File System (CFS). > What is inode in URS? ode structure in UNIX ypes of links [hard, symbolic] in UFS. Different between these links. > Discussion on choosing hard link Vs Symbolic link > Discussion about 5 IMP Note to Students {tis important to know that just login to the session does not guarantee the attendance. ‘Once you jon the session, continue til the end to consider youas presentinthe class. IMPORTANTLY, you need to make the class more Interactive by responding to Professors queries inthe session, ‘Whenever Professor cals your number / name, you need to respond, otherwise itwill be considered as ABSENT + Speaking broadly, a file system is the logical means for an operating system to store and retrieve data on the computers hard disks, be they local drives, network-available volumes, or exported shares in a storage area network (SAN) Whatis a file system? e Syren egal VED arn a Da TERT Whatis a file system? _ 8 What is a file system? 8 A file system can refer to the methods and data structures that an operating system uses to keep track of files on a disk or partition * Linux keeps regular files and directories on block devices such as disks + A Linux installation may have several physical disk SZ Units, each containing one or more file system types SSS . i i i Disk blocks are composed of The same track on each platter Partitioning a disk into several file system instances Sk Heck te compose ot es atnder track oylinder makes it easier for administrators to manage the data Se 0" partons are groups of stored there ‘contiguous cylinders, ap Es Cee She ENE — ATSIC theme open VED Fa a a ETT ‘The UNI File System Whatis a file system? . ln neneneganteamenl ‘bares cotaned ono Dosen [Comer + File system instances reside on partitions ‘in| Common programs shared bythe sytem, he system administrator andthe wes. + Partitioning is a means to divide a single hard ec Taraare Wich are repented as drive into many logical drives “ra Miter ten contig ae ir ola ats + A partition is a contiguous set of blocks on a drive | “hoas~|Henedivensiwaftiecommarmene that are treated as an independent disk oo ep se eee rea + partition table isan index that relates sections [itm reeuaniarax ean eae nao “ : Temporary ae a ye em ded pen one, Bana of the hard drive to partitions ie oa eg ee Stree alae and npr les aed ws a sear |te mal qa th re pss ae ortega sore 2 0 Sens resaceig VS0 Tisha Rana une STAIRS Pag VS ‘ivr mano ec TO UNIX and POSIX file attrib te + Filetype 1 Type of file ‘+ Access permission : The file accets permission (owner, group, others) + Hardlink count: Number of hard links of fle + uo + aio + Filesize + Last access time + Last modify time + Last change time ! The time the file was last modified ‘The time the file access permission UID GID or hard link count was last changed + node number: The system inode numberof the file ‘+ Filesystem ID: Thetfile system 1D where the file ie stored 4 Ses RE = VIE a TI File System Implementatidilalal (Disk Layout) A Possible File System Instance Layout 42 Sptens regancig V5E aa ann Uo OTT INTERNAL REPRESENTATIONOF lit FILES + Inodes + Structure of a regular file + Directories + Conversion of a path name to an inode + Super block + Inode assignment to a new file + Allocation of disk blocks + Other file types 2 Sten rogaine VSO ar eno esa OT File system layout 8 The central structural concepts of a file system type are: ~Boot Block - Super Block -Inode List -Data Block sytem gang "VEE Super Block Boot Block i i Boot Block: omer - Occupies the beginning of a file system Deis indes tet ees = Typically residing at the first sector, it may also ~Howlarge Ris contain the bootstrap code that is read into the TS ee newine tens loka —_ ~ inden af the nant free inode tv cine free inode lat ~ Although only one boot block is required to boot = Ube Heide for the free block and free inowe late the =) Mestnl evaiy’e-spstni hiap Cone a boot =A flag Indicating shat the super block hes block Caen =" [ero [ieee Spe REVIT a TS TT Se ENTS a NTT Super Block BB Inode List . Inode List: * Consists of following fields An inode fs the intemal representation ofa file — Size of the file system contains the description of the disk layout ofthe file — Number of free blocks in the file system data ~ List of free blocks available in the file system file owner ~ Index of the next free block in the free block list permissions — Size of the Inode list ~The inode list contains all of the inodes present in — Number of free Inodes in the file system. an instance of a file system. S22 eee, EES ene ERVIN TT ee Data Blocks Data Blocks: Contain the file data in the file system Additional administrative data -Anallocated data block can belong to one and only one file in the file system Sper ee REVI a NR TTT tm system has an inode table which keeps track ofall files Inodes in UNIX uN + Each entry im inode table is an inode record + Tnode record contains all attributes of file including inode umber and physical address of fle + Information ofa Mle is necessed using its inode number + Imode number is unique within a file system A file record fs Identified by a fle system ID and inode number Inode record doesnot contain the naine of the Ne + The mapping of filenames to inode number is done via directory les 21 Stems rage Vi era What is a file system? So what is a file system? -A file system is a set of abstract data types that are implemented for the storage, hierarchical ‘organization, manipulation, navigation, access, and retrieval of data Spans FRE VSO ae a a The kernel deals on a logical level with file systems rather than with disks The separate file systems that the system may use are not accessed by device identifiers Instead they are combined into a single hierarchical tree structure that represents the file systems as one whole single entity What is a file system? 2 Sten og SaRIAE-VEU $$“ srr an ac a RT File system consistency check i + During bootstrap operation, all the file systems are checked for self ~ consistency and attached to the root system UNIX command: fsck + To find the available space on the mounted file systems use UNIX command df (disc free space) yan lg VED Fi nb avn ne TTT 1/0 in Unix - Devices a | + 1/0 system used for — 1/0 devices, Network communication and Virtual Memory (Swap space) [SysPgm@labserver ~] $ Is -1 /dev/ — Observe File types + efor character device driver + b for Block device driver + Hor symbolic link — Observe file size [Replaced by 2 integer numbers] + Major device number and minor device number Sesenaroganing Vid Kernel I/O structure systema interface othe kernel | seve fe cei ] ‘YNODE ier | woke = xis [__ beatae 5) ste cooked] aw ctr wy | } as | us proves | [ss sia | ok sex —t aod | ae ton tele | dicnin| 2M dines sks dvr tars die es the ardvare 24 Sen RI =VIS Sa HT 1/0 in Unix - Devices . FB * Block Devices and Character Devices ~ Character devices — i.e. devices treated as byte streams + Include terminals, line printers, main memory apart from disks + Unstructured or raw Interface ~no butfering by kernel. Block Devices ~ i.e. block-addressable devices + Eg, Disks and Tapes + Structured interface ~ block-sted accesses - buffering done by kernel. 2 Sem omg VEO a an ae TT - Devices BB Device Identification — major device #, minor device # + Major Device # refers to the type of device and hence is sed to identify a device driver. = Whetna device le opened, Linux examines its majo urniber and forwards the calltothe driver registered for that device. + Minor Device # refers toa specific device and is interpreted by a device driver. ~ Minor number is used ta ident the space deve instance it ‘he diver contro mote than one device ofa type. 1/O in Ui — Block Device table and Character Device Table zr Sern EVE ST File Systems and inodes | ‘The i cocnand res the inode och les abs, and ican Ist mo of hem ug suible pionsOnc ofthe sth nod) option thats oo thine uber ofa ik 4151 tuets ES rerern 1 mer S181 din 31 U1 tales ‘Theft alec the inode mb 905. No othr fl se sane file pt on he his umber uns the fii emned Whe that haps there wil alloca thisnade number toanev fi ret Tide cons alata ecgtbefirane Trefiare teint drt house le Tenderers ant rete tela ane ayy ispestion sees seid 2 spe ree VEO an a TTT File Systems and inodes ee | ‘Every Hes aociaed wih tale tht coma hat ou cool oly eed know abt fie—excep te rame and cote. This abl i cel the inde (rene ram inde nove) ands cee bythe inode number. Teno contin he allowing abe of fle: «let gl ers eis) + Fle prisons (he nine pimisions and ihe mo) 1 Number fink (he mbes he fi hs). + The UID othe owner 4 TheGID athe gop omer + lesen + Dit sd ols acon + Duc anne of lites + Dita :ine oft change the ode + Anareay of pois that hep tack of lik locks wed bythe fle yen rovancing Wid BEF ai ims Hard Links i Whyte eee ot toed inthe oo So hat afl can hare mip eames Whe tht apes, neste les mare than oink Wecan then acres thi by any ois inks. nani pride asinge fle bae oe thing in camo; thy al ae the same node nembet ‘Thelinkcounisdplayedin the cond column ofthe iting, This cua noma (shown inthe pei iting), but the alli les baer inks: 16 Ju 13 2:35 tacks 163 Ul 13 21:35 restore sh ‘Al abt sen tobe ideal, btu silk copii ae diferente umber sth ink couathut senso sage bat thee” arlinkd teach tex Bat is canon beconirmed by using tb i opin 1 415-14 tact sh restore. sh ‘an rare 2 haar RM snare 2 haar svtene Ronse “V3 Dhar hear etal etal ret etal 165 al 13 2:36 bah. st 168 ul 13 2:36 [Link] In: Creating Hard Links . B “Aleintinkd wih in comand hha wo lenamcss segues Ta ‘cn crea both Aad na fink suse ac) a as yon in the one wey (The fling command (had) inept ith epg: 1 emp. 15terloyee mplayer mt xi Tht option oss thes hte the sae inl nami eine ie al one andthe sme fle etal 9 ray $0858 ep. 3t mel 15 Moy 40858 expoyee ‘Thnk couse mich is noma ea une ss shown we ci aka hd fname, dat, and incest arf ik othe $n ele enpaat: Is -1 ot 2B -rornire 2 kar 618 sori Samat metal 915 May 4098 emp W618 srorotrs urur metal SIS May 6 U8 pt W518 snar-are 3 turar nets! S15 My 40958 ence Sree eae VISE LIMITATIONS OF HARD LINKS a | 1. We cant have two linked filenames in two file systems. 2. We cant link a directory even within the same file system This can be solved by using symbolic links (soft links) ‘Stems oraseing VSO BP bana na aT ‘WHERE TO USE HARD LINKS _ Bw 1. Indata/ [Link] input_files 2. Links provide some protection against accidental deletion 3. Because of links, we don't need to maintain two programs as two separate disk files. selene rasensine Vad Fin eno Does ST Symbolic ‘Webi sen bow inks kr ws have mkile names fora Be Tes ins are fen called hard links, and eo aos « Youan thre told leu into fess In aberounl,oucan inka fkmane inthe isle eo ahr he beef sem, «You cai dcr een win he sme flee. histo timitson was oem when symbolilinks mae then Uni no, we ae ide fils the cates (otnary dicta nd erie) The splint fou fleypecomsidercin tise Unltd: hadi pbk oe tbe sts, butsinpy rvs the pathname ofthe fh rly hs the omens Being mor fel a ‘pmbscink sao kagn ssf lnk Windows shorcus ae mor ke sybalcinks, Systems regianne VI ‘iar ano aac be Symbolic Links _ 8 conan ets yb inks al, espa you ive owes opto. Tis time Tsing tells jou diferent sory. 5 ns nate note 51s i ote nate. sye 50 meron L har grap Feb 16 1642 ate {982 Irma 1 marr "Feb Ib 1:07 mote su > note ‘i can emi mba inks bythe charac (seen in be permissions el The pointer ation > nate sot that tee contains the pathname the iename oe tte, snd nto. sy tat 2a contains the dat When ou use cat ate. you det acually ‘open the symbolic ak rote, bathe leche ink points. Observe thatthe sie oft syboi aki his isthe leagh ofthe pane icons (rt) eto you wae that his ine we indeed hae to les, and they ate wt ides, agro. syovon sft us muchbecase we can cal eet tela Duif we reove tote, we wld se the flecotining the dita a chates tem ould point onesie fle snd become a dling symlink L IMP Note to Self 8 Cie Thank you QaA SEWIZC327, Systems Programming Lecture No. 4 IMP Note to Students 8 ItIs important to know that just login to the session does not ‘guarantee the attendance, (Once you Join the session, continue til the end to consider youas present in the class. IMPORTANTLY, you need to make the class more interactive by responding to Professors queries inthe session. Whenever Professor calls your number! name, you need to respond, otherwise twill considered as ABSENT Sens rosa VSO IMP Note to Self Fa Start Recording ‘nem ropuninbe VR0 BF cio TET Agenda | > Stat, df, & du commands, > 1/0 in UNIX Block and Character devices, device drivers, 0 ‘queving and interrupt handling > tnedes > structure of a regular file > Conversion of given path name to inode in UFS with more examples. srtems toga V0 Ba Sao ec NTT stat is a command which gives information about the file and filesystem. Stat command gives information such as the size of the file, access permissions and the user ID and group ID, birth time access time of the file. Stat command syntax of using stat command: stat ~options filenames 6 Specs Me BREE VI Gl A TT Information we get from tot + Fila: The filename, tthe provided file is a symlink, then the name il be differant ‘Site: The size ofa given file in Bytes. Total numberof alocated blocks to the file on hard disk + Filetype: Regular les, special les, directories, or symbole Hnks + Devic: Device number in hexadecimal format. + node: inode number of the fle + Links: Number of har lnk ofthe file. + Aecess The lst time at which the file was accessed, + Modify: The last time at which ile was modified. + Changer The lst ime the at which fies attribute or content was changed, + Birth: The time at which the file was created. 2 Stat command -L, dereference follow links -f, ~file - system display file system status instead of file status -¢ ~format=FORMAT use the specified FORMAT instead of the default; output a newline after each use of FORMAT z Ses SRE VET aan du command displays the number of blocks used for files. If the File parameter specified is actually a directory, all files within the directory are reported on. If no File parameter is provided, the du command uses the files in the current directory. Notes: + Files with multiple links are counted and written for only one entry. + Block counts are based only on file size du Command du Command Flags 2 Displays disk usage for each file specified, or displays the individual disk usage for each fil in a directory. -k Calculates the block count in 1024-byte units rather than the default 512-byte units. 1 Allocates blocks evenly among the links for files with multiple links. By default, a file with two or more links is counted only once. + Displays the total disk usage for all specified files, or displays the total disk usage forall files in a directory. syntax of du command 4ujornony. rte, 0 sptenaraiaaning VB Fn nh ane oe TERI df Command To display information about /boot filesystem, af fooot We can display information in various block format. Fi AM-byte blocks, run: df-m f-m foot Show statistics about the number of free and used inodes Pass the as follows to display index node (inodes) info: df fic dint 2 Sem reine V0 $i oan ena SaaS instance see df command (short for disk filesystem) is used to show disk utilization for a Linux system. Display information of device name, total blocks, total disk space, used disk space, available disk space and mount points on a file system Display all the file system, use -2 option. ata Use -h option to display size in power of 1024 df -h fhome/WASE Use -H option to display sizes in power of 1000. df-H /home/WASE Use -T option to display filetype a-T Phome/WASE df Command Sens re VIS Sa a SH TT Each file is represented by a structure, called an inode. Inodes Each inode contains the description of the file: file type, access rights, owners, timestamps, size, pointers to dat blocks. The addresses of data blocks allocated to a file are stored in its inode, When a user requests an I/O operation on the file, the kernel code converts the current offset to a block number, uses this number as an index in the block addresses table and reads or writes the physical block. 2 stem we ‘iar ann ec Doe Directories BB Links Fa * Directories are structured in a hierarchical tree. Each (directory can eovilin Hes and scbelrectores. The inode contains a field containing the number associated with the file, * Directories are implemented as a special type of files. Actually, a directory is a file containing a list of entries. Each entry contains an inode number and a file name. When a process uses 2 pathname, the kernel code searches in the directories to find the corresponding, node number. After the name has been converted to an Whena linkis deleted, i.e. when one uses the rm command to ‘Adding @ link simply consists in creating a directory entry, where the inode number points to the inode, and in incrementing the links count in the inode. , doles remove a filename, the kernel decrements the links count ae ay calecuenteguens nant ime memery ands and deallocates the Inde if this count becomes zero, “ a A TS CRT Sent ge VES a TT Device special files . = 1/O Queuing and Interrupt | + Devices can be accessed via special files. Adevice special fle -—* Each device driver manages one or more does not use any space on the filesystem. It is only an access queues for 1/0 requests pe the descr cee = Must be synchronized. * Two types of special files exist: character and block special — Multi-process synchronization also required. files. The former allows 1/0 operations in character mode _-—«* ‘Interrupts are generated by devices while the later requires data to be written in block mode via = Signal status change (or completion of operation) the buffer cache functions. = [Link] invoked thru’ a glue routine that is responsible for saving volatile registers. — ISR removes request from queue, notifies requestor that the command has been executed. + When an 1/0 request is made on a special file, itis forwarded to a device driver. A special file is referenced by a major number, which identifies the device type, and a minor number, which identifies the unit. 16 2 Syren romana Va TRAN «tens guns -VED $s an es RT 1/0 in Unik Block Devices and JOLLY 1/0 in Unix Block Devices and ONY + Entry Points for Block Device Drivers * Disk sector sized read/write pe, ce archon GRE = Conversion of random access to disk sector ~ Dene sell og ty of nein (eesHlevce read/write is known as block I/O reer .on opener meat oer cy wna ce Hone Z — Strategy — 1/0 request initiated and returned; if the Kernel buffering reduces latency for multiple Taqilestisaynchronous'thersamosss ust: Bick reads and writes. = Eg. on bread or bwate system cals pertomn YO if data not ie * Consistency maintained through forced writes = Close — null op for disks using sync system call. (also refer to fsync.) + may release device if it operates in a serialized mode + mark eof f writing and may rewind for tapes. e 0 ‘Sema rorenning VIO Fn nh ane oe TERI Sete eg ag -VED $s Fi ooh bans SS File System Algorithms | inode | waned | + Contains the information necessary for a aa Cie] GE ee process to access a file ict iput bmap + Exits in a static form on disk and the kernel jiblk rele | bread breads bwrhte ° " ° * reads them into an in-core inode Every fle a UND sytem has a unique ine. Processes interact with Hes using ‘wall defied sytem cals. The users speciale wih a characer sting winch = the le’ pa and then th system ge! the mace wich s apd othe le wich ‘carespends toe path 0 2 sytens resume VEO $i nROTETITT—_‘Sntne egIa "VEE ——$~“ sarin amie TT inodes Disk inodes consists of the following fields. file owner ideutiier -file type -file access pertnissions file access times number of inks to the file table of contents for the disk address of data in a file filesize ans AE VEO an The algorithm iget allocates an in-core copy of an inode. Accessing Inodes IF the inode is not found on a hash queue, it allocates an inode from the free list and reads the disk copy into the in-core inode. It already knows the inode number and device number. It calculates ‘the logical block number on which the disk inode resides according to how many inodes fit into one disk block ‘The formula forcalculating the logical block number is: block number = {{inode number - 1} / number of inodes per block) + start block of inode lst 4 Sytem roming VSO i Fa a unaware TET inodes core copy ofthe inode contains the following fields in adcition tothe flelds of the disk inode: status ofthe n-core mode (The inode flocked a process waling forthe ‘node to become unloced) logical device mumber of Me sytem that contains the fle inode number are stored in a linear array on disk, the Kemet identifies the number ofa disk mode by its position in the array. — poiaers to other in-ore modes reference coum, indicating the namie of instances of the le that are active (oe as when opened), seme naa VED Fa Ba TE «The Kernel locks the inode iF it has not heen already locked «The kernel decrements inode reference count «The kernel checks if reference count is 0 or not = Ifthe reference connt is © an the number of links to the file is 0, then the Kemiel releases disk blocks for fle(algorthun fee), five the inode(algovidim ities) + Ife file was accessed or the inode was changed or the file was changed , then the kemel updates the disk inode +The Kemel pus the inode on free list, = Ifthe reference count is not 0, the kemel releases the inode lock Algorithm iput put (node no) rekcassnOON structure of Regular rile ME — lock inode if not locked — decrement inode refernece count if (refernce cou In UNIX, the data in files is not stored sequentially on disk. If it was to be stored sequentially, the file size would not be 0) flexible without large fragmentation. {inode tink==0) In case of sequential storage, the inode would only need to = fre dk block store the starting address and size. Instead, the inode ~satieee stores the disk block numbers on which the data is if file accessed or inode changed or fle changed) arg ar peda ai Seat ganen 1000 blocks, the inode would need to store the numbers of 1000 blocks and the size of the inode would differ according to the size of the file. + put inode on free ist — Release inode lock tn 2 ‘Stem Proioning Vi FASTA Stem RT VE ar aan a eB incase 8 Structure of a Regular The inodes have array of size 13 which for storing the block numbers, although, the number of elements in aray is independent of the Fields of Disk inode continues plore sttoieat: —Table of contents for the disk addresses of data The fist 10 members of the array are "direct addresses", meaning ina file that they store the lock numbers of actual data. ‘The Lith member is "single indirect, it stores the block number of the block which has “lect addresses’. ‘Tho 12th member is “double indirect, it stores block number of a single indict” block 13/15 entries in inode table of contents + 10/ 12direct * 1 single indirect ‘and the 13th member Is “uple indivect, i stores block number of a eee “double indiract" block +L triple indirect This strategy can be extended to “quadruple” or “quintuple” indirect addressing, 20 Spenstaguining VEO tina oanatanmaivaaTGcnTGUR Te Speman VED ‘is ra ann ec DOI Structure of a regular fic UNIX (4K bytes per Block) ee | ata locks a 0 rene een VSS Saf aes awe SETI Sm hopaadng "VEE Fins ano ie aes SOT structive of a regular tle MAM Structure of a regular ste ‘Suppose System V UNIX + Processes access data in a file by byte offset and view a fle as a ‘Assume that a logical onthe fle system holds 1K bytes and that stream of bytes ‘a block number i addressable by a 32 bit integer, then a block + The Kemel accesses the inode and convert the logical ile block can hold up 10-256 block numbers {ato the appropriate disk block + Algorithm baep 10 direct blocks with 1K bytes each=10K. bytes ~The kemel calculates logical block number in file fom byte 1 indirect block with 256 direct blocks= 1K*256=250K bytes wr 1 double indiret block with 256 indirect blocks ~ The kemel calculates stat byte in block for YO DAK PISO Eyes ~The kemel calculates number of bytes to copy to ser £ uipte indirect block with 256 double indirect blocke~ ~The kemel checks if ead-ahoad is applicable, then marks inode TRMSASRS ~The kemel determines level of indirection 2 2 sem va TANNA Seema hogunciag VSS $s amine NTT Structure of a regular fle MEIN conversion of Path Name to an MOSo = While it’s not at necessary level of indirection, The kernel parses the path by accessing each inode in the ‘the kernel calculates index into inode or indirect block from path and finally returning the de of the required file. laalioskmnberin fe ge akbek manbrfomineds Eyer proces has a current directory. The current directory on ee of process 0 is the root directory. + Ifthe is no more levels of indirection the Kemal st a For every other process, it is the current directory of its conversing + Otherwise the kemel reads indirect disk block(bread) and percegroe ee adjusts logical block nuunber in file according to level of Later the process can change the current directory with the indirection system call chdir. The inode of the current directory is stored in the u-area, a rt 26 sntene Ropanig 32 SEALANT Stns gla “VET Se Sa Conversion of Path Name to ante Path conversion to an inca Kernel converts path + filename to the file's +f (path name starts with 0%) ode ~ working node" oot node + ols kernel parses the path name one component ~ woringnode=curen rectory inode ata time + while (there is more path name) All path name searches start from the current a ee directory of the process unless the path name = Hlcomponent matches a entry in deny start with ‘/’(start from the root). pn The current directory inode number and Root «worn naesnee ot mathe cmponent = eke return no node + return (working inode) (working inode) 3 sytem va i Fa a unaware TET Stem Pion “US Fi an aac RT inode number are available with the Kernel = IF path name starts fom root, then the Kemel assigns root inode(iget) to working inode = Otherwise, the kemel assigns current directory inode to working inode Algorithm namei = While there is more path name, the kernel reads next path name component from input, and verifies that working inode is of directory, access permissions OK + IF working inode is of root and component is ‘kemel checks whether there is more path name or not + Otherwise the kemel reads directory by repeated use of bmap, bread, brelse Y, thea the 20 sytens rowan V5 Fn nh one oe TERI Several pieces of data are needed to manage open files: = File pointer: pointer to last read/write location, per process that has the file open — File-open count: counter of number of times a file is ‘open — to allow removal of data from open-file table when last processes closes it — Disk location of the file: cache of data access information — Access rights: per-process access mode information Open Files sytem va i Fa a unaware TET Path conversion to an inode + Ifthe kemel finds a match, it records the inode number of the matched directory entry, releases the block and the old working inode, and allocates the inode of the ‘match component + If the kamel does not match the path name in the block, it releases the block, adjusts the byte offset by the number of bytes in a block, converts the new offset toa disk block number and reads the new block 2 Sys asf VIDS a Descriptor management and sehices The file descriptor is used by kernel to index into descriptor table for the current process to locate file entry or file structure file entry provides a file type and a pointer to an underlying object for the descriptor See ea VIE ‘is ra ann ec DOI Superblock contents ‘ize ofthe fle system. + number of free blocks in the filesystem, + list of free blocks in the file system. + pointer to the next free block in the free blocks list + size of the inodes ls. + number offre inodes inthe fle system. + Istof free inodes inthe file systom. + pointer to the next free inode inthe free inodes list. + lock fields for the free blocks and free inodes list. +a fold indicating whether the super block has changed. The kernel periodically writes the superblock to the disk if it had been ‘modified so that its consistant with the data on the die Sper Pernining VIO Fn nh ane oe TER Inede astignment to a new Ao 3. Inode list in super block is empty lock super block ‘get remembered inode for free inode search search disk for free inode until super block full or ‘no mote fee inodes(bread and brelse) unlock super block super block becomes free ifno free inodes found on disk . stop otherwise, set remembered inode for next free inode search + Ifsuper block is locked, sleep 4“ Sens resuming VIO a a an Uo a TO Inode assignment to a new HC + algorithm alloc : assigns a disk inode toa newly created file -super blocks unloched There areinodes in superblock inode Hist and ino is free ‘getinode amber rom super black inode let ‘eerement le system fee inode count 2. There are nudes in superblock inode list but inode io wot eee set inode nuasber from super block inode ist, et inode Geet) eaniedsamk release inte (pet) 2 Sten 0 BREE =VIE a Bal TE hank you QA IMP Note to Self i ay PCM eee) Cer eet a IMP Note to Self | Start Recording CSIWZC327, vets Programming Lecture No. 5 > Its Important to know that just login to the session does not ‘guarantee the attendance. » Once you join the session, continue tll the end to consider you as presenti the class. > IMPORTANTLY, you need to make the class more interactive bby responding to Professors queries in the session. > Whenever Professor calls your number / name, you need to respond, otherwise it willbe considered as ABSENT IMP Note to Students oe + To write and edit some programs and scripts, we require editors *+ UNIX provides vi editor for BSD system ~ created by Bill Joy + Bram Moolenaar improved vi editor and called it as vim (viimproved) on Linux OS vi basics 6 Stems reguneii VE) $~“ tia aan oa TT Agenda > The vi basics > Input mode — entering and replacing text > Saving text and quitting — the ex mode > Navigation > Editing text > Undoing last editing instructions > Repeating the last command > Searching for a pattern > Substitution — search and replace > cp, cut, paste commands. 5 The vi Editor The three modes of vi Editor: [Link] Mode: The default mode of the editor where every key pressed is interpreted as a command to run on text. To copy, delete text & navigate, this mode is used. [Link] Mode: Every key pressed after switching to this mode actually show up as text. To input text, this mode is used. [Link] Mode(Last Line Mode): The mode used to handle files (like saving) and perform substitution. The vi Editor REPLACING TEXT Insel wl becommandat yu ene apd th Coad ‘Me Wea ky ofthe apt Maes pes ites apron cen bt saequat key ceyesionsd. el coe te illoig comands « Ietand ape Tan Replace, sand), + Open lie (oad) Abas in mid that afer ou ae compe ext ety sig ay of these omens (ext) yuna tthe Conmand Madey sig [Ee Nowofdeecommanécin akobewed witha peat ogh ol ced eit wih ay sone of em. . INPUT MODE ~ ENTERING AND . Sg oer 6 a ROE 0 ree DE ne een: . INPUT MODE - ENTERING AND nervene rl REPLACING TEXT insertion of Text (anda) Insertion of Text at Line Bxtremes (land Al The following Ede forks process the vi mditor 1 ‘full-screen [Esc] the Wi full -screeleitor 1 [Esc] aeaaT jillthe fortowing code forks a process ‘the vi full-screen editol A AV[Esc] ay a Vink of ex/Fae} [1 The following code forks a process *fl the vi full-screen editor, Tink oF elf See Using I and Ato Create a Comment Line in aC Program 10 0 Sytenstaguining V0 tin oes maraaTSSOTCURRIOR ——Sysens agi “VE0 wih nso aac TPT INPUT MODE - ENTERING AND noe vi and ex arfg one and the same editor " is due to William Joy/Esc] vi and ex are one and the same editor It is due to William Jol Opening a New Line with 0 2 Syteme rennin V3 INPUT MODE — ENTERING AND REPLACING TEXT input Mode Commands Command Function lesen eto lef of cursor (sing te sified right) Appends texto right of cursor (Existing ext sified eight) Inset at beginning fine (Existing tet shied right) Append txt at endo line Opens tine below Open tine above Replaces text fram cursor to right (Existing text oerwriten) Replace single character under cursor with any number of characters a 1 A 0 = Replaces single character under cust with ch (No [Ee required) 5 s Replaces ei line INPUT MODE - ENTERING AND repLacindl TEXT Replacing Text (r, s, Rand S) his is the vi full-screen editor rT fhis is the vi full-screen editor Replacing a Single Character with r 2 tens reas VIE ain ano aes aS ex MODE — SAVING TEXT AND corn Saving the Work (sw) Webanealedy eth ex Made onnand tose ufkr anit eine Fae eaten esos wih i, oo must abl tse te bu and ean in the it Fro tet, tin youmst sth -vcommand to we the led Eater, mich appearson teat nett ren then an inal Ee: Ene "stn, 8 lites, 25 characters Youcan ar conian your editing wrk mally cma sure th you exe tisconmand teu Wi the comand you can pal spec aiken wl Tact cas the cont esr wien toa fie 15 sytem Popancing “V5 ‘ier an ec be ex MODE — SAVING TEXT AND curve ex MODE — SAVING TEXT AND A | Saving and nd ‘Teper onmud ase Conant dessin ein HOR, et at ig pce nd quit eg mde oes a tose and uit elitr, eto teste, sete ei commands Ther it omand dest a Ean Wot wo fir iamaned Smet, 8 nes, 38 characters * = fab bas ey mechan hat pees you ro chr scien iyo ae moe Tove skouseamwsreandguthecti Burtt euian knead iam ilu) in ayy Th follwing sage pial we outs: onmaode fre Ho ite since West ange (uit! overs) Sie recat wei sieideplianh achirgeaslt piney ted iti etrose DZ wCommand Med rand oH yn cng and slat andi ang hen ee sal gore al changes made od gus “6 7 ‘Spptems Programming W530 "BER Pint Ors be Uy wae Sean SIGE A 10" ‘Sytem Programing -V30BTSPion ermine Unbeniyanis Son DO ex MODE — SAVING TEXT AND cummed ex MODE — SAVING TEXT AND corn Writing Selected Lines ‘Writing Selected Lines ‘The command saath var of xing th cx Mode instrcon iL, $e¥ 20D There aetna speci stcance—the dt and. The do eens profiel by one orto adres separate by comma ‘The command ‘thecumetlin while represents the st line ofthe fil, You can use chem singly rin combination: sv tempfile Saves cnet hie achere cursor td) sn aes Thief nscale int mae ee sa tele Ses cen ie rag end ‘I 5 oucan se singe ie wel soe ing Dtugh thelr. aca sea singe “a Itenpfitexss ands wrinle by ou, ius eather naing ‘iors isi se fi “nile Fleets = we Vt tne! to oente nh aS a —_ oe ex MODE — SAVING TEXT AND curve Escape to the UNIX Shell (sh and [ctr-z)) How dodo you eit and compile your C progam repsedh? You need to make a emprary ‘aie tothe sell cu th command. Tere ae two was the ist metodo we the ex Moje command st sh 5 You hte’ uit ye ‘Thi eus sel pam, Exec eor any UNIX cand bee and hen return sig Cr ore, Doa mae the isa of nnn nce, aul te ae wo insane ofv4—an undesahlesuaton fours upp cont rich mos shel) you ean arent cure sion, Jus pes (Cr and oll been hl rom Ren your commands and chen use fg coma to reurto tke eae oa Sptenategacg Va Fn ane oe TERI ex MODE — SAVING TEXT AND cummed ‘Save and Exit Commands of the ex Mode tion Saves file and remains in editing mode Saves file and quits eng mode Arahove Like Save Ar. in Microsoft Windows [As above but overwrites existing fle ‘Quits editing mode when no changes are made file ‘Quits editing mode but afer abandoning changes Writes linen tm? wf but Isat ‘Writes earrene line to fle butlsal Writes last linet fle buts Runs omd command and returns to Command Mode caper to UNIX shell, Recovers file fom aera a a ex MODE — SAVING TEXT AND A | Recovering from a Crash (recover and ~) ‘reas an and wil ape, The power can goo aig work nae. Home dot etic srs of bfrifratin in hide sap fi nth Wf eae tit Aloe ex apne ihr animpopeshtdva park i ape ein nite ompin he ne ine ou ioe te ae fle Thecus cuninsone abigail abd towvetherth cx Mad command ser oi ones much nape, Afr you re eth, area lok atthe ur cots and aioe sce othe anag: ntl rie eter sees in, seth be and north xe if does dota oi mm Splem agermig VO 2 Command MODE — nave ‘Movement in the Four Directions (h, |. and pare pice prvi be e hand To more the cao in the ou dein, Tes keys ae reek sara demo dpb Vis pete me ura by one postion Ue these ys foc moving the curse eral ke Movescusae op J Moves cursor down “Tome the cr alog fn, ste commands h Morescarorle 1 Moves co ight ‘ystems reine V3 ‘ier ann ec be Command MODE — nancalon— al Movement in the Four Directions (h, | Kand |) Ua takes you right and h takes you left Relative Navigation with hj, kand 1 24 ‘ssens ong "V0 Fn nh anc oe TERI ‘Command MODE — nav ‘Moving to Line Extremes (0, | and) ‘Mongo the begining or endo iss commen iene. Thsshndedby he keys ad. To moe othe it charac, ae 30) monet amet 39 The aes eet fit nd sing that, you can psn he ono o cern cen To sion he cur onc 31, 3 ‘We wed asthe aes inthe er Made esa the lint fle The same sl inthe Command Male pesca the end fine. To more tthe endothe care ing, se 5 Nowestoed of line 2 fu) | srteme iacannine “Vie Bin ab unarrorSTTETST Command MODE — navicn oving y ne acces tay xa yorl en ned oe teal ie. destin ase gnon si hich a ede into was depending onthe key pres I your cor a suber of wc ay your sie postion, you an the ord angina commands othe ed Thre bc onan 1b Moresbactn beginning word gxample © More rua oendof wor ‘sb takes the cursor 3 words back Mores frvard to begianingofward 7” "4Het the cursor 3 words forward A repeat factor speeds p curr more aloo ie. Fr example, $b aes the cursor ve words back, we tas the cur three words oan A ward Beis imply 2 sing of alphanemerccarcte athe_(ooderso) Bs isone word ois profile tepipsthre rds the hyphen by deintion comes 2 word to. Stem rganaine VS 2% Command MODE — nave Moving to Line Extremes (0, [and $ ‘Nevigndon Along Line 2 ‘Sites neqansing Via ira Command MODE — nancalon— al Scrolling (/Ctr-f], [Ctr-b], [Ctrl-d] and [Ct Faster moment can be achieved by sclling etn the window sing te contol es, Theo command forcing pge at atime are Gel) Scrlsforeard ICe-5)—_Scrlsbucard You can me te epeat fc ke in (Co, scl 10 pues and vig fer in the proces You am scl by half pge aswel [Cxid)—Scolsbalfpge fread (Cra) Scola page backward ‘The nga facto cn slob sed here Sptemargunining 3D Fn nh Uae oe TER Command MODE — conc Editing Text ~ Deleting characters (x, X) This te the vi fullgecreen editor from UCB Thiet the vi fullscreen editor from Uc Y Peer spaces back This 1s the vi millscreen editor: from UCB es This is the vi Micreen editor from UCB Deleting Text with x G Sytem roaming Va i Fa an una oar Command MODE — navicd Oe "bs olite Movement (oT Upon sat, vi dp the tal sure of ince athe lat ine. any Hin you cn eh (Cr wa the eat ise suber *Yetfoassit [Read ony] hve 89 of 19 -9t-- ‘The cmos on in §9 (9 of 170), and his ed-prtted ile as 19 nein al. Comp li inte ne number inhi eer message awed use th command with he ing uber a epee co tact the lng ines. Tb mae tthe 4th in, se 6 Geeta ine number #0 andto moe tothe beginning ob fle, we % Gem ne mamber ‘The cnd othe lee reached by singly sing ‘ Goer endof fe Syren gag -VA8 BaP Bere ae NTT Command MODE — come Editing Text ~ Deleting lines (dd) This te the vi editor from UCe Te ts superior to ed, and friend] ter too Tt 43 due to Wit Ttam doy It {3 Slow in getting started but 42 quite powerful 1« B's superior to ed, and friendlier too Tt is due to Wj11iam doy Tt 42 slow tn getting started but 1s quite powerful tr Slow in getting started but is quite powerful Deleting Lines with dd 1 Systems rearing VS Command MODE — com Feriaute,w core ti. sto ol eto amps te haces and ie dele hed and guitar Moe outcast din stn andthe ro commands 1 sitio deme sti baron > ten ih—stio. bce sti Sic we wrk on pati, sitethe od ‘ght wind" with poste ‘onthe left of the cursor, To pute) kines aie an, youl ed wus te same command eg thane ae thikin tof tlw” and abr cen ep last ew te core ine sod plaestache 2 ‘Stem Poeonming V8 Fn eh Une oe TERT Command MODE — concen Editing Text = Copying Text (y and 2B este tr aking fr coping eh ean why the oper is ane. Tepes area te ane comgared dele with Forinsace, toy oan on ot are fines eth conan W fra ine iy af aren incon 9 ies “Thisyathd eat hasta be place tie ew lation. The pt commands arte sane—9 ad Sine wail enilines recan ol let copied tbo or abr cue ing, and atom ther sigh But spp youned place ante atheed of ante o Aare leit tote ine oad the jth woes ust ead on. sytem tegionnng “V0 Hs Pi barns en aT Command MODE — come Editing Text) They use operators, such as, d delete y yank (copy) dd delete entire line yy copy entire line 6dd_ deletes the current line and five lines below Ea Ses Pog SEREE= VET ao ma Command MODE — come Editing Text ~ Joining Lines 0) Ind reso youn cast alex ine ying cunt the noe and resin [Dele Ths woe we int Tithe cae neand ie ine illoving igue J flog es wih arent one remoresthenelie characte beeen theo inst pth ine ow icing here, fansite the maximum aha our eto nsapport. cule bey 20 (sin Sol or united (a in) ‘Tose the pte pose attend ft rious sion fon ple alin below the caret fnew joucan sed jin oles ‘sens rong V8 ‘ier ano ec bo Command MODE — eorivcOe ~ ™Gndoing Last Editing instructions (wand U) you hae imereny made an edtng change that you sould hare, v provides thew command tun the st change nade. Bele oad amhig ele rene thelacange you rade tthe bute ypesing ' Maa ain Conmand Made es Bt ify (When rer feng changes have ben nade toa sige ing als yo discal al hag beirut cy fo te lin. The comand v Dost masa from core ie frees changes mae tothe cueatling ial afcaos hat hae ee made se he or nas oreo ise, 38 ‘sysemstrginming VIS Fn nh aac oe TIER SEARCHING FOR A PATTERN . = + /search forward + ? search backward Jprintf The search begins forward to position the cursor on the first instance of the word pattern Searches backward for the most previous instance of the pattern n Repeats search in same direction of original search Command MODE — EDITING mr Repeating the Last Pattern Search (n and N) Hr (2 “Ypnae" } echo [ite] then > Bete “You have vot entered the string” exit 1 a ase vy Keto ater th file to be use Yet * read fare t if L! -n "Bflnane" ] 5. then ~=> Heo “tou have rt entered the filename ; exit 2 else Search and Repeat ih anda a Sntere rogaine “V9 arn eno aes aE Command MODE - EDITING TEXT _ B ‘Search and Repeat Commands Command Fonction fp Saks ter i) Sears backval ope pot 1 Repeats seh ina ition lng ich prensa ws ade \ Repeasearhin det oposite that ang wich priser ws mae eevee xo ‘Substitution ~ Search and Replace ('s fs ye anther powers, tha af bso, which sacked wih be ex Made ‘(ube command es ou replce pater ithe Blew something ee. The stant show bow ex MODE saben. pater patna “These pt reseed wih ae stn in ine seid by aes. The abies canbe oncara pio auabes spared by coma For instance, less all ines in fe The most commonly sed Oggi caer on he who rallocuences ofthe pater inane. The llowingexmgle show pi subst command 8s irectr fers Can ao me nae of $ Hers rectors replaced wither lebil throughout he fle Ite paren cae Fond, vw reponds wih the messi Subst tate pattern match failed Ifyou lene ot the g the ‘absitson il ciel out or he fin acurence in cch adds li. systems rogeneing V9 Bae eb ane oa TIER ex MODE ‘Substitution ~ Search and Rey i Iai Saban Seti you may ic weenie a ing I eae, thee (namatuy parm an the bag he en fo srctr omer 9¢ ich ae selec i ta ftlored by seen of acm the met jt low the {tern that requ stton Thersnrnpotooed thee hae Wane 904 ak legen orton 0700 ssiarm septa |erectat [persed 08/1/87|7000 sabes ater sales 21) eewtary oF earteting 09/26/5200 (ores ‘Ayres the ition ay ther pont dae’: This teqacce peat recht tetas in turn he pent ce be sabon ered oh he Bat 2 Setemeegumning V3 Substitution ~ Search and Replace (:s We can perform search and replace in ex mode using :s Its syntaxis, address/source_pattern/target_pattern/flags ‘$s/director/member/g ( Operates on All lines) ‘1,50s/unsigned//g ( first 50 Lines replacing “unsigned” ex MODE with null. 3,10s/director/member/g ( Operates on Line 3 to 10) .s/director/member/g_( Operates on current lines) :$s/director/member/g ( Operates on last line) a ex MODE ey Deletes ine amnd Deletes ne fram m ton nop ves ine afer ine p -mamop Moves Ine ton ater ine p map Copies ine marine p macop copies em tomar ine p mpwitet ‘les ine mio ntoa le het “mw >flet ‘Append ine mtonto fe let set Rear the contents the fl let ‘een caso positon "omens xeentes command a the ‘ulate command is ad at ‘he current eso psiion ‘Seema ros VIS 2 ‘Thee copy commandcopca flor group fies. ceaesan eat image ofthe on dik wha diferent same, The yt eis tas wo lenamesto be sce in the command lie Whe bo re dinacy lth sci cpied the end cag it tds e (at) does tei el fete ceed bere copying aks plc Mant, ‘ails be ocrriten witout oy varning frm eqs, Sbe cru ben yous you dein eran us check withthe 1 cnmand whee oot the le cis fein ne filet be copied denn ante ihe anor ede othe havethe option chosing yu desinason filename Teollowiag eampesows waa of copying filet pegs dy: ca prosjat cut pres cp— COPYING A FILE hap pido unt de ra, hp ets is ame depos cp—COPYING A FILE Options 2 Hiei) The! i opin ea ene esi le Litais, promps rrp: 5. dt wt th cor eerie anit (es)? ¥ eres flan ober espns ; May NIK emma apo te betaine canadien 2nd cami fs in lec. ier stare 04 PS OPER Ayaispomg (4) Coping Dire Se Thsmenes tbe comma an Thee -tomnandbekaes ear opt pgs ws eng tt eit 9 weg Sytem eganaigg "VIS aaa in Ua ar eT pion ed withthe sora etn, (tosh caret cyst desi, ering, tocopy the fle rofl fom fae hare to your caren dey, ou can we tithe ofthe wo commands 9 shri afte Deion fle 6 homshara/, profile . asin ith cane dictry Obrint scond ones pede becauseit requis nek, ‘can lok elo copy nore thane with ingle the oman I that ashes fleunmatbeadectory vinta copy thls chap and chat ‘othe pas dey you hae tse ie tis cp-COPYING A FILE cag chap cas res 4 pens FRAREE VSO Sa File Management Commands . ‘pet fe hes estar Move ne re he? es deste Remove le ff le? es este drecory kar —p dria ‘ony arecory| prot a2 ‘sor move drectory nv dir desir remove dredoy rm -fdrt mi 2 systems pagramming -V30 cut Command + Itis used for slitting the file vertically head -n 5 empst | tee shortlist will select the first five lines of emp.[st and saves itto shortlist + We can cut by using -c option with a list of column numbers, delimited by a comma (cutting columns) cut -c 6-22,24.32 shorts cut --3,6-22,28-34,55- shortlist 0 Spteneogacaing VIB Fn nh ane oe TERI cut Command + To print the remaining fields, we have cut—d \ | -f 1,4- shortlist > cutlist2 tens ozo VEO + Most files don't contain fixed length lines, so we have to cut fields rather than columns (cutting fields) -d for the field delimiter -f for the field list cut-d\| 2,3 shortist | tee cutistt will display the second and third columns of shortlist and saves the output in cuflist1. here | is escaped to prevent it as pipeline character cut Command 0 Sper pe RAREE VSO Ge aan aa + When we cut with cut, it can be pasted back with the paste command, vertically paste cutlist1 cutist2 We can view two files side by side paste Command st Scans raga V0 paste Command i | paste: pasting file (contd. + We can specify one or more delimiters with -d Joining lines (-s) Let us consider that the file address book contains paste -d“[' cutlist cutlist2 the details of three persons cat addressbook Where each field will be separated by the paste -s addressbook to print in one single line delimiter | paste -s-d"||\n" addressbook. -are used ina circular manner Spas FREE VSO Ga a ST IMP Note to Self hank yOu he a) Systems Programming ; CSIWZC327, Systems Programming CT) bee Lecture No. 6 IMP Note to Self 8 IMP Note to Students =a > ls important to know tat ust login to the session does not guarantee the attendance. Start > Once you join the session, continue til the end to consider onus presertn the cass Recording > IMPORTANTLY, you need to make the class more interactive by reponse iia sean! > Whenever Professor cals your number / name, you need respond, otherwise wile considered as ABSENT sytem honing Vie aT Stern Pogrming VEO aa bea TOE Agenda _ 8 > tena « eat-to create, view, concatenate files. + my-mivis used to move one or more files or directories = nin ia rarer agg fl rofl + emp, comm, diff - to display the differences by comparing the files + ehmod- used to change the acces permissions + top - Displays list of running processes + etd: Pick ines from the beginning + tall-Pick nes from the end + sort-Sort, merge and remove + we-to find numberof ines, word count, byte and characters, + tee -toview output and at the same time store it ina file 5 pte rng “VST a SN cat — DISPLAYING AND. creatine ried cat options (-v and -n) Didi Napinin Cer) a is neal wdfn ying en ls ol Baul was wih cig dla on ae nnping SC carats rap oan ati hopin a dep te carte. Neng Lies) Thames Coleen ows ar dete ad this numbering icity fe eps prone dbugggpogras, Bathe jour cio cn show ine ube two, cn your verona docs suppot 4, oucan eth (22 cmmad dot sae cb z ‘stems Moab "V0 i Fa a unaware TET cat - DISPLAYING AND crcaT eee '5,1 cat: DISPLAYING AND CREATING FILES ie UNIX tem esi edn sighthe Kextisoncofihe no xl morn osmanis Kou osal lon the era eat dnt bisere ta)pon Aaleritng 2. i personel (3 fsjpretcee 6 ise 108 psa Si ese her UNDE oman sb scp moan ea at capt ctxt Me cots fe second i ae ‘Mtn lather words. 084 Systems ognig “VI sity ae eG Se wit 07 Bede show i Seelam ust os cat - DISPLAYING AND ee cat is also useful for creating files, Scat > foo ‘A> synbo! following the comand neans that the ‘output goes to the filenane folloving it. cat used fn this way represents a rudinentary editor. {Crd} Re Prompt returns § cat foo A> symbol following the comand means that the output goes to the filenane following it. cat used in this way represents a rudimentary editor. spams ropamng V0 ‘iar ann aac DO 7 heal be degre rm — DELETING FILES 7 cus cape eae pike: clad cn be cred raven ce ica btu aie ‘rm on, can eee mo chp tropic with bg eg 7 ros/chap. prensa ren prossebag2) Yara seine el ode fens drecory part ctanu operon The when lye epee lls, and youcnthen ui ae hi fe Alfio DoS ict evar! When ou lt a hse ye won prong you wih de SSI (st retry si be tltd ei remoig ce fe The Spr ol ‘eur ea he work has ren don. The wed bee iequieat to." wed BOS | TE ——— OT RSTEOTET rm — DELETING FILES F ace Din) With rh pn retin te alk hgh ‘Se ech al brea and is mihi hoenslnecres Mesh ge ee, Spt fs amo ml ame dia, the ued widths a ‘Then x younate command wore Boku poi ie rie Ferree hare dir adie yada ka heath fils elo roe Feakkiand-$rapampsés xan tkivere pod The-fopionoreies ‘emia paeine nds emoea Wheyoucanncwahthe- pion voelg ae os ay thing ed mate Deke ech in the cre icra belo mn rm — DELETING FILES FE 5.3.1 m Options Iai Dein) Lain etn) Solan beeing hfe pio ake hea ase $m} capt cat cats : Fetove chap (esto)? 2y MB Fn Cha? (js? I ree han (te) (Ee Noropnit—file nat dled 1 ‘Systeme Prograraniing “W308i Dnerciloie Unwenyende Sma DUOC A TO Avra te her eponelenethefeuaeed. rm — DELETING FILES in he i * Bede bet yu Caf eu fig i ithe sem sential reesei deta bet he pee tam 0 ec et ele el tf ea vt | mv — RENAMING FILES BB ‘Theancommand renames (roves fies. this two distin factions + Kreanes file rdreco « Tenors gopoffestoa den dicony dost ct copy ofthe flit mee eames it No adil space isos on dk uingresaing. orenane theca tom, you souls 1 chp nal Tbedesnain ke dose eee thee aml sigh est flame in the esing decoy ay wi he ne nue. By dal av dasa prog fa ernie desian fifi Sob carl gi, Sree eee TID ‘The my command in Linux is used to move or re directories. 1. Renaming a File: Torename file, specify the current filename as the source and the new filename as the destination in the same directory. ‘my old filenametxt new_filename.txt 2. Moving a File to a Different Directory: To move a file toa different directory, specify the source file and the destination directory. iy filet /path/to/destination_directory/ Stns oI * Moving (and Renaming) File / Directory = Movinga file / directory means removing it from its currentlocation and copying it into the new location ‘Syntax: mv [OPTION] OPTION2] ... SOURCE DESTINATION ~ Source refers to the source file name/directory name and. Destination refers tothe destination filename or destination directory ~ ifthe destination isa directory, the source file is moved into the destination directory with the same name as source = The source and destination can reler to absolute or relative path specification of the fle location mv Command Stems Moga ar eno esa NO mv Command 3. Moving and Renaming a File Simultaneously. You can move a file toa different directory and rename it at the same time. ‘mv original_file-xt /path/to/destination_directory/new_name.txt 4. Moving Multiple Files: To move multiple les to a single destination directory, list ll the source files followed by the destination directory. iy filet file2.t file. txt /path/to/destination_directory/ syzans rogram 5, Moving a Directory: _ 8 ~Torriove e directory and its contents to anew location, specify the source directory and the destination directory. f the destination. directory does not exist, the source directory will be renamed to the destination name. If t exists, the source directory will be ‘moved inside the destination directory. ‘my source directory /path/to/destination_directory/, 66, Renaming e Directory: To rename # directory in the same location, specify the current directory name and the new directory name. ‘mv old_directory_name new_directory_name See E-TOC ~The cmp command in Linax compares two files byte by byto. Ibis ‘typically used for determining if two files are identical or, ifthey differ, identifying the location of the first discrepancy. 41. Basic Comparison: cmp fileL txt fle? txt This command compares filo txt and [Link]. Ifthe files are ‘identical, cmp produces no output. If they differ, itreports the byte ‘and line number of the first differing byte. For example: [Link] [Link] differ: byte 10, line 3 2, Suppress Output (silent Mode}: emp “5 file bet file2.04t Sirens Progamming VS cmp — COMPARING TWO a my ofee neo kook tofksidenialoomefthea canbe deed Thar theecommanisinthe UNIX semantic el loka (oma cemsod Ovo it acl wo leans aume $ ch cat ‘ChayOl chapl? differ: char 9, Tine 1 Tieton yyy alte liane iach nti ara aft fie in) ihe tthe sre. By fa, des ter coat pal bene rismatches bu ily adel hen sed with! i option 8 Ses ERE = VIO so aa comm — WHAT IS. common a yuk twolitof ope you ie findoat te asain ona inthe os ea the cmb bth, cmis he command you sed this wok sisted fe nd ist iain ile colunas Lt yton esto cat filet Sat fe ck, shkla ‘afl oganal cha sigh arin senigts A. sagt eke hak it cakobarty Talitchwtury St dsainta ies re sone and hve ome dra. When you run cam it plays the columnar . » sesame ro SaaeVEO $$“ sir ai eo TT comm — WHAT IS common a com feta} ‘nil aggre tara sent Cok hala church sight NE chtiy 5.0, fasgmta sunt chlrtarty Comparing tte an fie Then ois ins uit fis andthe end hin howtels nic tothe econ fl The tid colana dla toi cmon (hence its nan) tot fies 21 SE TREE = VSO an a ane oo TORI diff — CONVERTING ONE FILE TO ot Getic cnc uel dy dia Uae lnk 29love in boeing once bee ikl View ihheameis ples cele Saif Fite ter ovate tne) aa ead tei oi file > ail agra tie > hr sma edie ia (hang ie 2 i file lucha seh oii thie E be > lit dry ‘he “s Diet fin fe file The following command (with the halp of tee command) writes the output both to the screen (stdout) and to the fle Sis | tee file El Sse REVIT Sa a TT ank you Qaa IMP Note to Self i ay PCM eee) Cer eet a IMP Note to Self | Start Recording CSIWZC327, _vstems Programming Lecture No. 7 > Its Important to know that just login to the session does not ‘guarantee the attendance. » Once you join the session, continue tll the end to consider you as presenti the class. > IMPORTANTLY, you need to make the class more interactive bby responding to Professors queries in the session. > Whenever Professor calls your number / name, you need to respond, otherwise it willbe considered as ABSENT IMP Note to Students oe grep to search a file(s) for a pattem and display. grep options to display, count, line numbers or filenames Regular expressions Basic regular expressions (BRE) Extended regular expressions (ERE) ‘sed to edit / manipulate an input stream substitution features repeated and remembered patterns grep Command 6. sytem genie VEO a a oT Agenda > Commands : grep + Pattern matching aud Shell Meta characters ‘+ What is Shell Programming + Need for Shell Programming + Shel Programming Variants + Writing some Scripts + Interactive shel seripting + Arithmetic m shell ‘+ Variables in shell with more examples. 5. Sten Pog ans VIE Sa TT grep Command grep : searching for a pattern grep options Basic regular expressions (BRE) ‘An introduction The character class The* The dot Specifying pattern locations Metacharacters + Itscans the file / input for a pattem and displays lines containing the patter, the line numbers or filenames where the pattern occurs grep Command + Its. command from a special family in UNIX for handling search requirements grep options pattern filename(s) 8 grep Command grep “sales” [Link] + Patterns with and without quotes is possible Its generally safe to quote the pattern Quote is mandatory when pattren involves more than one word It retums the prompt in case the pattren can't be located grep president [Link] 0 sytem pang “V5 empist (Data used for grep) Sais Te ses 1 R/a/S2 [e000 Sharma | actor | production | 1203/50 | 7000, +3672 | suit ehakrobaty | gen. | marking | 19/04/48 | 600 +2365 | baru sergupt | director | petsoane! | 1/05/47 | 7600 + s4z3 | [Link] | chatman | admin | 0/0/36 | 3400 ‘+ 2006 | enaneal sings | eco [sales | 03/09/38 | 6700 +6213 | karunagareuy | em. | acount | 05/6/62 | 6300 + s265 | sn. desgupt | manager [sales | 12/00/62 | 5600 + 4290 | ayant chou | executive [production | 07/9/50 | 6000 +2476 | ai agar | manager [aes | 1/05/59 | 500 + 6521 | chewy | srecir| marketing | 26/0/45 | 200 + 3212] snyamsatsen | gem. aceouns | 12/12/55 | so00 1+ 3968 | sue agar! |execatve| personnel | 05/07/47 | 7500 + 2285 |b sesens | gi. | marketing | 12/03/85 | 2000 + 0110 | vicagrawal | gm markeong | 31/32/40 | 3000, ° Seen roganaie VIS aa aa TE grep Command + When grep is used with multiple filenames, it displays the filenames along with the output grep “director” emp! ist [Link] Where it shows filename followed by the contents sytene aise VID Si Sa ec grep frequently used options grep Command 8 -i ignores case for matching -v doesn’t display lines matching expression -eexp _ specifies expression with this option otherlist will display filenames only we otherlist will display 11 otherist 1. grep -e ‘Agarwal’ -e 'aggarwal’ -e ‘agrawal’ 1. grep —n ‘marketing’ [Link] [Link] 2. grep —c ‘director’ [Link] will print matching multiple patterns 3. grep-c’director’ emp* ist will print filenames prefixed to the line count 4 15 Sens REE VEO a a oT Site rE VEE $i a TO BASIC REGULAR expressions Aa BRE character subset i | i allel al aie COR, Zero or more occurrences + grep uses an expression of a different type to ¢ nothing oF g, 99, ggg, etc. match a group of similar pattems ‘ Asingle character + if an expression uses meta characters, it is : nothing or any number of characters termed a regular expression [pat] single character p, q orr + Some of the characters used by regular [et-c2] a single character within the ASCII expression are also meaningful to the shell range represented by ct and c2 16 2 ‘Systems Programing V0 "BS Fant Db ety ue ean UNO TO Santer Progiamening -WS8————~sra rw renci nie Unven vende SN SUOO A TO Ths character class wrepcommana |= * grep supports basic regular expressions (BRE) grep “[aAlgfar|farjwal” emp.|st by default and extended regular expressions (ERE) with the -E option + Asingle pattern has matched two similar strings + The pattern [@-2A-Z0-9] matches a single * Ategular expression allows a group of alphanumeric character. When we use range, characters enclosed within a pair of [], in which make sure that the character on the left of the the match is performed for a single character in hyphen has a lower ASCII value than the one on the group the right Negating a class (4) (caret) 10 10 sytene nega “VSD —— aaa in Ur eT sytens ropa V5 ‘iar ann ec bo * Zero or more occurrences of the previous character THE * g" nothing or g, gg, gag, ete. grep “[aAgg*[arlfarjwat” emp Notice that we don't require to use -e option three times to get the same output!!! 20 Sp SIRE VST Ga RN A TST Most of the regular expression characters are used for matching patterns, but there are two that can match a pattem at the beginning or end. ofalline “and $ * for matching at the beginning of a line $ for matching at the end of a line stem Pepa VIE THE DOT ‘A dot matches a single character signifies any number of characters or none grep “j*saxena” [Link] 21 Ce grep Command grep **2" emp ist Selects lines where emp_id starting with 2 grep °7...$" [Link] Selects lines where emp_salary ranges between 7000 to 7999 grep *’2]" [Link] Selects lines where emp_id doesn't start with 2 2 sytem nestle VE) $~“ sir ami eon TT When meta characters lose their nearing fai + Itis possible that some of these special characters actually exist as part of the text + Sometimes, we need to escape these characters Eg: when looking for a pattern g*, we have to use \ To look for [, we use \[ To look for .*, we use \.\* 24 ‘Sem rozmeing V0 Bin bc une oe SE grep ERE b+ matches b, bb, bbb, etc. b? matches either a single instance of b or nothing These characters restrict the scope of match as compared to the * grep -E “[aAlgg?arwal” [Link] 26 yams emai -VIO——~ isa as a TOSI EXTENDED RE (ERE) + If current version of grep doesn't support ERE, then use egrep but without the -E option + -E option treats pattem as an ERE + matches one or more occurrences of the previous character 2 Matches zero or one occurrence of the previous character 25 Sptenatrogagy Va arn eno es a OT The ERE set cht matches one or more occurrences of character ch ch? Matches zero or one occurrence of character ch exptjexp2 matches exp1 or exp2 (x1)x2)x3 matches x1x3 of x2x3 a yen ea -VIO iar See STE Matching multiple patterns | grep -E 'senguptaldasgupta’ [Link] We can locate both without using -e option twice, or grep -E \(sen|das)gupta' emp ist 20 sytenatteganaing Vie Bad ion nae NTU Intriuction to sed Command sed stands for Steam Editor. SED or Stream Editor is used for both simple and complex text ‘transformations, such as finding and replacing, inserting, or deleting text. ‘The basie syntax is sed ‘commane! filename Sptenatrogagy Va arn eno ea a SE Fitering Data p (Print) command MINN ing and epiacing- Subttte Cone Yauiéan use the p command to print species, To see who isin the sales department, you can search for the word sales and print those lines, ‘The -n option i crucial here to suppress the default output of every line. sed -n '/sales/ p' employee datactxt ‘his command wil ov 12233 | akcshukls | gm | sles | 12/12/32 | 6000 1005 | dhanchal singh | cvctr | ales | 03/09/38 | 6700 11265 [sn degupta | manage sales | 12/09/63 | 3600 2476 | ail aggacwal | manages | sales | 01/05/59 | 5000 ‘sens roisnning VB i Fa a unaware TET ‘The most common use of sed is for substitution. ‘The syntaxis:s/old_text/new_text/ sed 's/directin/director/’ employee _data.txt ‘The output for this line wil be: 16521 | lalit chowdry | director | marketing | 26/09/45 | 8200 ‘sens inne 50 ‘is ra ann ec DOI Olen: conmana \ “The d command deletes lines that match a specific pattern, To print onlyihermntriesirombann telarane Let's remove the entry for n..gupta (10 5423) from the output. sed-n ‘/barun/,/katuna/ p* employee_data.txt sed '/5423/d' employee_data.txt sed '2,5d' employee_data.txt This will delete lines 2, 3,4, and. Sptemaragiannlng V0 Bn ano ane oo ee aiteng ai oscuro aca ine Essential Shell programming | ‘we will use the g flag to replace all the occurances of a pattern in a line, ‘Whenagopofconaiads kre tn eect se hei lt ‘ : leislexestelas shel i ose progam Though on nay neal ee ths een shel ci. Ths esi maththem wih wads The substitute flag /g (global replacement) specifies the el xd tbe fe Shel sips arena wr cls pres, and hs tel savairtiethettouied Leaning Progennee cane splash ner vork cen ygbelisBowns, ncn ea . sab sel oan usp By ak hci pares egoteegp bto0 sed 's/unix/inux/@! filename canna itis nt cgi il pre nt 25 ‘sens roisnning VB i Fa a unaware TET ene SSE “VEO ain mc What is Shell? * It allows a user to execute commands. + Either manually at a terminal or automatically in programs called shell scripts * Ashell is not an operating system * Itis a way to interface with the operating system and execute commands + Login shell is BASH = Bourne Again Shell Sens roimeing WSO Bash Shell provides a mechanism for generating a list. of file names that match a pattern ~ Example: Is-1*.c — All file names in the current directory that end in .< ~The character * is a pattern that will match any string including the null string = This mechanism is useful both to save typing and to select names according to some pattern orton ogee “V9 _ 8 Advantage of using Shell 8 * AShell Is a program that interprets commands 1 File name short hands Can pick up @ whole set of file names as arguments to ‘ program by specifying a pattern for the names ~ Shell will find all the filenames that match the pattern 2. Input ~ Output redirection Can take input from a file and can redirect output to afile 3. Personalizing the environment —Can define own commands and short hands 26 El tens SERRE VIO a Ra TT characters interpreted by shell ‘What does the command do? Wild cards Wild cards ~ Speci ‘Command Matches any amber of characters inelnding none [Maiches a single character eth SC [At Teast a single charect within this ASCH range co xz] Ar Teast asin Nota single character with fea] this range At Teast single character within @ to d or x to 2 ASC range 20 Systems oie V3 ‘iar ann ac bo Wild card —“*” _ 8 * stands for zero or more characters. + Examples: -lsa outout is only a if ais a file, output is the content of aif a isa directory —Isar* all those that start with ar —Is*viet all those files that end with viet =Is*TiK] all files having i,j or k as the last character, of the filename = Is *[b-t] all files which have at least any character, between b and ras the last character of the filename -Is* allfiles —— sytem a gaaeg WA WRT IO ATT NTT TTT stile Bun pl; Hussccertl ung 3 2 Sptems Progamming 30 BF anh anata TAO Shell Meta characters = = Frege i; Aopend-tnda oupe t 7 Mach anal charerin file anes havc mn : Cnet rina 9: oe 9 Shell Scripts . FB The shell may be used to read and execute commands contained in a file * Assume a scenario where commands who, Is and date are executed one after the other ‘+ To write a shell script to achieve this: — Open a new file [say [Link]] in vi editor = Type alll the commands that you want to get executed one after the other. i.e. who, Is and date Save the file using :wq option. rr tS stems rogaine Va ‘ier ann ec bo

You might also like