Unix and the Mac OS X Terminal - udel.edu

28
Unix and the Mac OS X Terminal An Introduction J. F.Weiher 2004 1 2

Transcript of Unix and the Mac OS X Terminal - udel.edu

Page 1: Unix and the Mac OS X Terminal - udel.edu

Unix and the Mac OS X Terminal

An Introduction

J. F.Weiher 2004

1

2

Page 2: Unix and the Mac OS X Terminal - udel.edu

Basic Concepts

GUI vs. UnixCommand LineDirectoriesPathsInput/Output

Basic Concepts GUI vs. Unix

Recognition — Recall Order from Menu — Cook yourself Limited — Need to Learn Learning curve Shell

3

4

Page 3: Unix and the Mac OS X Terminal - udel.edu

User Friendly?

Learning Curves

Difficulty

Time Time Time

GUI Aqua CLI

5

6

Page 4: Unix and the Mac OS X Terminal - udel.edu

Basic Concepts Command Line

Syntax Critical Case Sensitive Command Name Options = Switches or Flags Arguments = Objects of Command Example ls -al ~/Documents

Basic Concepts Directories

Root Subdirectories Users and Home

7

8

Page 5: Unix and the Mac OS X Terminal - udel.edu

Basic Concepts Paths

Full file designation = <path>/<filename>

Paths - Absolute /Users/you/Documents - Relative Documents

Mac OS X Typing Aids -drag file to terminal - tab completion

Basic ConceptsInput/Output

stdin and stdout Specification in command line overrides default Redirection > {file} >> {file} < {file} Pipes <cmd> | <cmd>

9

10

Page 6: Unix and the Mac OS X Terminal - udel.edu

Getting Help man <command> -[hk] whatis <command> apropos <word> <prog> --help <prog> - h Experiment Research the item

Some Commands pwd cd <dir> 0-1 ls <dir> -[alFtrRd] 0-n file <file> 1-n touch <file> 1-n rm <file> -[i rR] 1-n mkdir <dir> 1-n rmdir <dir> 1-n

11

12

Page 7: Unix and the Mac OS X Terminal - udel.edu

Example 1

Welcome to Darwin!• [localhost:~] weiher%who am I

weiher ttyp3 Oct 11 07:03 [localhost:~] weiher% whoamiweiher[localhost:~] weiher% pwd

/Users/weiher[localhost:~] weiher% ls -al /Usersdrwxr-xr-t 5 root wheel 126 Apr 27 2002 .drwxrwxr-t 65 root admin 2166 Oct 5 20:58 ..drwxrwxrwt 6 root wheel 264 Sep 28 16:43 Shareddrwxr-xr-x 16 km staff 500 Aug 22 2002 kmdrwxr-xr-x 28 weiher staff 908 Oct 11 04:01 weiher

Some Path Shortcuts Drag Path from GUI Tab completion Cut and paste within Terminal Window ~ = home directory . = current directory location .. = parent directory / = root directory

13

14

Page 8: Unix and the Mac OS X Terminal - udel.edu

Example 2[localhost:~] weiher% pwd/Users/weiher[localhost:~] weiher% cd ..[localhost:/Users] weiher% pwd/Users[localhost:/Users] weiher% cd ..[localhost:/] weiher% pwd/[localhost:/] weiher% cd ~[localhost:~] weiher%/Users/weiher[localhost:~] weiher%

pwd

More Commands man <command> mv <file…> <to> -[i f] 1-n, 1 cp <file…> <to> -[i] 1-n, 1 more <file> sp, b, q, ? 1 less <file> b f h /<text> 1

in search mode n = next; N = previous head <file> -[1 2 3…] 1 tail <file> -[1 2 3…] 1 wc < file> -l -w -c 1-n

15

16

Page 9: Unix and the Mac OS X Terminal - udel.edu

Some Terminal Tricks <cmd>| pbcopy = output to clipboard pbpaste | <cmd> = input from clipboard ^<str1>^<str2>^ = substitute str2 for str1 Arrow keys for navigation history [m] !n !<chrs>:p set history = <n> echo $history open <dir> = orders Finder to open <dir> open -a <application> = launch it open -e <file> = edit <file> with TextEdit

Concatenate cat {file} list file to default output cat default input and output cat > {file} default input to file to ^D cat >>{file} same but append to file cat {file} | pbcopy copy file to clipboard pbpaste |cat display clipboard cat {file1} {file2}… > {output file}

17

18

Page 10: Unix and the Mac OS X Terminal - udel.edu

More Terminal Tricks locate <path/file> or “*<str>” find <st-dir> [criteria] powerful but complex ‘<string>’ or “<string>” \ = quote next character backtick quotes `<expression>` ps -ax | grep <application> = returns PID

kill - STOP <PID> kill -CONT <PID> kill <PID>

sudo <cmd>

Special Characters * ? [ ] { } filename matching & expansion $ denotes variables ! ^ history reference & substitution & sends command to background | pipe > < I/O redirection

19

20

Page 11: Unix and the Mac OS X Terminal - udel.edu

