101: Introduction to UNIX (i.e. Unix for Windows Users)

Mark Kegel September 7, 2005

1 Introduction to UNIX (i.e. · solid and basic understanding of unix Unix for Windows Users) ¥ not enough time in the regular school year to teach you this and teach you everything • The cold hard truth else you need to know ¥ and also because the CS department · this course is NOT sponsored by the CS doesn’t have the resources, or want to of- dept. fer this course on its own - maybe they will · you will not receive any credit at all in the future ¥ we needed something to do with our sum- • introduce ourselves to the students mer vacation · Should introduce myself and Marshall ¥ the goodness of our hearts, etc. ¥ are seniors in CS and CS/Math • To make this perfectly clear... • And what do we think we’re doing... · will not teach you to program · will not teach you emacs tricks; will provide · here to teach you fundamentals of Unix a reference sheet · to teach you the student to teach yourself ¥ not about learning to use a single applica- about unix tion · to give you the necessary basics to go out ¥ will not solve world hunger, peace, or to be and teach yourself whatever you might need a better person to know to get work done on unix · catch a fish for a man vs teach a man to 2 Computers on Campus fish · trying to follow Mudd philosophy - teach • Available computing resources you to teach yourself · graphics lab • who this course is targeted at · terminal room - printer · AC and LAC labs - printer and scanner · those students who have never worked with unix before and would to get some experi- · Engineering lab - to be avoided ence doing • Servers · those students who have had some experi- · odin, thuban, banshee ence but would like to learn more · knuth, knuth, wilkes, muddcs, arc, cortana · someone who is lost, confused, and looking ¥ knuth is the one you care about for spiritual enlightenment ¥ main server everyone will be working on - ¥ mashall has a great religion for you to join give specs. - we sin so jesus didn’t die in vain • People to know ¥ ask around, see who has what experience · Tim Buchheim and Roger Wiechman • Some reasons to learn about Unix ¥ Bring them milk, cookies and be nice to · and OS X them, they make things work

1 3 Philosophy and History of ¥ about as different as mango, peach, kiwi, straw- UNIX berry ice-cream and peach, strawberry, kiwi, mango ice-cream with bits of vanilla

• Origin of Unix ¥ especially that way in linux, its all just annoying

¥ computers were mainframes that people ¥ it doesn’t matter what you run, what we are worked at, much like knuth going to teach you here will apply across most ¥ teletypes - basically a typewriter plugged though not all into a computer • What is Unix? · Ken Thomson implemented the first UNIX environment ¥ unix in some sense is not an operating sys- tem but rather a programming language ¥ had been working at Bell Labs on a project (C)/philosophy/interface (POSIX) called Multics (Multiplexed ...) ¥ the development of C (and the internet) are ¥ Unix was kind of a castrated Multics - much all connected smaller because invented on an underpow- ered system - PDP-7 ¥ unix is (kind of) C, the Standard C Li- brary, and POSIX (Portable Operating Sys- ¥ became popular mainly because it was dis- tem Standard) which is the API that pro- tributed to Universities grammers use to interact with the system ¥ will want to display Unix timeline kernel

• Unix Today ¥ the design and implementation of unix also follows a certain philosophy about how ¥ programmers environment - because of this an OS should be designed, how programs is extremely powerful should be written, etc. · still very much a tinkerer’s environment ¥ everything else is just historical baggage · unix encourages and support ”hacking” the ¥ makes unix extremely flexible - can start system from scratch with a new implementation but still same ”design” · Fundamentally multi-user and multitasking ¥ designed in the days of mainframe environ- ¥ what would be some good philosophical ideas to ments when many people would all work on follow when building an OS? one computer system ¥ what are some good rules of thumb? what trade- ¥ we still use the same model in many ways offs do you want to make? with knuth • Unix philosophy ¥ clean, simple interface · programs should be designed to operate to- ¥ Chalkboard exercise gether ¥ unix is also multi-layered in design, mean- · output of one could be used as the in- ing that functionality separated into levels put to another going from the hardware to the user, stuff ¥ text streams -> universal input is also abstracted aways at each layer · Programs are viewed as simple tools that ¥ user | (shell, other programs) | OS(kernel) combined will do complex tasks | hardware · each program should do one thing ex- ¥ at top user at bottom hardware, what’s in- tremely well between? ¥ LEGO blocks - simple tools but can create ¥ scales very well - from supercomputers to very complex systems microwaves (embedded systems) ¥ another analogy - hammer, wrench, saw ¥ two different flavors of unix - BSD (FreeBSD, ¥ Avoid the ginormous programs that do ev- OSX) and System V (Solaris, Linux) erything

2 ¥ Windows has this problem (think of Of- make commands and programs interact fice) with each other, not just from treating ¥ Windows programs also don’t integrate each as a fixed black box. very nicely - no pipeline · Richness of utilities (over 400 standard · want to keep everything as simple as possi- ones) often overwhelms novices. Doc- ble umentation is short on examples and tutorials to help you figure out how to · KISS - keep it simple stupid use the many tools provided to accom- ¥ this applies everywhere - both in OS plish various kinds of tasks.” interaction (system calls and library ¥ ”Unix *is* user-friendly. It’s just picky calls) and in program creation about its friends.” · separate mechanism from policy ¥ very true - have to be able to understand ¥ Unix (and programs that run on Unix) the system at some intuitive level to be able provide a mechanism to do something to work with it but do not lock you down to one policy ¥ also if you don’t like something you can fix ¥ policy - how a program should look and it, maybe not easily, but you can fix it behave ¥ users have to decide on policy ¥ What makes a system secure? What needs to be ¥ games and GUIs are generally a good protected? examples - mods and skins ¥ What is a good way of protecting things, like ¥ walk up to some one and try to scare them property? Ownership · principle of least surprise • Unix security · applications will generally produce no output if successful · concept of ownership - everything on the · applications that fail will make it evi- system is owned by someone dent (sirens if necessary) ¥ ownership implies responsibility - so who- · programs should do the least surprising ever owns these files is in the end responsi- thing ble for them ¥ do not try to feed emacs a stray cat · protect users from one another and from · there is no ”one true way” - unix is not doing dangerous things perfect! · protect privacy -> data ¥ unix has things wrong with it, and · protect work -> programs things that unless you understand will · protect programs and files from users - or cause you a great deal of pain rather allow only certain users to do certain ¥ The Unix Hater’s Handbook - written things to expose the flaws in unix ¥ security: not perfect, can break, and will · from a stanford site: cause you to scream a few months down the http://pangea.stanford.edu/computerinfo/ line unix/overview/advantages.html · ”The traditional command line shell in- terface is user hostile 4 Types of users · designed for the programmer, not the casual user. ¥ what powers is each user going to have? · Commands often have cryptic names ¥ do you need to have a class of users that manage and give very little response to tell the the system? user what they are doing. Much use of special keyboard characters - little ty- ¥ what kinds of users are there going to be? pos have unexpected results. • administrative users · To use Unix well, you need to under- stand some of the main design features. · root - is the best known account of this type Its power comes from knowing how to - aka The SuperUser

3 · complete control over everything on the sys- ¥ for security reasons do not want to have a tem web server running as root · can do anything that they want (mostly) - ¥ what if someone owned your web daemon - has to respect file perms. would own your system...is bad · can become any user on the system ¥ unless you’re setting up your own system ¥ loaded gun with the safety off, easy to shoot don’t have to worry about this yourself in the foot ¥ and even then still don’t really have to · is a standard account on every worry about this since most of these users UNIX/Linux/BSD machine will have been created by default - rarely have to create a new system user · used for system administra- tion/maintenance ¥ now that we have users, how do we manage · what this is will cover later them? ¥ any other user however can be granted ad- ¥ should all users be lumped in to one category? min privileges, so root isn’t strictly neces- sary to run a system • groups ¥ why would you want? what if you tried to ¥ are the unix solution to separating respon- delete all the files on the system? shouldn’t sibilty amoung users be able to do · every user on the system belongs to one or more groups ¥ should there be different types of normal users? is there a better way to organize responsibility? · these are sets of users which all share some characteristic • ”normal” users ¥ on the board draw a Venn diagram · have a home directory - directory to keep ¥ one group, on knuth at least, is students all your files to yourself ¥ every student that has an account belongs ¥ cannot generally see others home directo- to this group ries, Odin had this problem and Charlie still ¥ consultants are additionally members of the does operator group · can use most of the programs on the system ¥ however since consultants are also students, · any special configuration files are generally we draw another distinction stored in your home directory · every user has a primary group - this is a · on knuth you have about 120 Mb for all group to which file which you create will be your files assigned ¥ normal users have to obey the security poli- ¥ can generally be a member to as many cies of the system, superusers can ignore groups as the admin wants you to be most of them ¥ some groups serve special purposes along ¥ on your own system can have as much as with the ”special” users you want ;-) or if you’re CS staff · can have groups ftp, www, etc. ¥ the CS department does daily backups so if · exists to allow easier management of a sys- you lose a file you REALLY want then you tem by administrative users can ask them to restore it ¥ for what jobs do you need a third type of user? 5 Logging In ¥ what jobs do you not want root doing, or a nor- mal user? webserver • Loggin In • other users: nobody, ssh, ftp, etc. · two parts: username and password ¥ username can be anything, though some · used to run daemons in the background are reserved for special purposes (root, bin, ¥ run ps -aux | grep daemon etc.)

