Introduction to “If you have trouble sounding condescending, find a Unix user to show you how it's done.” – Scott Adams Cornell University CS 316 – Fall 2006

Slides by Michael Siegenthaler

Why Bother? UNIX Philosophy

• Most programmers who learn UNIX end • Multiuser / multitasking up finding it useful • Toolbox approach easier • Many simple tasks are to – Combine multiple simple commands instead accomplish with a powerful command line of using a single complex application interface – Possible to script repetitive operations • Designed by programmers for programmers • UNIX is widely used in research and industry, and runs most of the servers on the Internet

UNIX Philosophy Command Line Environment

• Does what you tell it to • Shell is the command line interpreter – no confirmation is requested – Just another program – no acknowledgement is given – Bourne shell (bash) – many commands only produce output on – C Shell (csh) failure • Commands may be any of – Built-in shell command – Script (e.g. perl, python, sh) – Other program

1 Running Commands Plumbing

• Commands follow the form: • I/O Redirection – command – Options modify the command > Redirect standard output to file – Arguments indicate what file to operate on >>Append standard output to file • Get help by typing man command < Get input from file • Example: [msiegen@tiger ~]$ ls -l /usr • Pipes (|) are used to take the output of one total 301 drwxr-xr-x 2 root root 69632 Oct 18 08:43 bin/ program and use it as input to another drwxr-xr-x 2 root root 4096 Aug 12 2004 etc/ e.g. du -sk /home/* | sort -nr | head -10 drwxr-xr-x 2 root root 4096 Aug 12 2004 games/ drwxr-xr-x 117 root root 20480 Sep 12 20:40 include/ > disk_hogs.txt ...

Plumbing Practical Tips

• Running multiple commands in sequence • Use less to view output that will not fit on your – Use semicolon (;) to run commands screen unconditionally e.g. ls -lR | less – Use double ampersand (&&) to run • Use to output, and wc to count lines commands only until the first error occurs e.g. ps aux | grep “vim” | wc -l • Use parentheses to group a sequence and • Use && to run multiple commands in sequence redirect output e.g. ./configure && make && make install e.g. (date && ls) > logfile • Many more possibilities! Not the same as: date && ls > logfile

File System File System

• Case sensitive! / • Moving around, working with directories cd Change working directory bin etc lib usr dev tmp home pwd Print working directory ls -la List all files in working directory sh ls libc.so ttya null mkdir Make directory rmdir Remove directory passwd group bin man local kwalsh egs msiegen cp Copy file mv Move or rename file cs316 mailbin stuff • Searching e.g. find -name Makefile

2 Wildcards File System Permissions

• Shorthand for referencing multiple existing • Permissions can be specified for – Owner files on the command line – Group –* any number of characters – All • Permissions are –? exactly one character – Read – [abc] any one of a, b, or c – Write – Execute – [!abc] any character except a, b, or c • Example: -rwxr-xr-x 1 msiegen ta 10152 Sep 21 17:04 disassemble • Examples -rw-r----- 1 msiegen ta 329 Sep 21 17:04 main.c The disassembler may be executed by anyone on the system, but ls -l *.c the source file may only be read by people in the ta group. Both lpr [Mm]akefile files may only be edited by the user msiegen .

File System Permissions Job Control

• For a directory, “read” means being able to list • Use & after a command to place job in its contents, “execute” means being able to background access files within the directory – Unless the files have more restrictive permissions • Manage jobs: • Use chmod to add or remove permissions (rwx) – jobs List jobs for user, group, and others (ugo): – fg %1 Bring job 1 to foreground chmod ugo+x Let anyone execute – bg %2 Run job 2 in background chmod go-w Prevent non-owner form writing – kill %3 Terminate job 3 • Or, specify absolute permissions in octal – 4=r, 2=w, 1=x – ^Z (control+Z) suspend foreground job – e.g. 755=rwxr-xr-x, 640=rw-r----- – ^C (control+C) terminate foreground job e.g. chmod 755 filename

Job Control Environment Variables

• Example • Display all variables by typing env [msiegen@tiger ~]$ sleep 800 & [1] 16139 • Set a variable, example: [msiegen@tiger ~]$ sleep 400 & NETID=abc123; export NETID (bourne shell) [2] 16141 [msiegen@tiger ~]$ jobs setenv NETID abc123 (c-shell) [1]- Running sleep 800 & • Use a variable in a command, example: [2]+ Running sleep 400 & [msiegen@tiger ~]$ kill %1 echo $NETID [msiegen@tiger ~]$ jobs • Environment variables are used to control various [1]- Terminated sleep 800 [2]+ Running sleep 400 & behaviours of the shell, as well as pass global [msiegen@tiger ~]$ fg %2 information to other programs that are started from within sleep 400 the shell ^Z [2]+ Stopped sleep 400 • The variable $PATH is used to locate programs that are [msiegen@tiger ~]$ bg %2 run [2]+ sleep 400 &

3 Beyond a Single User Useful Commands ps aux List all running processes • file Determine the type of a file who ; w Show who else is logged in • sort Sort lines in a text stream top Show CPU, memory usage • uniq Eliminate duplicate lines (useful for finding out why a system is • wc Count bytes, words, or lines soooo slow, and who to blame) • cal Display a calendar • grep Filter a text stream • sed Search and replace on text stream • awk (Slightly) more advanced scripting

Advanced Topics Further Reading

• Shell scripting • Manual (man) pages – Anything which can be done from the • O’Reilly Books command line, can be scripted – Free access on campus: • Regular expressions http://proquest.safaribooksonline.com/ – Fancier version of wildcards – Or from home through the Safari Tech Books – Allows complex matching and search and link at: replace operations on text http://www.englib.cornell.edu/erg/shortlist.php – Suppored by grep, awk, and many scripting/programming languages

4