Special Characters (cont) ; command separator (and) && command separator (if) || command separator (or) Space argument separator Tab file completion in tcsh ( ) subshell execution ` quote for command

substitution \ ‘ “ quote characters

Terminal a Rich ResourceThe directory /usr/share/misc[…:/usr/share/misc] weiher% grep AGC Airport AGC:Pittsburgh/Allegheny County [localhost:/usr/share/misc] weiher%

[:/usr/share/misc] weiher% grep 19810 zipcodes 19810:Wilmington, DE [localhost:/usr/share/misc] weiher%

[:/usr/share/misc] weiher% grep Bremen inter.phone 49:421:Bremen:Germany, Fed. Rep. Of [localhost:/usr/share/misc] weiher%

21

22

Page 12: Unix and the Mac OS X Terminal - udel.edu

Wildcards * in command line ? in command line [abc] in expressions or CL [^abc] in expressions or CL [a-dA-D] in expressions pr CL {chrs, chrs,chrs}in command line . in command line .. in command line

GREP Searches in 1-n files for a string grep [opts] <pattern> <file…> Options: -iclIrw More available but start simple grep -ic jim * grep -rl “Kontextkontrolle” /* grep -i "[tb]o[mb]" grepfile1 grep -i "[tb]o[mb]" grepfile?

23

24

Page 13: Unix and the Mac OS X Terminal - udel.edu

Some More Utilities ftp <remote cpu> telnet <remote cpu> finger <user@remote cpu> uptime cal bc

ftp Example

[localhost:~] weiher%

ftp udel.edu220 copland.udel.edu FTP server (Version wu-2.6.2(1) Mon Aug 11 12:44:29 EDT 2003) ready.Name (udel.edu:weiher): weiher

331 Password required for weiher.Password: ******

25

26

Page 14: Unix and the Mac OS X Terminal - udel.edu

ftp Example pt 2

230 User weiher logged in.

Remote system type is UNIX.Using binary mode to transfer files.ftp> cd public_html

250 CWD command successful

ftp Example pt 3

ftp> ls Bodmer*200 PORT command successful.150 Opening ASCII mode data connection for /bin/ls.-rw-r--r-- 1 weiher 0966 250927 Aug 30 17:09 BodmerWortschatzAlpha.pdf226 Transfer complete.

27

28

Page 15: Unix and the Mac OS X Terminal - udel.edu

ftp Example pt 4ftp> put(local-file) /Users/weiher/grepfile1 (remote-file) grepfile1local: /Users/weiher/grepfile1 remote: grepfile1200 PORT command successful.150 Opening BINARY mode data connection for grepfile1.226 Transfer complete.59 bytes sent in 0.261 seconds (226 bytes/s)

ftp Example pt 5ftp> ls grep*

200 PORT command successful.150 Opening ASCII mode data connection for /bin/ls.-rw-r--r-- 1 weiher 0966 59 Nov 2 18:30 grepfile1226 Transfer complete.

29

30

Page 16: Unix and the Mac OS X Terminal - udel.edu

ftp Example pt 6ftp> quit221-You have transferred 59 bytes in 1 files.221-Total traffic for this session was 2357 bytes in 4 transfers.221-Thank you for using the FTP service on copland.udel.edu.221 Goodbye.[localhost:~] weiher%

Some Hints Remember spaces count! Copy terminal output and paste in text

document for printing/saving/study Practice in a special account Backup regularly--just in case Remember touch and cat to quickly create

files for practice Be nice; logout of terminal session ^H = backspace; ^D = end of file

31

32

Page 17: Unix and the Mac OS X Terminal - udel.edu

Regular Expressions . matches any single character [xyz] any one character of x,y,z [^xyz] that is not x, y, or z [x-z] in the range x to z * preceding pattern 0 or more times ? preceding pattern 0 or 1 times + preceding pattern 1 or more times ^ the start of a line $ the end of a line \x or [x] removes any special meaning of x

finger Example[…:~] weiher% finger [email protected]

[udel.edu] X.500 Finger Service...3 exact matches for "flexman": Edmund.A..Flexman Edmund.Arthur.Flexman Student, Division of Continuing Education, Ruth.M.Flexman Student, Division of Continuing Education, NRQ, Undergraduate, non-degree Coordinator, CE-Prof & Noncredit Programs

33

34

Page 18: Unix and the Mac OS X Terminal - udel.edu

finger Example pt 2[…] weiher% finger [email protected][udel.edu] X.500 Finger Service...1 exact match found for "Ruth.M.Flexman":"Ruth M Flexman, CE-Prof and Noncredit Programs" Also Known As: Ruth M Flexman E-Mail Address: [email protected] Work Phone: +1 302-573-4416 Title: Coordinator, CE-Prof & Noncredit Programs …Uniqname: flexman

telnet Example[~] weiher% telnet udel.eduTrying 128.175.13.92...Connected to udel.edu.Escape character is '^]'.SunOS 5.9login: weiherPassword: ******Using project 0966, ALL-EMAILYou have $483.97 remaining in this account.

35

36

Page 19: Unix and the Mac OS X Terminal - udel.edu

telnet Example pt 2Last login: Sun Nov 2 21:08:32 from ppp-as37-25.nssSun Microsystems Inc. SunOS 5.9copland.udel.edu% quota -v

Disk quotas for weiher (uid 14158):Filesystem usage quota limit timeleft/home/usra 17292 24576 24576 copland.udel.edu% logout

Connection closed by foreign host.

Last Words Version Changes: Apple giveth & taketh

away [localhost:~] weiher% Bill Gates OK? kill Gates? /Library/Documentation/Commands

/grep/grep_toc.html Also for diffutils, dc, gzip

37

38

Page 20: Unix and the Mac OS X Terminal - udel.edu

39

40

Page 21: Unix and the Mac OS X Terminal - udel.edu

41

42

Page 22: Unix and the Mac OS X Terminal - udel.edu

43

44

Page 23: Unix and the Mac OS X Terminal - udel.edu

45

46

Page 24: Unix and the Mac OS X Terminal - udel.edu

47

48

Page 25: Unix and the Mac OS X Terminal - udel.edu

49

50

Page 26: Unix and the Mac OS X Terminal - udel.edu

51

52

Page 27: Unix and the Mac OS X Terminal - udel.edu

53

54

Page 28: Unix and the Mac OS X Terminal - udel.edu

55

56