4 ¥ on knuth it will be the first character of ¥ the teletype would send the input, and the your first name, followed by your last name output would be printed out on a sheet of (hopefully); on knuth the username limit is paper, this meant that the teletype and the 16 characters computer could be located either next to ¥ so on knuth I am: mkegel; Marshall is each other or across the country, the com- mpierce puter couldn’t tell ¥ later the sheet of paper changed to a CRT • password should be strong monitor - would just print the text to the screen · what makes a strong password? ¥ over time the teletype changed to a full · min 8 characters in length (turing only pays keyboard but the fundamental relation be- attention to the first 8 characters of a pass- tween the teletype and computer (input word) and output) for most things hasn’t really · special characters (Numbers and changed @ ! # $ % ^ & . , ( ), even spaces) ¥ obviously today you have both upper and lower case characters · uppercase characters ¥ most of you probably come from either a win- · should not look like a dictionary word dows or mac world where most things can be · pass-phrases are good things done with a graphical interface, for certain things · I took CS60# could become -> a GUI (graphical user interface) can be very 1 t00k CS 6o# (thats a one with good (ex. web browsing), however, certain other two zeros in took and a lower case O) things are made much simpler by the use of the cmd line. ¥ Demonstrate logging in to knuth ¥ for you, however, the best reason to learn the ¥ notice that no password characters show up command line is that you have to when typing ¥ the CS department has chosen to have students do most of their work on unix systems, therefore 6 Introducing the shell to make your life easier you should learn how to use them ¥ log into knuth with the default knuth setup ¥ Why should you learn to use the command line? ¥ what you see when starting What does it offer?

¥ the default knuth setup, easiest and most • The Blinking Cursor of Doom familiar setup · simple, quick, and powerful ¥ components of the prompt: username, time, ¥ what does that mean? command number, and directory ¥ can do an enormous amount of work, with very few commands ¥ change the prompt to something much simpler, like % ¥ for certain tasks are much superior to a graphic interface • Sessions of the Heart · becoming a unix power user involves be- · unix is at its heart a cmd line system - in coming familiar with the cmd line some respects this is much more powerful ¥ we will focus this course on using the command than having just a graphical interface line ¥ it was created in the days of the teletype, like a telegraph with a keyboard only up- ¥ change prompt to something simple - probably percase letters though - so very limited use a special account on farmboy ¥ so the teletype would be hooked up to the ¥ for now we want a simple prompt, don’t want to giant room filling computer distract you

5 • Home sweet Home · things in brackets optional, angle brackets not optional · when you log in you will automatically be ¥ at your home directory cmd [switches] [arg1] ... [argn] ¥ that is how commands will appear and be · home directories on knuth, and many other used on the command line systems (OS X is different) are located at /home/username • switches · on OSX home directories are at ¥ What is an argument and what is a switch? /Users/username · switches are arguments that you pass into a ¥ for me: /home/mkegel (knuth, farmboy) program that modify the programs behav- /Users/mkegel (shadow) ior - switch the behavior of the program · directories are denoted with /, windows · sometimes called flags, we’ll use the term uses switch though ¥ there are other directories that we will get · different than what is generally referred to to later, but on knuth you should never have as an ’argument’ to worry about them · a switch will generally have the form ”-?” • Unix directory tree where ? is some character ¥ most programs take multiple switches and ¥ what do i mean the home is /home/mkegel? also accept multiple forms for what is the no c:? same action · file systems can generally be seen as a tree · other style, GNU style, is "--switch" · a cdrom or hard-drive can generally be lo- · most programs accept many switches cated anywhere in the tree ¥ gpg (encryption key generator) accepts 360! · no preset structure ¥ demo the tar stuff from the command line · working with devices we’ll get to much later so just sit tight for now ¥ can have multiple switches, example tar ¥ ¥ there is no c:, d:, a: like on windows, every- tar (tape archive) is an archiving program thing starts at / (root) from the days of tape drives ¥ file archives are given the extension ”.tar” · is filesystem independent (gee big surprise!) • Introducing the Shell · to uncompress will most often type ¥ what is the shell? talked about multi- ¥ tar -xvf [tar file] layered design but what is it? ¥ ”-xvf” could have been typed ”-x -v -f” but · The shell is the highest level interaction easier to write the first way layer between you and the OS ¥ on old archaeic systems this may not actu- · it’s what sits between the teletype and the ally be the case but on new systems it most computer - makes the computer easier to often is use • Arguments · it runs whatever commands you give it, · data that you want to program to operate with whatever arguments you give it on · the most important thing in learning to use ¥ what is an argument? what would you pass unix in a productive manner is to become in? comfortable with using a command line · filenames, text strings, man pages ¥ that’s not a computer fan, that’s the shell purring... ¥ all of this stuff is sensitive to spaces, so ”ab” is not ”a b” • syntax of a command ¥ not every command takes arguments; some re- · will use quire them

6 · some basic commands to get started with - these · there are also special characters will in dif- commands are more complicated than what we ferent contexts will be interpreted differ- are actually showing here, you’ll be able to find ently out more stuff about them after the end of this lecture • Ctrl+World = Mwahahaa! · discuss C-? and M-? notation ¥ these are also the commands that you’ll be using the most often · you see this everywhere, particularly in doc- umentation, so is very useful to know ¥ students are going to have to write all of this · is incredibly simple: C == Ctrl; M = Alt down, will probably want to put on the chalk- (but stands for meta) board · therefore C-x is Ctrl and lowercase x held ¥ cd [directory] :: changes the current di- together rectory · are what might be called shortcut keys un- ¥ pwd :: prints the current directory you are der windows in · one other notation is caret notation; ^h ¥ mv :: moves the would mean C-h ’source’ file to ’target’ file/directory • quitting programs ¥ cp :: moves the ’source’ file to the ’target’ directory · most programs can be quit through either Ctrl-c or Ctrl-d ¥ rm [file2] ... [filen] :: removes files · C-d sends an EOF character ¥ ls :: gives you a listing of the files in the · C-c sends an interrupt signal that causes directory the program to clean up and then to exit ¥ mkdir :: creates a new directory · if C-c doesn’t work and you have to quit the program, then C- will cause the program to ¥ rmdir :: removes an empty direc- exit without cleaning up first (as well as tory dumping core) ¥ man [command name] :: prints the ”man • tab completion page” for various system commands · wouldn’t it be nice if say you have some ¥ some commands are interactive long directory name, ./thisdirectorynameis- ¥ all these programs just run, do their thing, and toolong/ but there was some way that then quit you didn’t have to type it out every time, hmmm... ¥ what we generally refer to as an application is · most shells (and any that you would interactive; commands are generally not interac- normally use) provide whats called tab- tive completion, allowing you to complete par- ¥ emacs and the shell are both applications tially written statements into their full form ¥ demo this feature • some things to know ¥ so given partial data the shell will deter- · note: unix is case sensitive - AAAA is dif- mine what sequences of characters will ap- ferent than aaaa - this shouldn’t be too sur- propriately complete what you’ve written prising on the cmd line · also most unix commands are done in lower- ¥ this is accomplished by hitting the tab key case as you’ve just seen ¥ hit tab key to show different completions · most switches to programs will also be in ¥ once you’ve typed enough to make the rest lower case, although some need to use both of the string unique (that is there is only - pay careful attention to both the switch one possible thing that you could be typing) you’re using and the case of the letter(s) the shell will automatically complete what you may be passing in you’re typing when you hit tab

7 ¥ demonstrate on the long directory name · the shell interprets a number of characters ¥ since directory names have to be unique on as being special the same level, tab-completion work really, · that is some characters have meanings to really well the shell other than what you may have ¥ but what if you want to tab complete other typed things? · so which characters are special? ¥ like say the arguments to a functions, or · switches... # $ * ? [ ] ( ) = | ^ ; < > ‘ $ " ’ \ ¥ luckily this is setup by default with the shell you’re using, so you can tab complete any ¥ what do you need special characters for? number of things, man pages, arguments, ¥ for example the semi-colon ’;’ separates etc. commands on the cmd line • files and directories ¥ cmd1; cmd2; cmd3;... ¥ up arrow gives old commands, continue ¥ but what if you want to pass a special char- pressing to work back through your en- acter to a program? tire command history, useful for when you have to type the same commands again and • Escape From ”The Shell” again - like ¥ make program ¥ what is you want to pass these characters into a program? ¥ the down arrow with of course cycle forward in your history · have to escape them; \?, where ? is the special character you want to use • command line editing · demo echo semi; versus echo semi\; · the left and right arrow keys move the cur- sor allowing you to edit the cmd you’ve · so when you type \; the special meaning of typed on the screen - that way you can go ; is disabled back and correct something without having · you can also disable the special meaning by to retype the entire command surrounding the character in quotes, either · other useful key commands are (assuming ” or ’ (double or single quotes) they aren’t already used): · demo echo ";" · C-? will delete back · how quoting is more powerful since all the · C-d will delete forward special characters inside of the quotes get · C-u will delete an entire line disabled - well mostly, there are some rules · C-k will delete all of the line forward from the cursor · pg. 527 UPT · C-l clears the terminal · ’xxx’ - everything get disabled · C-y will paste the characters you have · "xxx" - everything but $ ’ and \ deleted · the character has another meaning that · C-a will move the cursor to the begin- you have to worry about ning of the line · C-e will move to the end of a line · if you have a at the end of a line then the · M-b moves back a word new line will be removed and the shell will treat the two lines as one long continuous · M-f forward a word line · C-_ will undo the last thing typed · C-r will do a history search ¥ echo Who \ stole \ · there are many more...and most impor- the cookie, \ tantly you can create your own! from the \ • special characters cookie jar \?

8 7 Finding help · is a great resource for general informa- tion, system policy, etc. • Finding Help ¥ give the URL for the QREF’s ¥ our goal is not to teach you unix, but to · http://www.cs.hmc.edu/qref/ give you the basics, then have you be able · the professors will also help you out as much to teach yourselves what you need to know as they can ¥ but sometimes you have to ask for help, · some good mailing addresses for questions and often times this is the best way to · [email protected] - linux mailing list learn something new, don’t beat your head for hmc students and others against the wall if someone you know al- ready has the answer and can help you fig- ¥ give out others (?), maybe fo- ure it out! rums.muddstudents.org · so part of teaching yourself is knowing when • Built In Docs to ask questions · man pages · there is one resource that is the best and it is? ...Google! · purpose is to document how each of the various programs/commands on the · whenever you have a question about some- system work, and how to get them to thing the first thing that you should always do what you want do is google for the answer - most likely · generally short on examples, but in someone else has already taken the time to general well worth reading solve and write up whatever solution you are looking for · can document other things ¥ get in the habit of using man pages - · lots of very good resources saves time and effort! · websites (particularly forums), books, · what man pages are not documentation, and people · general reference, programming · some good websites to check out are: guides linuxdoc.org, freebsd.org ¥ what man pages are · some good books are: UNIX Power Tools, FreeBSD Handbook... · very specific references on a limited set of system commands · people: Marshall, Mark, Mac, and many others!! · essentially they document how to use the programs on the system · Computer Science Department Staff and ¥ some man pages break these rules but Consultants in general they seem to be followed · they have people hired to help you out!! ¥ demo man You should use them as a resource! · Other systems · Staff take care of the various boxen the department runs · info ¥ give a listing of the boxes, their pur- · is mainly about emacs pose, and their addresses · the ’help’ command for bash · knuth.cs.hmc.edu – shell server · Most programs come with more documen- ¥ find new mail server tation than just a man page · muddcs.hmc.edu – web · check out: /usr/doc and · Consultants sit in the terminal room /usr/share/doc and answer Unix and CS related ques- · slackware uses /usr/doc and is really tions good · give a list of who are the consultants, emails, and schedule 8 How to use man · The CS department website · QREF’s - documentation written • how man is organized - sections - explain what specifically for YOU each section covers

9 · 1 - Commands available to users ¥ SEE ALSO - other related man pages to see · 2 - Unix and C system calls ¥ BUGS - bugs the program may have · 3 - C library routines for C programs ¥ FILES - files that the program may use · 4 - Special file names (Devices and Device (usually configuration files) Drivers) ¥ AUTHOR - who wrote the program

· 5 - File formats, protocols, and conventions • navigating a man page for files used by Unix · 6 - Games ¥ man by default uses more (more is a pager that sucks) · 7 - Conventions, Macro packages, Word processing packages and Misc. ¥ on knuth man is setup to use less, but be aware!! · 8 - System administration commands and procedures ¥ so learning to use man is really like learning to use less ¥ always need to keep in mind that one com- mand could be in two plus different sections · arrows move man page up/down left/right at the same time · space bar moves a screen at a time ¥ allows for a better organization of documen- · to search would type /pattern, then press tation enter • some switches · demo this feature · typing / or n again moves to the next in- ¥ man -k perl stance of pattern ¥ so then ”man -k” allows you to search for · N moves to the previous instance man pages · b : back - like page up ¥ ”-s” - specify the section to search · f : forward - page down ¥ ”-a” - show all man pages that match the search, not just the first ¥ man almost always uses the less program as its pager, so learn to use less! ¥ man -a sync (demonstrate this) ¥ you’ll also use less for lots of other text • how to read man pages - the parts of a man page viewing

¥ at the very top is the is the name and sec- ¥ exercise - now for a man page hunt tion number; for man it is man(1) ¥ NAME - just a quick one line description 9 Making programs work to- ¥ SYNOPSIS - all the arguments that the program accepts gether on the cmd line ¥ how to tell if something is optional or • Working Together required ¥ optional things are surrounding in ¥ ask question before showing slide brackets ’[xxx]’ ¥ unix is about more than just running one ¥ required things are not program at a time (that’s windows job); in ¥ show example - man requires at least unix programs work together, so we’ll show one argument you how to do that ¥ man man ¥ How do you get programs to talk? Files? ¥ DESCRIPTION - a paragraph ”summary” Pass in a program as an argument? What of the program should handle this mechanism? What about basic program I/O? ¥ OPTIONS - what each of the switches to the program do ¥ Already know that can accept arguments and print stuff to the terminal. ¥ ENVIRONMENT - what environment vari- ables the program expects ¥ Text streams are a universal I/O format

10 • Working Together · so stdin, stdout and stderr are files? no, but are treated as files · unix runs more than one program simulta- neously · are actually file descriptors, so being treated like files · programs need to work together · why so surprising? you read() from stdin, · programs use stdin, stdout, stderr for gen- write() to stdout and stderr eral I/O purposes · each program has its own set stdout, stdin · by default I/O comes from cmd line and stderr that is managed by the Operat- ing system • What stdout, etc do · how do you combine simple programs? how ¥ you should already know about stdin, std- do you get two programs to talk? out and stderr from CS70 or CS60 ¥ good time to use the chalk board ¥ they allow a single program to get input from the cmd line or print output to the ¥ ask group for ideas cmd line ¥ we know that two programs can share the · stdin - represents the characters you are in- same file - can have the same file open two puting different times · stdout - the standard place for the program ¥ so to communicate one program could write to print output to a file, the other could read · general goes to the terminal you ran the ¥ [program one] --> [file descriptor/pipe] --> program from [program two] · stderr - like stdout but for error output ¥ can use the stdin, stdout, and stderr files to have programs talk to each other ¥ need to show distinction between stderr and stdout ¥ [program one] --> [stdout/stdin] --> [pro- gram two] ¥ in C++ the three are accessed through cout, cin, and cerr ¥ the stdout of one program IS the stdin of another ¥ can accessed in other ways through other languages ¥ very flexible and powerful solution ¥ so we have some idea of what stdin and • Using pipes stdout are, they represent the input we get from the terminal and the output we send · stdin, stdout, and stderr are actually pipes to the screen, but how do you think that · instead of the program printing its output they actually work? to the screen it is piped to the second pro- gram • Everything in Unix is a file · keyword: pipe · devices (keyboard, mice, monitor) · you can do this from the shell · directories (yep even directories) · the syntax is · links (we’ll cover what these are) ¥ cmd1 | cmd2 · and what you normally think of as files ¥ the ’|’ character is known as the pipe and ¥ what does it mean to be a file? anything is located on the key, so Shft- is | used with read() and write()? ¥ example of this in action - first run who and ¥ put quotes around file -> ”file”, aren’t re- tell what wc does, then run this ally files, just treated like files ¥ who | wc -l ¥ a file is just a stream of data, in some cases ¥ (gives the number of terminals open on the with limitations as to how that stream is system) handled ¥ get a longer piping example • Are they file? · programs used in this way are called ’filters’

11 ¥ filters are the tools that you use to make ¥ have a demo ready to append tasks easier, and you need them to be good ¥ echo "Hello there!" > newfile ¥ if you can’t use filters you’re never going to ¥ echo "Good to see you." >> newfile be able to exploit the power of the system ¥ cat newfile ¥ cause you won’t be able to leverage the • Working with stderr work of other people ¥ how many people think that they could im- ¥ word of warning: this is very shell depen- plement ls? how about a good implementa- dent, but since you are working with zsh, or tion? you ought to be the syntax provided here should work ¥ to make them good you have to follow a certain set of standards and conventions · send both stdin and stdout with ¥ cmd1 |& cmd2 ¥ if you don’t the filter will be difficult to use ¥ cmd >& file ¥ so what are these conventions? we dis- ¥ cussed many of them at the beginning of spacing matters so pay attention to it the class ¥ can also append stderr to a file - better for log files since you don’t generally want to ¥ KISS, principle of least surprise, you’ll learn send error messages along a pipeline, only most of these just by using a UNIX system want real output ¥ demo piping on terminal · to append stderr very much like redirecting • Redirecting Output stderr ¥ cmd5 >>& file ¥ can do more than just piping between pro- · to redirect just stderr to a file grams ¥ cmd 2> file · you can redirect the output of a program to ¥ a file redirecting just stderr along a pipe is a bit more difficult · you can redirect a program to read stdin ¥ demo this concept from a file ¥ keyword: redirection • Complex Redirection · you can have whatever file you want to be · can modify stdin, etc, file descriptors from stdin, or stdout/stderr, using the shell the cmd line · redirect file to stdin · stdin=0, stdout=1, stderr=2 : are file de- scriptors (integers) ¥ cmd1 < file1 · file descriptors 3 to 9 are free for you to use · You use the < to have file be stdin for the · examples: program · 2>&1 · redirect stdout to file · 1>/dev/null ¥ cmd2 > file2 · 1>file is same as >file · So you use > to redirect the output · 2>&1 1>&2 (this won’t actually swap ¥ Some notes on redirecting output stderr and stdout) · (this will swap the ¥ the file cannot already exist (if you have 3>&2 2>&1 1>&3 two) NOCLOBBER set) ¥ won’t execute if it does ¥ demo this concept ¥ since this is set by default on knuth you ¥ not every program takes things from stdin or need to be aware of it sends output to stdout · You can append to the end of a file (add ¥ for example can’t do lines of standard output at the end without writing over what is already there, useful ¥ emacs < myfile for log files) by doing the following ¥ instead have to send myfile in as an argument to ¥ cmd4 >> file emacs

12 10 Another way to combine ¥ example: cd is a builtin, its a part of the programs shell and not a stand alone program like say emacs is

• The Back-Tick ¥ need to have some sense of the basic, underlying abstractions of how the computer works - not on · you may remember the ‘ as being a special a bits and byte levels, but higher up character · ‘ is located on the key above tab ¥ exercise: back to the layer thing, how the com- puter is organized · is called a back-tick or back-quote · anytime you surround a string with back- ¥ again is a layer thing - bottom up ticks the shell will treat it as a command ¥ hardware - the machine itself - just electri- and then replace the string with the output cal signals of the program ¥ develop on the board a better layered model ¥ kill -HUP ‘cat /var/run/sshd.pid‘ ¥ user | shell - applications - libraries | kernel ¥ this will restart your ssh server, since ‘cat..‘ - modules | hardware - remote systems returns the PID of sshd ¥ kernel - a mediator between the hardware · another example (and remote systems) and the user - pro- ¥ emacs ‘grep -l error *.c‘ vides a set (library) of functions for con- trolling the hardware that every program ¥ this will let you edit all of the files that have you would write would need (ex. drawing the word error in them text to the screen, writing to disk) ¥ lots of other uses, mainly in shell scripting ¥ applications - do the things that you want the computer to do 11 You Ought to Know this by ¥ libraries contain useful code that you’d now rather not reimplement ¥ the user - running applications and making • Review sense of the electrical signals that the com- puter processes - only here do things have · Philsophy and History of Unix any ”meaning”

· Piping, Redirection, Back-Ticks ¥ in understanding unix you don’t so much about · Finding Help the hardware - leave that to the engineers

· Users and Groups ¥ really only care about the applications layer - · Some Basic Commands how to make programs do what you want · What the Shell is and why its cool ¥ but in order to fully appreciate and use unix you · Few Other tidbits have to understand at some level how applica- tions interact with the kernel, and what a kernel ¥ if you don’t know what we’re talking about is here, then you’ll want to review before go- ing on • What is a process, program

¥ what is a program and what is a process? 12 Processes and the Kernel - ¥ ask what a program/executable is - just the Actually running programs file containing instructions ¥ is very broad: does it matter what kind of ¥ do most of this on the board instructions? ¥ some of the commands we’ve been running ¥ the instruction could be machine code haven’t actually been ”programs” in the sense ¥ could be byte code like in java or com- normally described piled python

13 ¥ or could be an interpreted scripting lan- ¥ kernel does more than this guage ¥ take Operating Systems if you want to write a ¥ what is process? kernel ¥ a program in action ¥ an abstraction provided by the kernel ¥ take CS105 to learn more about how Computer Systems work ¥ every process thinks its the only one running - runs in a self-contained envi- ¥ as users all we care about is running programs, ronment with the kernel being respon- leave the other stuff to the CS geeks sible for enforcing this abstraction ¥ how is a program different from a pro- ¥ and unix is very good at running programs cess? ¥ so what does this have to do with anything? ¥ how many copies of each do you normally have? ¥ not a whole lot other than making you un- ¥ 1 for executable (one file containing the derstand that you can run multiple pro- binary code) grams with fear of them crashing the sys- tem like on some other systems we could ¥ can have different versions but those mention don’t count ¥ n for processes • Making Forrest Run ¥ why would you have more than one process · unix is a multitasking environment of a single program? ¥ can run multiple processes at the same ¥ need them to be doing different things time - two xterm windows for example ¥ no fear of them interacting • The Kernel? ¥ each process runs in its own little envi- ¥ how do the processes get run? ronment isolated from others ¥ unless you WANT them to interact! · the job of the kernel is to manage the very key! many processes on the machine and run them in some sane order so that the · unix is a multiuser environment system is both responsive and produc- ¥ can handle multiple users using the sys- tive tem at the same time · the kernel also provides an interface to ¥ so if you are going to run multiple programs all the hardware on the system from the shell(s) it would be helpful if you · this interface is uniform across archi- knew how to manage them tectures and hardware ¥ with GUIs most of these things are not · so you always call read() though the really an issue, but its still very use- disk might actually be just a network ful to have some idea in case you mess share somewhere in asia things up ¥ does the kernel just run processes? basi- ¥ can open as many xterms as you want, cally, and do what processes need done but at some level you need to know ¥ what might be the other jobs of the ker- this... nel? ¥ from a single shell session can run ¥ manage devices about as many programs as you would ¥ reading/writing files ever want to - constrained by memory ¥ handling system calls and processing speed ¥ enforcing system policies - security for ¥ normally though you are only ever run- example ning one program at once in a shell - ¥ allows for multiple users on a single sys- this is referred to as the foreground pro- tem cess ¥ abstracts a common interface for differ- · we’ll assume one shell and that you need to ent hardware - creates an abstration run multiple programs

14 · is called ”job control” · will want to demonstrate that pine can ¥ why would you need this even though we not be suspended by default have GUIs? • Background Processes ¥ because you won’t always have a GUI, · when we type may end up working at just a terminal ¥ cmd • Making Forrest Run · this program is the one we’ll be running in · three types of processes: foreground, back- the foreground ground, suspended · to run a program in the background we type · foreground process is the one that receives ¥ cmd & keyboard input (if interactive) and prints to · this causes the program to run, but not in the terminal the foreground - so it won’t be receiving ¥ mv .. any input that you type unless you switch to that program ¥ ls .. ¥ just because a process is backgrounded does ¥ rm .. not mean it isn’t running, and that sus- ¥ sometimes you want to run other programs pended process aren’t running ¥ say stop reading a man page to copy a file, ¥ you could have a copy operation running in or read email the background ¥ could open another terminal - do you ¥ music program in the background really want to do this for every pro- ¥ also note that the system has to run all gram? what if you only have a limited these, so no free lunch! number of terminals? ¥ remember: limited by RAM, hard drive · or could ”suspend” the process in speed (swapping), and cpu power which case it stops executing · or put it in the ”background”, and con- · to switch back to the process you just sus- tinue to let it run pended you use the fg command · background process don’t recieve input, but · can of course suspend multiple processes can still print to the terminal · demo two other man pages, also suspend · suspended processes just aren’t doing any- · will have three jobs in the background with thing - but are not terminating cmd line open · open emacs in background and switch to it • Sending the Right Signals ¥ emacs & · recall that C-c (SIGINT) kills the program ¥ do some thing on the cmd line you are currently running ¥ fg · so does C-\ (SIGQUIT) ¥ this is one of the most useful things to know, ¥ sometimes its C-d (sends an EOF – usually so remember it! for interactive programs) ¥ why this is useful - give this example sce- · to suspend a running process use C-z nario (SIGTSTP) ¥ are logged in remotely with no X ses- ¥ common mistake to think that the program sion has just suddenly died ¥ have an emacs window open and ¥ people unfamiliar with the system will just change a bit of code start over, being angry and frustrated at ¥ too slow to exit emacs and restart the work they ”lost” ¥ so press C-z to suspend ¥ do a demo of suspending a process ¥ then can rebuild from the cmd line ¥ demo reading ”man page”, suspending, and ¥ then can switch back to emacs to con- using pine tinue working

15 ¥ really only need to remember C-z to sus- ¥ continues job n in the background pend and fg to bring the process back ¥ also cover %+ and %- ¥ draw on chalk board what is happening when ¥ what happens if we background emacs? you suspend or background a process what happens if we suspend emacs and let ¥ shell -> process -> shell it continue in the background with bg? ¥ shell -> (splits) -> shell (-> process) ¥ now that we can manage a few processes ¥ and more what about the processes on the rest of the machine? • Mid-level management · use the jobs command to list all the jobs that you have running 13 Understanding process own- ¥ jobs ership [1] + Suspended man page • Process ownership

[2] Suspended man perl · every process on the machine has a par- [3] - Suspended man printf ent/child status ¥ the number identifies each job · so when you log in and get a shell that pro- ¥ we use the fg command to switch back to cess will act as the parent to all the other which ever job we want using the number processes you create on the machine ¥ by default fg will bring back the job · there is one master process on the machine, with the plus sign init ¥ this is the current job · every process is a descendent of init ¥ the minus sign was the job that used to · every orphaned process is a child of init be the current job · a zombie is a process which has exited but ¥ the rest of the output of jobs is obvious hasn’t been reaped by its parent ¥ Stopped means it doesn’t receive any ¥ draw out a parent/child diagram from init cpu time down to the shell ¥ running means just that · init -> getty -> your ¥ example to go back to man perl we would shell (zsh) -> your programs type · init -> sshd -> your ¥ fg %2 shell (zsh) -> your programs ¥ nifty trick: if we know the name of the pro- ¥ parents can query for the exit state of a cess, say we have a single emacs process process along with other can quickly switch back to emacs by using a pattern matching scheme • Process Ownership for fg · each user on the system can own processes, ¥ instead of %n, you have &?xxxx where xxxx so when you log in the shell that you see is is the first part of the name of the running owned by you process · to identify all the processes on the machine ¥ obviously you need enough of the name each process is given a PID or process id to disambiguate the process that uniquely identifies the process ¥ so returning to emacs could be as simple as · you use the ps command to see the pro- typing cesses that are running ¥ fg %em ¥ run just by itself (no switches) will just ¥ don’t have to use jobs and look there, just print out your process that are being run do a single fg command from the terminal you logged in on ¥ can also do some of the same tricks with bg ¥ to see all processes ¥ bg %n ¥ ps -aux

16 · can see what everyone is running with this ¥ then press the ’u’ key; then type your user- ¥ point out which column is the process id name, or another username if you want to see their processes ¥ pids are not always 5 digits ¥ ¥ init for example will always have its then enter PID equal to 1 ¥ demo this feature also ¥ on OS X pids are generally 3 or 4 digits ¥ to get help on the other commands top of- ¥ linux tends to be 5 digits fers use the ’h’ key when running top ¥ will most always between 2 and 32000 ¥ to quit top press the ’q’ key or C-c ¥ so why is this useful? ¥ top is really nifty, so spend a few minutes • How to be like Charlie Manson trying to figure it out ¥ digression: what would happen if say, we ¥ what if we want to kill a runaway process were root and we typed or any other process ¥ kill 1 ¥ imagine you have a java application in an infinite loop ¥ what would happen? is that even referring · this would be a good time for CS60 stu- to a process? dents to pay attention ¥ in general killing a parent does not kill · don’t want it running forever, so want to child, child is orphaned and becomes a child kill that process of init · we use the kill cmd to do this ¥ init is the exception ¥ kill [PID | %n] ¥ yes it is - init (or launchd on Tiger) is given PID of 1 ¥ two ways to kill ¥ so typing kill 1 while root would kill you ¥ (1) if the process is running in the back- entire system - basically all processes stop ground, can use background number and the system has to be rebooted ¥ (2) can use the process id (PID) ¥ this is a bad thing and should be avoided ¥ also demo ¥ killall • Process priority ¥ we can use ps to find the process id ¥ what is process priority? ¥ demonstrate using ps to find the id and kill · priority defines how much time the partic- the process ular process should get from the machine ¥ but how did we know that the process was · processes with a low priority gets run less out-of-control in the first place? often than a process with a high priority · nifty utility on unix called top · as a user you can’t actually change the pri- ¥ will list out the processes in order of cpu ority, only root and the scheduler can do time that ¥ demonstrate top - point out the PID column · what you can do is set the *niceness* of the ¥ can use this to see if any of your processes process are out of control · a higher nice value means that the process ¥ should be able to point out several aspects of is nicer to other processes on the system, top giving them more time to get stuff done ¥ memory usage, cpu time, state and pri- ¥ so a high nice value leads to a low priority ority and a low nice value leads to a ”normal” ¥ top is made more useful by seeing only the priority processes that you own ¥ to set the niceness of a process you use the ¥ first start top nice cmd ¥ top ¥ nice cmd

17 ¥ if you are going to be running a long and ¥ or just when you want to write to stdout fairly intensive job on knuth please use, and ¥ usage be, nice! ¥ echo "text string" ¥ nothing worse than locking the system up for fifty people while you try to compute · or prime numbers ;-) ¥ echo text string and more text ¥ show how mprime has been niced on ¥ before using you should really read the man odin page (it has some very interesting options) ¥ to use nice run it like such ¥ cat ¥ nice [-n NUM] cmd args... NUM=0 to 19 (lowest) (root for 0 to -20 (highest)) ¥ like echo, instead takes file names as argu- ments ¥ find a demo for nice ¥ prints the contexts of each file in order to ¥ check out the man page for more info standard out ¥ does nice have an effect? ¥ one of the more useful commands, espe- ¥ nice will have an effect if the process is *cpu cially if you want to look at a small text bound* meaning that it requires the proces- file sor more than anything, doesn’t really need ¥ any input cat is short for concatenate - you may think they should have used ”cons” ¥ on *IO bound* processes not so much since they wait for input anyway, and while wait- ¥ usage ing the system will do other things, won’t ¥ cat have much effect ¥ if no file is given will take its input from ¥ now know all about managing processes and stdin how the system handles the ¥ end the text stream with C-d ¥ will then print back to stdout 14 Some programs to remem- ¥ most useful when combined with redirec- ber tion and piping

