Introduction to Unix

BorisBoris KonevKonev [email protected] http://www.csc.liv.ac.uk/~konev Important information Instructor: – Boris Konev – Office: 1.12 – Email: [email protected] – Web resources: www.csc.liv.ac.uk/~konev/COMP110

NoNo lectureslectures onon Monday!!Monday!! Objectives • Basic structure of the • What makes Unix special? • Unix System Design • Essential commands • Essential concepts • Shells • programming • Text processing • Unix utilities Why Bother Studying Unix? • Most powerful computers (supercomputers) use the Unix operating system • Unix runs the Internet • Unix is used as an embedded system in hardware – networking, user appliances (DVD players) • A knowledge of Unix is likely to be helpful in your future career, regardless of where you pursue it. History of Unix

1960s “multics” project (MIT, GE, AT&T) 1970s AT&T Bell Labs 1970s/80s UC Berkeley 1980s DOS imitated many Unix ideas Commercial Unix fragmentation GNU Project 1990s Linux now Unix is widespread and available from many sources, both free and commercial Unix Systems • Commercial – SunOS/Solaris Sun Microsystems – Digital Unix (Tru64) Digital/Compaq – HP-UX Hewlett Packard – Irix SGI – UNICOS Cray – Mac OS X Apple – … • Free – NetBSD, FreeBSD, … – Linux – … What makes Unix so special? (1) • Portability (runs on almost every hardware) • Old and popular – it is easy to information and get help • Simplicity, elegance & flexibility • -sharing: multi-tasking & multi-user environment • Unix is very stable – computers running Unix almost never crash What makes Unix so special? (2) • Unix is very efficient – it gets maximum number crunching power out of your processor (and multiple processors) – it can smoothly manage extremely huge amounts of data – it can give a new life to otherwise obsolete Macs and PCs • System design decisions (made by programmers for programmers) • Free and open source flavors available (Linux, *BSD) • It is not Windows ☺ The Unix Philosophy

• A large set of primitive tools, which can be put together in an infinite number of powerful ways • An example: – Three separate tools are necessary to develop software: • Text editor • Compiler • Debugger – MSVC is an “IDE” (“Integrated Development Environment”) • All three tools are found in one shrink-wrapped box – (There are IDE tools for Unix) Unix Has Home Drawbacks

• Unix computers are controlled by a command line interface – NOT user-friendly – difficult to learn, even more difficult to truly master • Hackers love Unix – there are lots of security holes (first software viruses were Unix-specific) – most computers on the Internet run Unix , so hackers can apply the same tricks to many different computers • There are many different versions of Unix with subtle (or not so subtle) differences Mac OS X The K Desktop Environment (KDE) Gnome Operating Systems

Managing Hardware and Software

Disk Screen Keyboard Drive

Printer Application Software

Utilities Unix Structure

Kernel: controls hardware, creates processes, implements multi-tasking, manages file system Programs interact with the kernel • Unix Utilities: compilers, text processors,… • Shell: interprets user commands and sends them to kernel • GUI: based on X Window System • User programs Getting Started—Logging In Unix is a multi-user operating system • Login and password prompt to log in • login is user’s unique name • password is changeable; known only to user, not to system staff (!!) /like the debit card PIN number/ • Unix is case sensitive Passwords (1) Do: • make sure nobody is looking over your shoulder when you are entering your password. • change your password often • choose a password you can remember • use eight characters, more on some systems • use a mixture of character types – include punctuation and other symbols Passwords (2) Don’t: • use a word (or words) in any language • use a proper name • use information in your wallet • use information commonly known about you • use control characters • write your password anywhere • EVER give your password to anybody Command Line Interface (1)

A command is a program that tells the Unix system to do something. It has the form:

command options arguments

• “Whitespace” separates parts of the command line • An argument indicates on what the command is to perform its action • An option modifies the command, usually starts with “-” Example: $ ls –l –a Papers Command Line Interface (2) • Not all Unix commands will follow the same standards • Options and syntax for a command are listed in the “man page” for the command Some First Commands (1)

$ date Sun Feb 29 18:27:09 GMT 2004 $ who konev pts/0 Feb 27 18:15 konev pts/1 Feb 27 18:15 konev pts/2 Feb 27 18:22 konev pts/3 Feb 28 19:32 konev pts/4 Feb 29 18:17 (konev- lt.laptop.csc.liv.ac.uk) $ who –q konev konev konev konev konev # users=5 Some First Commands (2) $ finger