¥ this is a list of programs that we consider to be ¥ clear the most useful or that are basic to being able ¥ clears the screen (also C-l) - useful for when to use a unix system your terminal has filled up with junk and ¥ locate, echo, cat, clear, less, head, tail you’d like to make it go away ¥ clear ¥ locate ¥ more ¥ allows you to find files on the system ¥ has to search a database so not always up- ¥ is a pager to-date ¥ sometimes you have more text than can be ¥ usage fit on a single window, so need to way to see the entire text of a document ¥ locate ¥ more lets you view the entire text of a docu- ¥ accepts wildcards for files which we’ll get to ment, but is limited in that you can’t scroll later but which you may already be familiar up with... ¥ use the space bar to advance to the next ¥ echo screen

¥ prints whatever arguments that you give it ¥ less to standard output ¥ useful for when you want to write/append ¥ less is also a pager some string to a file ¥ less is a better pager than more however

18 ¥ everyone knows the old adage ”less is ¥ ”+NUM” : shows all but the first NUM more”, there is also another pager called lines of a file most ¥ now on to some other commands ¥ most > less > more ¥ have already had exposure to this program ¥ the next two are both searching commands - through the man command one is for searching through stdin, the other for searching through a directory tree ¥ man opens up its man pages in less (not by default but generally) ¥ grep ¥ but of course can use less to view more than ¥ just man pages allows you to search for a regular expression within a file ¥ usage ¥ often though you just want to find a string ¥ less within a file, not everything matching a cer- ¥ and some of the key commands to make less tain pattern do what you want - interactive ¥ this is really just a copout since we don’t ¥ arrows move man page up/down left/right want to show you regex’s until later ¥ space bar moves a screen at a time ¥ so to search through a single file ¥ to search would type /pattern, then press ¥ grep [switches] "string" file1 ... fileN enter ¥ grep has some useful switches that make it ¥ demo this feature nicer to work with ¥ typing / or n again moves to the next in- ¥ ”-c” : instead of printing lines that match stance of pattern will instead count the number of lines that ¥ N moves to the previous instance match and print that instead ¥ b : back - like page up ¥ ”-n” : print the line number on which the pattern was found ¥ f : forward - page down ¥ ”-H” : prints which file the pattern was ¥ head found in ¥ prints out the first n lines of a file to stdout ¥ ”-r” : if a directory name is given will recur- ¥ useful for when you have a sorted list and sively descend through the hierarchy look- want only the first n ing for matches in all the files in that tree ¥ by default (if n is not specified) will print ¥ there are also different versions of grep, so out the first 10 you’ll want to check out the grep man page, as usual ¥ usage ¥ head [-n XX] ¥ find ¥ tail ¥ at its most basic level find just descends through a directory tree and prints out the ¥ like head, in that it will print out the last n names of whatever files it finds there lines instead of the first ¥ so you can replicate (in functionality but ¥ more useful in that files, especially log files, not speed) the locate command with are often appended to ¥ find ./ | grep pattern ¥ can then follow these files for as long as they ¥ are open and see what is being added to this is generally much slower than locate, them but has to go through the entire file sys- tem starting at ./ (which is where ever you ¥ usage currently are) ¥ tail [-n XX | -f | +NUM] ¥ but find is much more complex than this ¥ ”-n” : gives last XX lines of file and can do locate all on its own ¥ ”-f” : keeps file open ¥ find ./ -name glob -print

19 ¥ the ”-name” switch will search for the glob, ¥ show this output and ”-print” will print it to standard out (note that glob != regex) ¥ at the end of this would be a good time to poll to figure out what other programs people would like ¥ find can of course do much more interesting to know about; things ¥ try to find out what this does, and why you ¥ questions like ”what is the program that does would need it ....?” sorting, calculator, etc. ¥ find ./ -name ".foo" -perm +444

-exec chmod -R a+r {} \; 15 Files, programs and file, and the filesystem ¥ these are other commands which are useful, but not exactly necessary to know ¥ what is a file? some review from before

¥ w ¥ a file is a stream of bytes; a file isn’t always nec- ¥ prints out who is on the system essarily on disk ¥ w ¥ traditionally think of a file as something like a text file or mp3 ¥ which ¥ in unix though we know that everything is a file: ¥ prints out which command exactly will be directories, devices, etc. executed when you just type cmd ¥ useful for when you want to figure out either ¥ review the unix security model which program you are using or where the ¥ program you are using is users have a home directory ¥ ¥ which cmd users not allowed to do random (dangerous) things - only allowed to do those things that ¥ du they have permission to do

¥ displays the amount of space that a file (or ¥ there is a superuser that can do anything directory) is taking up on disk ¥ users belong to groups, and have a primary ¥ du file1 file2 file3 ... group ¥ to get the output to be a bit more useful, • Files will want the ”-h” switch ¥ how should an OS handle security for files? ¥ demonstrate this how do you tell who owns a file? what’s a ¥ if file is actually a directory, du will recurse good way to manage sensitive files? through the directory printing out the size · files have their own security model of each file as its gets them finally printing out how large the directory is in total at the · files have an owner and a group that they end belong to ¥ this is not very useful, so pass in the ”-s” · user is you switch to suppress the extra output · group is the group the file belongs to ¥ this creates a useful ”sizeof” command · so when you create a file, the file is created ¥ du -sh file1 file2 file3 ... with you as the owner, your primary group as the group the file belongs to, and all the ¥ df permissions on ¥ shows a detailed listing of the amount of · so we’ve defined two categories that a file space being used on each file system that is belongs to, what else is left? mounted · what about the other people on the system? ¥ show the output of and explain · so in all a file is concerned with three sets ¥ made nicer by the ”-h” option of people: user, group, others