$ finger Who is logged in? $ finger $ man

UNIX Manual Pages Hands-on Practice • In the department: – “Open Unix session” • At home: – A range of free Unix-like systems is available • FreeBSD (www.freebsd.org) • NetBSD (www.netbsd.org) • SUN Solaris (www.sun.com) • Linux Linux Distributions • RedHat – probably the best-known Linux company in the world • SUSE – popular in Europe, especially in Germany. Acquired by Novell in late 2003. • Mandrake – French distribution (special attention to usability) • Debian – completely non-commercial project • Knoppix – great for trying out Linux without getting your hands dirty as it runs directly from a CD, no installation required (www.knoppix.com) The Hierarchical File System

Ordinary (plain) files - text, data, programs, etc Directories – files containing references to other files (folders) Special files - interfaces to hardware (e.g. /dev/rmt8), named pipes,… Unix Filenames • Unix is case sensitive (myfile.txt and MyFile.txt are different) • UNIX filenames contain only letters, numbers, and the _ (underscore), . (dot), and - (dash) characters. • Unix does not allow two files to exist in the same directory with the same name. – Whenever a situation occurs where a file is about to be created or copied into a directory where another file has that exact same name, the new file will overwrite (and delete) the older file. – Unix will generally may alert you when this is about to happen, but it is easy to ignore the warning. Filename Extensions • Most Unix filenames start with a lower case letter and end with a dot followed by one, two, or three letters: myfile.txt – However, this is just a common convention and is not required. – It is also possible to have additional dots in the filename. • For example, myfyle.txt.gz • The part of the name following the dot is called the “extension.” Some common extensions

• The extension is often used to designate the type of file. • By convention: – files that end in .txt are text files – files that end in . are source code in the "C” language – files that end in .html are HTML files for the Web – Compressed files have the .zip or .gz extension • Unix does not require these extensions (unlike Windows), but it is a sensible idea and one that you should follow The working directory At any given time when interacting with Unix, you are “working in” or “connected to” some directory. This is called your ``working directory.'' You will typically change your working directory (with the cd command, discussed later) several times during a single session. The command prints the name of the current working directory. $ pwd [print working directory] Files & Directories $ file $ ls $ ls –a $ ls –l To copy, move & remove a file $ cp $ mv $ rm Directories Making, removing & changing directories $ mkdir $ rmdir $ cd Displaying the current path $ pwd Wildcard Operators Asterisk * matches any character sequence, e.g. in file names Question mark ? matches any single character Brackets [ ] match specified characters $ cp *.doc WordFiles/ $ cp *.??? WordFiles/ $ rm ab*c $ ls art[12] Shortcuts • There are some important shortcuts in Unix for specifying directories – . (dot) means "the current directory" – .. means "the parent directory" - the directory one level above the current directory, so cd .. will move you up one level – ~ (tilde) means your Home directory, so cd ~ will move you back to your Home. • Just typing a plain cd will also bring you back to your home directory Unix File Protections • File protection (also known as permissions) enables the user to set up a file so that only specific people can read (r), write/delete (w), and execute (x) it. • Write and delete privilege are the same on a Unix system since write privilege allows someone to overwrite a file with a different one. File Owners and Groups • Unix file permissions are defined according to ownership. The person who creates a file is its owner. – Everyone is a user – You are the owner of files in your Home directory and all its sub-directories • In addition, there is a concept known as a Group. – Members of a group have privileges to see each other's files. – Users are in at least one group View File Permissions

A “long” ls listing shows file permissions: $ls -l … -rw------1 konev csc 477658 Feb 21 17:20 main_save.ps -rw------1 konev csc 2040 Feb 3 17:33 michael -rw-r--r-- 1 konev csc 365089 Jun 4 2003 old.ps drwx--x--x 3 konev csc 1024 Feb 21 17:29 one_file … The first character in the attribute string indicates if a file is a directory (d) or a regular file (-). The next 3 characters (rwx) give the file permissions for the owner of the file. The middle 3 characters give the permissions for other members of the owner's group. The last 3 characters give the permissions for everyone else (others) What You Can Do With Permissions

Permission File Directory

r (read) Read a file List files in …