20 · others are by definition everyone else on the ¥ examples of common permissions settings system ¥ 666: everyone read, write · besides owner, group, etc., files have per- ¥ 777: do anything to file missions associated with what people inside ¥ 022: user can’t do anything, group w, of these classifications can do with the file other w • File Permissions ¥ 744: user do everything, group read, other read ¥ based on the security model above what ¥ as an exercise write out the octal permis- kinds of permissions should we have? what sions in the rwxrwxrwx form kind do we have? ¥ so just because you own the file doesn’t nec- • Changing Ownership essarily mean that you can write to it, or ¥ do an ls -l on some files execute it if its a shell script ¥ demonstrate the two following programs · so what things can you do to a file? · chgrp - group · well unix defines only three (there are more that could be useful, but unix is old and the · chown - owner spec. hasn’t changed with the times) • Changing permissions · these three are: read, write and execute · chmod - will also change ALL aspects of a · can set these three permissions for any cat- file if wanted egories: user, group, others ¥ demonstrate some standard examples - · so the permissions look like: rwx|rwx|rwx make file world readable · the first rwx is the user, the second group ¥ switches u, g, o and the +-= syntax, with and the third others perms r,w,x,X ¥ have an exercise to figure out what the per- ¥ also cover -R for recursive missions of several files mean ¥ since may want to change all files in a ¥ so when you see rw-rw-rw what does this directory mean? etc. ¥ now on to octal permissions • special files - directories ¥ so rather than writing out a bitmap each · directories are files too, though can’t time you want to say what the permissions read/write to it as you would a regular file of a file are, what would be a more compact · but because are files, they have to have the representation? same permissions that other files on the sys- ¥ use permissions base 8 tem has ¥ they are permissions base 8, because each · unfortunately the permissions on a direc- permission group (user, group, others) can tory act a bit differently (which you might be represented by a number from 0 to 7 expect) ¥ if we write out 0 to 7 in binary we see that ¥ list how permissions affect a directory file we require 3 bits (hmmm!) · r can list with cd ¥ ___ so these bits have value 4 2 1 and are · x can access the file within mapped to r w x · w means can write a file to that directory ¥ 4 2 1 ¥ so 7 looks like 1 1 1 • dot-files ¥ and 4 looks like 1 0 0 ¥ so when you type the ls cmd, do you see ¥ combining this we can write permissions out every file? as a group of three numbers 0 to 7 ¥ obviously not since I’m asking this question ¥ permissions look like XXX ¥ do an example of a hidden file - .hidden or ¥ the first X is user, the second is group, and ”.you don’t see me” the third is others ¥ is that all the files in the directory?

21 ¥ not necessarily... ¥ therefore .. is the parent directory of ¥ ls -a the current directory ¥ ../ is the same as .. · ”-a” switch will show ALL files in the direc- tory ¥ cd .. · this is useful because dot-files are generally ¥ so can probably guess that is you see used to hide configuration information for ¥ /foo/bar/../baz/box/ programs away from the user ¥ you know where this refers to? where · also keeps the configuration information in does this go? your home directory so that each user can ¥ answer: /foo/baz/box/ have their own settings as they like ¥ so you can’t ignore these, but can be ¥ some examples of .files useful for when you need to craft a path ¥ one common one you may care about is your to a file .zshrc, this controls some of the settings you • other special files have in your shell as you login ¥ another is .emacs - controls the settings for · tilde (~) denotes your home directory emacs ¥ cd ~ ¥ you can also have .directories · will take you to your home directory, a plain ¥ these are directories that are hidden cd will do the same ¥ do the same thing as .files (hide config info), · username denotes the home directory of but for programs that may have many con- that user figuration files that need to be hidden ¥ so is it a nice thing that just ls doesn’t show • relative and absolute paths all the files in a directory by default? yes, but is still historical crap ¥ can refer to files in two different ways ¥ a better solution might be possible ¥ what if you have written a program and ¥ show a gftp listing of my home on farmboy want to access a certain file - config.cfg ¥ so when you do a ls -a, do you recall seeing ¥ how do you refer to this file? two files one named ”.” and the other ”..”? · two ways: relative and absolute path · probably not but these are two special files · absolute paths never change and describe in · the file named . is this directory ”absolute” terms where a file is in the tree ¥ do a literal translation for whatever di- ¥ emacs /home/mkegel/school/fall05/graphics.txt rectory in on board ¥ so if you type · absolute paths will always start with / ¥ cd . · you are starting at the root and working ¥ you stay in the same place down the tree ¥ what if you see a dot in the full path · relative paths start with a ./ - the directory name of a file? tree before the ./ is not known ¥ ex. /foo/bar/./baz/box/ ¥ don’t necessarily know where the program ¥ where is this referring? will be stored, but always know where the ¥ to /foo/bar/baz/box/ config.cfg file will be in relation to the pro- ¥ so it means nothing, should just ignore gram it ¥ or should at least know where it will be ¥ ./ is the same thing as . ¥ can refer to it then by saying · the file named .. is the parent directory ¥ cmd ./one/two/config.cfg ¥ you should already know that if you want to go up in the directory hierarchy ¥ the ./ is filled in by the OS or the shell - that you type depending on context

22 16 Links - symbolic and • Creating Links hardlinks ¥ ln [OPTIONS] target link_name ¥ so now that we’ve confused you this much what · the basic syntax is backwards, so to ”link” if you wanted to have two files have the same emacs to emacs-21.2 you would type name? ¥ ln emacs-21.2 emacs ¥ why would you want this? · to create one pass the ”-s” switch to ln ¥ ln -s emacs-21.2 emacs ¥ how about when you upgrade software - could have the program named emacs, but the /real/ · this will create a soft-link name might be emacs-21.2, an older version ¥ show output of ls -l could be emacs-20.7 · symlinks don’t always work, for example ¥ you want to continue to access this program when trying to get to things through ftp through the same name, but have it always refer - have to mount instead to the new version

¥ how might you do this? 17 File globbing

• File Links ¥ most people are probably familiar with file globs, but for now, what is a file glob? · unix uses a thing called links · links are a special type of file which trans- ¥ why might you want file globbing? parently redirect you to a different file · many times will want to work with multiple files · so you can type emacs, but the OS will know that you really mean emacs-21.2 · say you want to mv or cp or rm all the files in a directory · there are two types of links: soft and hard links · how do you do that?

• Types of links · you use a thing called file globbing

· a hard link, which was what we created · is very similar to UNIX regular expressions, but above, is literally a different name for the should not confuse them same file · globbing is provided by the shell · the new name is stored in the directory its supposed to be in so takes up virtually no ¥ this is key - is one of the many complaints the memory people have about unix · the hard link literally points to the same · the shell is responsible for expanding globs data on disk · a hard link cannot cross file-systems (say ¥ programs generally only take file names and you have two different hard drives and want don’t generally do pattern matching to have one file refer to another) ¥ why should is be the responsiblity for the shell · you cannot hard link to a directory to expand globs? is this a good thing or a bad · a soft-link is different and more flexible one? · it is a separate file (not just an entry in a ¥ some people see this as a bad thing - for you you directory file) that the OS interprets as the don’t have to care real file, basically just a type of pointer · some standard globs - see page 658 U.P.T. · soft-links can go across file systems and can point to directories · * - matches all files in a directory (not dot- · in practice you should probably almost al- files though) - match anything ways create soft-links · will not match dot-files, however · can link both directories and regular files · ? - match one character

23 · [a-z] - any character a to z - character ¥ and then we come to the way the directory classes structure is organized · (x|y|...) - match either x or y - this is ¥ what do I mean by this? not pipe! ¥ so with windows you were probably com- · {a,b,...} - expands to ”a” ”b” fortable having all your programs be in- stalled to C:\Program Files\Program; so ¥ give some examples and ask what they would everything you installed was in a common match place · a[a-z]* ¥ what if some one decided one day · ?[c]*c to instead install everything to C:\Your Programs\Program and ¥ can put these anywhere in the expression C:\My Programs\Program; but all the old ones you had would (by default) install ¥ by far the most common is to do to the old directory (Program Files) ¥ ex1*ex2 ¥ unix has a ”unified hierarchy” ¥ *ex3 ¥ this would be quite a pain ¥ ex4* ¥ in unix the same thing exists, but across the different unixes that exist ¥ to match dot-files have to put a dot explicitly ¥ of course unix is supposed to be indepen- ¥ to match all dot-files in a directory would dent of the directory structure on which its do .* working, just makes it a pain for people in ¥ show listing of ls .* real life ¥ note that this also lists ”.” and ”..” ¥ this is an historical artifact ¥ so what would happen say if we did this... ¥ in order to gain market share one thing they ¥ chmod -R a+r ./ teach you in business school is to differen- tiate your product ¥ as root from /home? ¥ so when unix began to come out in various ¥ much badness is what...since more than just commercial flavors the companies changed the down directory will be changed things a bit to reflect how their product was ¥ .. follows to its parent and so you’ll also different change all the files in all the directories at ¥ the most obvious and easy way to change the same level and below in the tree an OS is to change where you put things ¥ draw out to illustrate ¥ so you end up with apache (a web daemon) being in four different places in four differ- 18 The tree of life - How the ent flavors of unix Unix directory tree is orga- ¥ luckily the madness has continued!!!!! ¥ linux suffers from the same problem though nized perhaps not to the same extent • Unix Directories ¥ where things are put in debian is different from gentoo, is different from... ¥ now that you understand how to work with ¥ FreeBSD has some resemblance to the linux files, how to work with programs should directory structure similar know a little bit more about the environ- ment you are working in ¥ just to limit the confusion we’ll just go over ¥ unix is in some way a very structured envi- the basics which apply to most unix sys- ronment tems, except for OS X which is very differ- ent ¥ 35 years of tradition have specified how you do certain things and why they are the right · *now the real material* things to do ¥ draw a /

24 ¥ so we know that everything in unix starts ¥ libexec/ system daemons and system at / utilities (executed by other programs) ¥ so how do we decide what goes under this? ¥ local/ executables, libraries, etc. not included by the basic ¥ we don’t get to so much, though if you want create your own distro... ¥ sbin/ system daemons and system util- ities (executed by users) · so lets start with something you already ¥ share/ architecture-independent data know /home files • General Directories ¥ share/man/ manual pages ¥ src/ storage for kernel/other source ¥ cd into each directory as they are being dis- code under linux cussed ¥ /var/ multi-purpose log, temporary, tran- ¥ /home is where the heart (and your home- sient, and spool files work) is ¥ log/ misc. system log files ¥ in some sense this is true since this is where the home directories of all the users on the ¥ /lib - shared libraries and kernel modules system are stored ¥ /opt - larger static packages - where you ¥ the home directories generally have the would install KDE for example form /home/username, though this isn’t ¥ /boot - kernel image and other boot files strictly necessary, just useful ¥ this is where the linux kernel gets put ¥ rest of this is taken from ¥ is sometimes a separate partition - Red ¥ man hier (on OSX, also present on Hat does this FreeBSD, haven’t seen it on Linux) ¥ probably a good idea since if your main ¥ see also pg 822 UPT one gets fried can still boot ¥ /bin/ user utilities fundamental to both ¥ /mnt - temporary/permanent mount point single-user and multi-user environments for devices ¥ /dev/ block and character device files fd/ ¥ devices can be mounted anywhere, so file descriptor files; see fd(4) one solution, if you have lots of hard ¥ /etc/ system configuration files and scripts drives, optical drives, cameras, scan- ners, etc. is to make lots of mount ¥ X11/ configuration information for X points under /mnt, so /mnt/dvd/, · NFS: exports /mnt/ipod, etc. · system: group, passwd, and shadow ¥ is this a good thing? · shell: profile ¥ all up to your own preferences · sudo: sudoers ¥ /mnt is meant for (at least in FreeBSD) · network and inet: hosts, hosts.allow, a single device that won’t be mounted hosts.deny, hosts.equiv for any length of time · lots of other stuff ¥ would mount to a different point, say ¥ /sbin/ system programs and administra- /ipod, so mounted right from root tion utilities fundamental to both single- ¥ are multiple solutions, could also have user and multi-user environments a /vol (/Volumes) ¥ /tmp/ temporary files ¥ /usr/ contains the majority of user utilities 19 Some special devices and applications • Special files ¥ bin/ common utilities, programming tools, and applications · /dev/null ¥ games/ Unix games, also fortune · this is a sink for input, you can send ¥ include/ standard C include files anything here and it will just go into ¥ lib/ archive libraries the void

25 · useful for when you need a place to get ¥ printenv rid of output, but don’t want to waste · what about shell variables? space on the disk ¥ set (lists everything: functions, shell, and · if read from will give you an EOF env. variables) · /dev/zero ¥ pg 705 has a good listing of pre-defined en- · produces an infinite stream of zeros if vironment variables read from, good for when you need to null something out • Common Variables · if written to will just give a permission · good ones to know about - will be all caps error (?? Is this always true ??) · PATH - when you type in a command · /dev/random and /dev/urandom these are the directories that the shell · when read produces a random stream will search for the cmd you have typed of bytes · HOME - where your home directory is · permission error when try to write to located · HOSTNAME - name of the system you are on 20 Environment Variables and · SHELL - full path name of shell you are Shell Variables using · USER - who you are logged in as • Variables · UID - your user id · these are special variables that all programs · TERM - type of terminal you are using know about · they exist in the background and let pro- 21 The Different Shells grams know about certain things · where to find programs to run, which ¥ now is a good time to talk about what shell you editor to use, etc are using · serves the same purpose as the registry ¥ there are in fact many different shells! (sort of) in windows ¥ you’ve probably been using zsh, the knuth de- ¥ cover the distinction between the two fault, all this time with ever knowing it · environment variables are inherited across all shells you may have and all ¥ there are a multitude of shells, each with differ- programs that you run ent strengths and features · shell variables are local to each instance ¥ most fall into two different categories of a shell that you may have · each shell gets a clean slate of shell vari- ¥ those based on sh, which was the first shell ables when it starts ¥ those based on csh · analogy - programming: global versus ¥ give proper pronunciation lesson local scope · variable naming convention ¥ the default on knuth is zsh, which is a hybrid of sh and csh · CAPS are environment · lower-case are shell ¥ other shells include bash (probably the most widely used shell), tcsh, ksh (Korn-Shell) · please follow convention!!! ¥ you generally only ever care about environ- ¥ other random shells: ash, fish, pdksh (free ver- ment variables sion of Korn-shell) ¥ show listings of the variables ¥ probably the best (certainly the most fully fea- · how do you see what environment variables tured) shell is zsh you have? ¥ we recommend that you use either zsh or, if you · simple use the printenv command have to, bash

26 ¥ why use zsh? ¥ you can see the output of a variable by using the echo cmd ¥ its just plain nifty ¥ echo $ENV ¥ will tab complete many very cool things, ¥ and even not so cool ones have to remember the $ ¥ ¥ e.g can tab complete kill and man for ex- demonstrate this for the students ample ¥ the most common thing to do is to add paths to PATH ¥ also we are only going to demonstrate for this shell, you have the tools to figure out ¥ may have to do this for a class how to do the same things for yourself on ¥ may want to do this depending on your own shell which box you are logged into ¥ so how are they different? what really distin- ¥ from the cmd line for zsh we would type guishes a shell? ¥ echo $PATH (to see path) ¥ in terms of user interface, shells are almost iden- ¥ export PATH=$PATH:/newpath tical · or -

¥ shells provide the ability to run more one com- ¥ export PATH=/newpath:\$PATH mand at a time ¥ the two are different!

¥ if you string commands together in a file, with ¥ first puts the new path at the end some control structures like if, while, and case ¥ the second puts the new path at the front statements: you get a language ¥ is important because when searching for the ¥ called shell scripting cmd to run, the shell will take the first one that matches, so you want to make sure its ¥ fundamental different between csh and sh - csh matching the right cmd is more based on C syntax ¥ unfortunately you would have to do this every-time you logged in ¥ sh is just funky and has a weird syntax, but for shell scripting is better (better control over out- ¥ would get boring put and input) • Setting Env. Variables ¥ so when we say a shell is based on another, what ¥ maybe there is an easy way fix this prob- we really mean is that zsh or bash can run scripts lem? written for sh ¥ ask crowd for suggestion ¥ and that zsh and bash offer scripting options that ¥ real answer: this is why you have dot-files weren’t available with sh ¥ we know dot-files hold configuration infor- ¥ the same for tcsh mation ¥ we won’t show you shell scripting yet, but you ¥ zsh is no exception to this rule will learn about it ¥ zsh has several files that hold login infor- mation 22 Setting Environment Vari- ¥ /.zprofile ¥ /.zlogin ables ¥ /.zshrc • Setting Env. Variables ¥ and more ¥ depending on default setup will want to ¥ to set environment variables in zsh show how to do this add path ¥ export NAME=value ¥ the one that you care about is .zshrc ¥ can also do this ¥ once you change your .zshrc it won’t imme- ¥ NAME=value; export NAME diately take effect