w (write) Write a file Create a file in … Rename a file in … Delete a file in … x (execute) Execute a file Read a file in … (eg ) Write to a file in … Execute a file/shell script in … Changing Permissions • Only the owner of a file can change its protections • To change the protections on a file use the chmod (change mode) command. [Beware, this is a confusing command.] – First you have to decide for whom you will change the access permissions: » the file owner (u) » the members of your group (g) » others (o) » Next you have to decide if you are adding (+), removing (-), or setting (=) permissions. • Taken all together, it looks like this: > chmod u=rwx g+r o-x myfile.txt This will set the owner to have read, write, and execute permission; add the permission for the group to read; and remove the permission for others to execute the file named myfile.txt. The cat command Displaying the content of a file $ cat testfile $ cat < testfile Creating a file, line by line, ending with CTRL-D $ cat > newfile What happens with the following? $ cat Standard Input and Output Standard Input/Output: Keyboard/Screen Redirect using > and < $ ls > mylist (rewrite the file) $ ls >> mylist (append to the file) $ cat < oldfile > newfile $ cat veglist fruitlist > grocerylist Pipes and Filters Pipes: Using output from one command as input to another command $ ls ¦ lp $ cat mydata ¦ lp Filters $ head –5 preface ¦ wc > wcpreface Processes & Time Sharing (1) Multiple users & processes share the same CPU Since only 1 program can be executed at any time, we need time sharing Process: an instance of an executing program (Unix creates a process every time you run a program) Processes & Time Sharing (2) • the Unix kernel is responsible for creating, running, suspending, and terminating processes; the CPU scheduler assigns time slices to processes • a system call is a request for kernel services, and is the only way a process can access the system’s hardware • as a user, you can request kernel services using shell commands (that, in turn, use system calls) Processes & Time Sharing (3) Priority of a process Running processes in the background $ acroread paper.pdf & Parents and children: all processes are children of the shell process Listing processes running $ ps Processes & Time Sharing (4) Killing a process with termination signal $ kill Killing a with unconditional kill signal $ kill –9 • You communicate with a Unix computer through a command program known as a shell. • • The shell interprets the commands that you type on the keyboard. • There are actually many different shells available for Unix computers, and on some systems you can choose the shell in which you to work. • You can use shell commands to write simple programs (scripts) to automate many tasks Popular Shells • (sh) – Korn shell (ksh) – Bourne-again shell () – Z shell (zsh) • (csh) – T-C shell () Bourne Shell (sh) • Good features for I/O control —often used for scripts • Not well suited for interactive users • Other shells based on Bourne may be suited for interactive users (e.g. bash) • Default prompt is $ C-Shell (csh) • Uses C-like syntax for scripting • I/O more awkward than Bourne shell • Nicer for interactive use • Job control • History • Default prompt is % What are Environment Variables?

• The environment is an array of strings of the form NAME=VALUE • Each process (program) has its own copy of the environment • Processes started by the shell get a (“deep”) copy of the shell’s current environment • Each process can examine and modify its own (and only its own) environment • The “meaning” of environment variables is merely a matter of established conventions Viewing your shell’s environment

To view a single environment variable’s value: $ For example: echo $HOME will display /homes/iws/evgenyr To view all environment variables at once: – tcsh/csh/bash: printenv – sh/ksh: set Setting environment variables tcsh/csh: – setenv – Example: setenv PRINTER ps329 bash/ksh: – export = ! – There is no space on either side of ‘=’! – Example: export PRINTER=pstud sh: – =; export Appending to environment variables

Appending /uns/bin/ to your PATH – bash/ksh: export PATH=$PATH:/uns/bin – tcsh/csh: setenv PATH=$PATH:/uns/bin Prepending is similar: – bash/ksh: export INFOPATH=/uns/info:$INFOPATH – tcsh/csh: setenv INFOPATH /uns/info:$INFOPATH Common environment variables

The “meaning” of environment variables is purely a matter of convention. However, these environment variables are quite common: – PATH, INFOPATH, MANPATH, EDITOR, VISUAL, PAGER, HOME, MAIL, USER The man page for a program will usually list the environment variables the program pays attention to. Shell Programming

Example of a shell scripts/program: $ cat > cmdfile if [ $LOGNAME = konev ] ; then echo “hello Boris” ; else echo hello ; fi $ sh cmdfile hello Boris