27 ¥ either you have to log out and restart your · so you can refer to a complicated, or repet- shell or you can apply the change from the itive, command with a smaller, easier to cmd line type alias ¥ to apply from the cmd line use the source ¥ you always want to use these when you are cmd typing some long string over and over again that you cannot just tab complete ¥ source .file ¥ is very much like a macro ¥ will cause the shell to read the configuration information, and apply the changes ¥ what is one common thing that you might be typing? ¥ but this doesn’t cause the shell to reset its old configuration, it just overwrites what it ¥ how about ”ssh [email protected]” changes ¥ so to create an alias we need a name: how ¥ so your PATH will be two copies of your about sshtoknuth old path followed by the added directory ¥ from the cmd line would type: (spacing is assuming you put it at the end important) ¥ not always what you want, and shouldn’t ¥ alias sshtoknuth=’ssh [email protected]’ cause any problems unless you’re doing ¥ can also use ” when creating alias’ (good if some funky things in your .zshrc or .zlogin you want to reference env. variables) ¥ *changing the prompt* ¥ this alias is, of course, only good for so long ¥ well if you can change your PATH what as the shell is open other things can you change? ¥ once you quit the alias is gone ¥ lots of things, but the most fun to change ¥ the basic syntax is is the prompt ¥ alias name=’cmd arg1 arg2’ ¥ demonstrate some nifty prompts in zsh and ¥ but what would be a good name? some- how to change them thing better than sshtoknuth ¥ other shells options ¥ the whole point of the alias (for users) is to ¥ shells are complex beasts and offer loads of make your life easier options, one of the most annoying is the ¥ so want to choose a simple name that is easy shell beep to remember ¥ to alert you to an error when you mistype ¥ how about: knuth something, the shell will cause the com- puter to emit a beep ¥ says what you want, is simple and short ¥ this is the most annoying thing ever ¥ what would have happened though if we’d aliased this to ssh? ¥ to turn it off we would use the setopt com- mand that zsh has ¥ question of precedence...and the alias wins ¥ setopt nobeep ¥ in zsh and bash when you type something on the cmd line that you want to be taken ¥ we would put this line in our zshrc; by de- as a cmd, then if that text string has been fault it should already be there aliased to something else it will be replaced ¥ there are lots of options so check them out! ¥ is this a good thing or bad thing? ¥ may want to read through the default zshrc ¥ used in the appropriate places is OK to see what’s set ¥ in general, however, is a bad thing because then you get used to a potentially broken 23 Using alias in the shell or misleading command action. ¥ almost always want to use a new name • ALIAS ¥ one example is ls ¥ it is a builtin cmd that allows you to ”alias” ¥ by default what you get on knuth is not one set of commands to another /usr/local/bin/ls

28 ¥ instead you get ”/usr/local/bin/ls -F” • Useful Applications ¥ do an example of what each outputs ¥ these are almost all graphical tools and are ¥ discuss why this might be a bad thing, tell essentially replacements for programs that about -G on GNU ls (hide groups) students would be using on either their Mac ¥ should watch which options you pass by de- or Win box fault · web browser - firefox and mozilla, kon- ¥ so how did I know that all of you had this? queror ¥ demonstrate the defaults alias’ that people · text editing - jedit, emacs, vi have in zshrc · word processing - abiword, open office ¥ for certain programs you’ll want the same · document creation - latex behavior every-time you login · spreadsheet - gnumeric, open office ¥ so to make an alias more or less permanent · music - xmms, amarok (my favorite) you would put it in your zshrc · movies - totem, xine, mplayer, vlc ¥ so if you wanted the alias we talked about before you would add this line · graphical terminals - eterm, aterm, xterm, rxvt, gnome-terminal, konsole ¥ alias knuth=’ssh [email protected]’ · mail - thunderbird, evolution, pine/mutt ¥ spacing is important so follow closely through xterm · pdf viewers - xpdf, kpdf, gpdf 24 More about zshrc · ftp - ncftp or lftp (cmd line), gftp

¥ so the zshrc can set options and aliases for you, · cd burning - k3b what else can it do at login? · aim - gaim, kopete · bit torrent - azureus ¥ the at login is important since you can do this all from the cmd line; just gets old if you want · file manager - konqueror, nautilus it every time · spell checkers - ispell and aspell • creating files and the umask · Windows like desktop - GNOME, KDE · window managers - fluxbox, xfce, window · by default every permission is ON, so its maker, enlightenment your job to turn them OFF · the umask is the thing that does this ¥ show in zshrc 26 su and sudo ¥ written out in octal permissions (remember ¥ up to this point have been fairly general, infor- those?) mation applies to almost all users of a *nix sys- ¥ write out the permissions of the umask tem ¥ ask what the permissions of a newly created ¥ this information is really only useful to those file would look like who will using Linux on their own system, not ¥ ask if the results are good or not so much for those people who are just going to ¥ what might be a good umask; what would be log into knuth a bad one? • sudo and su ¥ discussed before how use 25 UNIX programs for Win- · su allows you to assume the role of the supe- dows geeks ruser (aka root; is default behavior) without having to directly log in as root · there are many different tools that unix offers ¥ su · you should now know how to make them work · allows you to become that user (changes together and some of why they work as they do, your UID and GID), but have to know their but don’t exactly know what tools are out there password

29 · if you have first become root, you can do ¥ one of the most basic things to configure su without a password on a newly installed linux system are the · sudo allows you superuser powers without partitions and how the hard disk will be having to become root - more fine grained managed control than with su ¥ in windows separate partitions would just · sudo is flexible, you can have limited pow- magically show up (not always, but most ers, or have the whole shebang times) · sudo privileges are controlled in ¥ more too it than this, but the management /etc/sudoers of the disks was done mostly by the oper- ating system · is a regular text file, like almost all config- uration files on unix ¥ also in windows the most the you would re- ally ever do is put the disk/partition at G:, · can add powers for users, or groups H:, or so on · generally only modifiable by root (kind of a security risk otherwise) ¥ other devices were located at D: (generally a cdrom or dvd drive) ¥ can also get a shell out of sudo by using the -s switch ¥ A: - floppy drive ¥ C: - your main disk - the disk you booted • editing /etc/sudoers off of ¥ demonstrate adding · these ”locations” are known as mount ¥ mkegel ALL=(ALL) ALL points ¥ %wheel ALL=(ALL) ALL · in unix you don’t have a:, c: ¥ %special ALL=(root) · instead disks and other devices can be NOPASSWD:/usr/bin/myprog ”mounted” most anywhere ¥ joe user local host=/sbin/prog ¥ that is if you have a newly installed hard · the basic format is: drive you have to decide where in the USER HOST=[(RUN_AS)] CMD filesystem tree that drive will be accessed ¥ see page 992 of unix power tools ¥ this is what makes the file system(s) trans- parent to both users and programs ¥ would you ever want to let a person run emacs ¥ draw out a diagram of how multiple (or vi) with sudo? no: they would have access drives/cdroms/devices can be accessed on a to a root shell...bad thing need to watch out for? filesystem tree reason why you would want: let them edit sys- tem files ¥ generally mount stuff in /mnt, but can be anywhere ¥ why might you need to use sudo and su? what kinds of things should be be an administrator to ¥ can also mount what you might consider do? important directories from other drives ¥ for example: /home on my system is an- other partition that I created for the ex- 27 Mounting partitions press purpose of holding everything that I would want in my home directory ¥ this information is really only useful to those who will using Linux on their own system, not ¥ / holds everything else I need for my sys- so much for those people who are just going to tem, but is separate from what I have in my log into knuth home directory ¥ this is a nice because it is very flexible and • mounting partitions* completely transparent to you and to any ¥ when managing a system you will almost program that you might run always be modifying ”files” that ”normal” ¥ one example of this being done is with users should never touch each of your, and mine, home directories ¥ these files are generally configuration files on knuth

30 ¥ your home directories are mounted locally ¥ hard drives are generally given the designa- on knuth when you log in, but where they tion hdXN, where X is [a-f] and is the drive were before that doesn’t really matter and N is [0-9] ¥ obviously knuth can’t have enough physi- ¥ so hda1 would be the first partition on cal space to hold everything that we might the master hard drive want - home directories, staff accounts, ¥ hda2 would be the second partition projects, random data, backups ¥ hdb would be the second hard drive ¥ all this disk space is provided transparently ¥ drives have up to 4 primary partitions, across a network through something called and can have more logical partitions NFS or network file system (or No Fucking ¥ generally if you just say hda, or hdb you Security, nfs assumes you are on a trusted mean the entire device and not any one network) partition ¥ so your home directories are located on one · scsi drives (like what you’d have under machine, while you are working on knuth firewire) are denoted the same as hard and doing things to your files, editing, com- drives except with sd instead of hd nota- piling, etc. tion ¥ however that was just a setup for what · another would be /dev/input/mouse0 (on you’ll likely do on your own machine my system at least) for the mouse • mounting devices ¥ most often though will deal with hard drives and partitions or other comparable (NFS) · in linux, we won’t worry about bsd or other things *nix, devices are located in a /dev like we ¥ so then how do you get a partition talked about before mounted? · a device is of course just a file - is actually ¥ easy with the mount cmd a special file called a device node - tells the system where in the system the device can ¥ mount is fairly smart, can usually figure out be found (for PCI devices major and minor which file system is on the device number of device) ¥ note to windows users: Most any computer ¥ device node != regular file or directory with XP will use NTFS which is a filesystem that linux can only read (trouble writing) ¥ one thing to note: the linux 2.4 kernel is different from the 2.6 kernel in one major ¥ FAT32 will work just fine way -> the 2.6 kernel does use the /dev ¥ to use mount you need a directory on which filesystem approach to devices; 2.6 is just to mount, so if we were to mount /dev/hda4 more intelligent and doesn’t enumerate ev- on /mnt/windows, then windows would ery possible device need to be a directory under /mnt ¥ in the 2.4 kernel every device that you • mount command could possibly have attached to the sys- tem was enumerated and given a device ¥ also to use mount you have to be root or node in /dev have root powers (sudo)! ¥ in 2.6 the kernel is a little bit smarter ¥ mount /dev/device /mnt/mntpoint about devices now and can handle ¥ to specify the file system use the -t switch them with some intelligence like such ¥ for you this won’t matter too much un- ¥ less you’re trying to hook in some ex- mount -t vfat /dev/hda4 /mnt/windows otic device ¥ to un-mount the device use the umount ¥ or if you have sound/video/usb prob- command like such lems (or want to use these devices) then ¥ umount /dev/windows you’ll get to know /dev pretty well ¥ also cover --bind (GNU, different on BSD) ¥ generally a device will be owned by root and - can bind one filesystem to two points - in one of several groups: disk, video, audio, useful for when symlinks won’t work (net- root, etc. working stuff)

31 ¥ when you are setting up a system its quite helpful · to modify fstab you have to be root to know which tools are available (wouldn’t want random users messing with this) ¥ most systems you get will either come with win- dows installed, in which case the entire hard · fstab tells the system which filesystems are drive is one windows partition to be mounted at boot time · also lets the system know which mount ¥ or you got a new hard drive points the system has ¥ (Windows Case) · so say I wanted to mount windows then all i would have to type would be ¥ You’ll want to get your hands on a program called Partition Magic ¥ mount /mnt/windows ¥ another good program is GNU Parted · and the system would auto-magically know ¥ if you know where to look on the network what to do (ffs.muddstudents.com *ahem*) you might · you also use fstab to mount NFS exports, be able to find the program but we won’t cover that here ¥ it can do most anything that you’ll want, · do not fubar this file, will make you un- including converting, moving and resizing happy Windows partitions ¥ you probably shouldn’t let Partition Magic 28 Using cron touch any linux partitions ¥ (New HD Case) · cron is a daemon that runs in the background and schedules tasks ¥ you’ll want to use cfdisk or fdisk to create new partitions ¥ such tasks might be backing up a hard drive, updating the locate database ¥ use mkfs to then format the partition ¥ this configuration information is stored in a ¥ (General Case) crontab file ¥ the cmd df will show how much space is ¥ available on each mounted filesystem generally you just give crontab a file containing what you want done and when - this can be done ¥ df as a regular user ¥ the cmd du will show you how much space something is taking up ¥ crontab mysched ¥ du · also, edit the crontab with crontab -e ¥ useful alias · mysched will then contain single line entries for ¥ alias sizeof=’du -sh’ each task that you want cron to do

• /etc/fstab · each line is formatted like this ¥ So I mentioned that I have /home mounted ¥ mins hrs day-of-month month weekday [username] cmd from another partition ¥ this happens on boot, but how? ¥ the username is only there in BSD systems, but check your local docs first · /etc/fstab is a configuration file that lets the system know which devices are to be ¥ you can give a single number (7), range (3-8), mounted, where, who can mount, and with list (5,7,9), or * to indicate all valid numbers for what other options (executable flag being a given entry one) ¥ ¥ show my /etc/fstab - point out what each mins: 0 to 59; hrs: 0 to 23 (military time); day: column is 0 to 31; month: 1 to 12 weekday: 0 (Sunday) to 6 (on BSD it goes from 1-7 with 1=Sunday) · there are other things in things in fstab than just hard drives, but you can ignore them ¥ show a random cron example

32 ¥ something scary for Friday the 13th, and some- 31 Sharing files under linux thing cheerful for Christmas, and school starting every Monday ¥ use a package of programs called samba ¥ ¥ cron can also make your life easier by just run- allows you to share and access windows network ning things on a consistent schedule - say you shares need to run things on a hourly, daily, weekly, or ¥ really easy to use but has issues with some of the monthly basis newer versions of the windows sharing protocol - may not be able to access all files ¥ cron will run scripts that you put into /etc/cron.xxxx/ ¥ better yet why don’t you just use ftp?

¥ where xxxx is daily, hourly, etc ¥ ftp is much easier to setup, and you don’t have to dirty yourself with Windows slime ¥ on those intervals

¥ you’ll have to be root to put stuff in these direc- 32 Emacs and vi tories though ¥ please use emacs, or if you have a graphical en- vironment something like jedit

29 - Windows ”emula- ¥ vi should only ever be used in the most desperate tion” for linux of circumstances ¥ emacs and vi ¥ use wine!! ¥ hand out reference cards ¥ there isn’t much else to tell you ¥ demonstrate some nifty tricks (M-q for one) ¥ install wine ¥ how to exit and save ¥ F10 is menu in emacs ¥ do this from the cmd line ¥ vi is a modal editor - some people ”just get ¥ wine WinApp.exe this” way of editing ¥ ¥ hope for the best; does not work for every appli- not our goal to teach students to use these cation ¥ and besides by this point in the semester you should already know how to use emacs ¥ if you want to run games (or itunes, or quick- time) then you’ll need a thing called cedega 33 More topics to talk about ¥ there is good documentation, you just need to find it ¥ shell scripting

¥ go read Unix Power Tools 30 ¥ demonstrate a for loop on the cmd line how- ever ¥ can get lots of GNU programs ¥ perl and python ¥ can get X11 ¥ both powerful interpreted languages ¥ runs on Windows ¥ easy to learn and use; generally only need to know one but should learn at least one ¥ get a poorly implemented version of a terminal ¥ checkout these sites: ¥ but can do unix-y type stuff on windows if you ¥ diveintopython.org and python.org absolutely have to ¥ perl.org and perl.com

33 ¥ cannot stress how useful knowing a lan- ¥ grep ’fo?’ guage like one of these is ¥ would match f, fo ¥ learn it! ¥ grep ’f[oe]*’ file1 ¥ logging in through ssh ¥ would match f, fo, foe, fe, fee, etc, but not ¥ how to X forward with ssh (-X | -Y) fie ¥ how to use ftp/sftp and scp ¥ grep ’^f[oe]{1,2}$’ file1 ¥ why ftp is a bad idea - any password send in the clear ¥ would match fo, fe, foe, feo, fee, foo, but not f or fooo 34 Things I left out ¥ grep ’f[^aieou]*’

¥ speaking of other frustrating things also mention ¥ would match f followed by 0 or more non- the C-q and C-s control flow ”bug” vowels ¥ C-q resume console I/O ¥ grep ’(fa|e)o*’ ¥ C-s stop console I/O ¥ would match fa, faoooo, e, eoooo, but not fae 35 Regex Info ¥ Note that grep ’fo+’ is equivalent to grep ’foo*’

¥ 3 slides for this topic ¥ sed is generally used for substitutions. There are two really handy commands for sed: s(ubstitute) ¥ metacharacters: and d(elete). Coupled with addresses, you can do some really cool stuff. . * + ? [] ^ $ { } \ () | ¥ By default, sed applies the commands to every ¥ POSIX classes: [:alnum:] [:digit:] [:blank:] etc line. You can change this by specifying an ad- dress pattern to use. Patterns can be line num- ¥ two main commands that we’ll cover that use bers, regexes, or things like $. regular expressions: sed and grep ¥ No address -> apply commands to every line ¥ grep just searches for the given pattern ¥ 1 address -> only on lines that match the address ¥ sed can do that too, but often sed is used to string replace things ¥ ¥ grep ’foo’ file1 2 addresses -> only on lines between the first line that matches the first address and the first line ¥ returns every line that contains the string that matches the second address ’foo’ ¥ You can negate addresses by appending a !. ¥ grep ’^foo’ file1 ¥ So, just ’d’, without an address, will delete every ¥ returns every line that starts with foo line.

¥ grep ’foo$’ file1 ¥ 1d will delete just the first line. ¥ returns every line that ends with foo ¥ 1,5d will delete lines 1-5. ¥ grep ’^foo$’ file1 ¥ $d will delete the last line. This is not the same ¥ only lines that are exactly ’foo’ as $ = end of line. ¥ grep ’fo*’ ¥ /^$/d will delete every blank line. ¥ would match f, fo, fooooooooooooooooo, ¥ 1,/^$/d will delete from the first line until the etc first blank line.

34 ¥ All the addressing stuff also applies to the ’s’ 37 screen info command, but to clarify exactly what ’s’ does, we won’t mix addressing and s. ¥ 2 slides for this topic ¥ screen is basically a way to have multiple ter- ¥ s/tree/cat/ replaces the first instance of ’tree’ minals inside one standard terminal window. with ’cat’. (Think job control, but instead of pausing one job to go back to the shell, you could simply have ¥ s/tree/cat/g replaces every instance of tree a bunch of shells running, only one of which was with cat (g for global). in the foreground at any given time.) In the days of large resolutions and good terminal emulators ¥ s/tree/(&)/g replaces every instance of tree and window manager, screen isn’t that handy with (tree). & references the entire match. for doing stuff on a local session. It is, however, extremely useful for managing shells on remote ¥ s/tr\(ee\)/\1/ replaces ’tree’ with ’ee’. sessions.

¥ s/\(tr\)\(ee\)/\2\1/ replaces ’tree’ with ¥ Make a new (default) session: ’eetr’. ¥ screen

¥ s/\(tr\)\(ee\)/\2\2\2\1/ replaces ’tree’ with ¥ Resume the default screen: ’eeeeeetr’. ¥ screen -r ¥ s/<\([^>]\)>\(.*\)<\/\1>/[\1]\2\[\/\1]/ ¥ or replaces ’foo’ with ’[b]foo[/b]’ ¥ screen -x (like -r, but better – allows multi- heading) 36 tr info ¥ Make a new screen session with a title:

¥ 1 slide for this topic ¥ screen -S foo ¥ Resume foo: ¥ tr is very simple. It takes stuff it finds in the first argument and replaces it with the stuff it finds in ¥ screen -x foo the second argument. It does take position into ¥ account: something found in the 17th position in You can also use just -x f, if you don’t have any the first argument will be replaced by the thing other screens starting with ’f’. in the 17th position of the second argument. ¥ If you’ve got a un-resumable screen session, tidy it up with: ¥ echo "abc" | tr ’a’ ’z’ = zbc ¥ screen -wipe ¥ echo "abc" | tr ’[a-z]’ ’[A-Z]’ = ABC ¥ Show available sessions:

¥ echo "abc" | tr ’[abc]’ ’[cba]’ = cba ¥ screen -ls ¥ The screen commands I use a lot: ¥ It’s also handy for deleting newlines or some other character you may have a particular ha- ¥ ^a c make a new shell tred of: tr -d ’\n’ ¥ ^a switch to the next shell ¥ This is what you would do if you really hated ¥ ^a k kill the current shell your grader ¥ ^a d detach; go back to the original shell leaving ¥ strip out all comments the current session intact ¥ remove all newlines ¥ The screen commands I use not so much:

¥ get a failing grade on that assignment ¥ ^a l redraw the screen in case things got funky

35 ¥ ^a a send a literal ^a

¥ ^a ? (shift /) show help

¥ ^a " (shfit ’) show current shells in a list

36