Unix Notes for Tutorial Inclusion – From OS-X Unix Tutorial v4.doc From p 10 which ls reports where the ls command is found ls -lt long list sorted by time modified ls –h sometimes give help on a command like ls ls .. directory listing of next higher directory touch .hidden creates a hidden file name .hidden if it does not exist, modifies the date if it does exist ls ../.. directory listing of two levels up man tcsh >
Unix Notes for Tutorialv4.doc Page 1 JFW ls –R one check current nest ******* p24 mkdir –p a/b/c/d create a nest of dirs rmdir –p a/b/c/d if empty remove the nest of dirs ls –ld
Unix Notes for Tutorialv4.doc Page 2 JFW who can be owner (u), group (g), other (o) prot can be either a set of prots to be enabled or +
P38 Shells- The shell is your interface to the UNIX OS; it listens for input, launches other programs as found in the command line, displays output of those programs. Displays a prompt, keeps a command history, allows auto completion, includes framework to enable piping and a scripting language. The Terminal is an Aqua program that runs the shell for you. It adds the cut and paste, drag and drop complete path-filenames, etc for ease of use The Bourne shell (sh) is in every UNIX system, good scripting system, poor interactives csh was an improvement on sh but scriping is different tcsh was a further improvement incorporating many OS-features that DEC developed in their PDP-6, 10, Tops-10 etc systems. Good interactives but different scripting than sh (is default for OSX10.1 and 10.2 bash incorporated many improvements of previous systems, retaining excellent interactive behavior but with a scripting system upward compatible with sh (bourne again shell) is default for OSX10.3
Unix Notes for Tutorialv4.doc Page 3 JFW p40 head *.txt display first 10 or so lines the each file with extension .txt head –25 /var/log/system.log display first 25 lines of system log tail /var/log/system.log display last 10 or so lines of system log tail –f /var/log/system.log display last 10 or so lines of system log but leave it open and display any additions to the file (a command line equivalent of the aqua console program.) ctrl-c to end P41 locate
P42 find [options]
Unix Notes for Tutorialv4.doc Page 4 JFW find ~/Sites –mtime –7 find in your Sites dir all files modified less than 7 days ago find ~/Sites –mtime +7 find in your Sites dir all files modified moe than 7 days ago find ~ -newer last-backup.log find all files changed or created since the last-backup.log was modified find ~ –size +2000 find all files larger than 2000 blocks (i.e. 512 byte blocks) find ~ -size –2000 find all files smaller than 2000 blocks (i.e. 512 byte blocks) find . –empty find all empty files (includes empty directories that do have the . and .. in them so they are not 0 bytes long. find ~ -name “*.doc” –ls give a dir listing of all files matching the pattern inyour home dirs find ~ -name “*.txt” –exec cat {} \; do a cat listing on all files found find ~ -name “*.sh” –ok chmod a+x {} \; change the protection on all files found. find . -maxdepth 2searches nested dirs only 2 levels find . -maxdepth 2 -name "index.*" finds ./Sites/index.html (here ~ was current dir) find . -maxdepth 2 -name "ind" finds nothing (here ~ was current dir) find . -maxdepth 2 -name "ind*" finds ./Sites/index.html (here ~ was current dir) find . -maxdepth 2 -name "ind??.*" finds ./Sites/index.html (here ~ was current dir) find . -maxdepth 2 -name "ind??" finds nothing (here ~ was current dir) find . -maxdepth 2 -name ind finds nothing (here ~ was current dir) find . -maxdepth 2 -name index.html finds ./Sites/index.html (here ~ was current dir) Note on times in find: -amin and -atime are access time -cmin and -ctime are changed time (i.e. change in any way, permissions changed or content) -mmin and -mtime are contents modified time sh run the Bourne shell csh run the c-shell tcsh run the tcsh bash run the Bourne again shell zsh run the z-shell p46 grep [optons]
Unix Notes for Tutorialv4.doc Page 5 JFW [chrs] any character inside the [] will be a match for that postion [^chrs] any character but those inside the [] will be a match [chr1-chr2] any character between and included chr1 and chr2 will be a match . any charcter in a list in [] a literal ] must be first in a list in [] a literal ^ must not be first in a list in [] a literal - must be last the $ anchors to the end of line for a match the ^ anchors to the beginning of line \< matches the empty at the beginning of a word \> matches the empty at the end of a word \b matches the empty a the edge of a word \B matches the empty anywhere but at the end of a word ? is a repetition operator; preceding item must be repeated 0 or 1 time * is a repetition operator; preceding item must be matched 0 or more times + is a repetition operator; preceding item must be matched 1 or more times {n} is a repetition operator; preceding item must be matched exactly n times {n,} is a repetition operator; preceding item must be matched n or more times {n,m} is a repetition operator; preceding item must be matched at least n times but not more than m times two regular expressions can be concatenated; a match is any string which is a contatenation of the match for each regular expression two regular expressions can combined with the infix operator (|); a match is a match for either expression repetition take precedence over concatenation which takes preference over alternation any subexpression can be enclosed in ( ) to override the default precedence backreference \n matches the substring previously matched by the nth parenthesized subexpression; n must be a single digit. The metacharacters ? + { | ( and ) in basic regular expressions lose their special meaning; use the backslashed versions instead \? \+ \{ \| \( \) Examples: ‘Janice’ simple string will be matched only by that string ‘Ja.’ Will match Ja followed by any character, but not match the word Ja; it will match Jazz, Jabber, Jane etc ‘Ja..e’ will match Ja followed by any two characters followed by e; thus matches Jabber and Jayne but not Jane ‘J[aeiou]n’ will match the first three characters of Janice, Jenny, Jinny, Jon, or June but not Jean because one one the the alternative characters can be matched against. ‘F[0-9][a-z][A-Z]L’ will match F0oOL for example ‘Ja*’ will match J, Ja, Jaa, Jaaa or the first two characters of Janice. Note * does not match anything, it is a repetition operator. ‘He+lp’ will match Help, Heeeelp, but not Hlp ‘He?lp’ will match either Hlp or Help, no others ‘K[a-zA-Z][0-9]’ will match K followed by an upper or lowercase character followed by a digit ‘J.*e’ will match J followed by any number of any characters followed by an e; e.g. Je, Jayne or Jpokijuyklyt6o9e
Unix Notes for Tutorialv4.doc Page 6 JFW ‘J[aeiou]+n’ will match J followed by one or more vowels followed by n; e.g. Jen and Jean and Jeeeeeeaaaaaaaaaan ‘^Start’ matches Start but only a the beginning of a line ‘End$’ matche End but only at the end of a line ‘^A whole line$’ will match only a line that contains only ‘A whole line’ and nothing else ‘goodbye.$’`matches at the end of a line goodby. And goodbyZ because the . is interpreted as any character not as the literal . To treat a special character as a literal use the backslash or []; e.g. \. Or [.] for the literal .
‘x+’ is the same as ‘xx*’ ‘j*’ will match anything! Note: wildcards in shell globbing have a different meaning than in regular expressions. To see what the shell will do to a regular expression, before you give the return to the command, enter chrtl-x followed by *. The line will be rewritten as the shell expands it. To test regular expression at Terminal enter grep or egrep followed by the desired pattern (in single quotes of course). Terminal waits on the next line for input from you. Enter a string to see if it matches. If it matches it will be echoed back and Terminal will wait for another test. If it does not match the Terminal will just wait for another test. Use ctrl-D to end the cycle. P 47 cat invoke cat program, waits for input from keyboard and echoes each line inpu cat
Unix Notes for Tutorialv4.doc Page 7 JFW ps cx | grep –i finder uses ps (the process status program) to get all running processes, including those without a controlling terminal (-w) and report them using just the filename (not the full path, -c) Note that here the normal signal for a flag (-) is not needed but can be used. The i flag allows the lower case in the search pattern to match the Finder entry. Note also that for grep the explicit string does not need to be quoted. P 56 sudo cmd1 | cmd2 cmd1 required super user priv, cmd2 did not; output of cmd1 is piped by the shell to cmd2. cmd1 is run by root; cmds 2 is not. ps cux | grep –i –e finder –e pid ps outputs the lines with more info (-u); grep finds and outputs all lines with either finder (the process we are looking for) or pid (one of the heading items) in the line. Notice grep can have to more than one match string if each is preceded by the –e. cmd1 | cmd2 | cmd3 | cmd4 output of 1 is pipelined to 2, output of 2 to 3, output of 3 to 4 cat file1 | grep Hello if there is an error, e.g. the file does not exist, the error msg is sent to the terminal not grep. cat file1 |& grep Helloerror msgs are also sent to grep p 93 chmod u+t testdir sets the sticky bit for the directory; others can append but not delete from the directory chmod g+t testdir sets the sticky bit for the directory; others can append but not delete from the directory chmod o+t testdir has no effect chmod u-t testdir clears the sticky bit
Macworld Nov 2002 p90 open /Applications/calculator.app open the calculator applicaton open /Users/weiher/Pictures Finder open my Pictures folder
MacAddict July 2003 p59 sudo softwareupdate p94 /usr/share/emacs/21.1/lisp/play displays the list of text based games; type ‘emacs’ to launch this editor, then escape followed by ‘x’ to get to the menu bar. Finally type in the name of the script minus the .el or .etc extension and you are off to the races.
MacAddict Nov 2003 p 65 df –kl shows free disk space –l for local only, -k for in Kb
MacAddict Dec 2003 p 69 find . –name ‘*and*’ –type f finds all files of type file (f) whose names contain the string ‘and’ starting at the current dir and working down the directory tree
Unix Notes for Tutorialv4.doc Page 8 JFW Macworld April 2003 p 89 Ditto –rsrcFork ~/*.doc /private/tmp will copy all your wp documents to a temp directory including the resource forks. cd `cat gohere.txt` file gohere.txt contain a line with a directory; the backticks send the result the display of that file to cd. So the new working dir is set accordingly find ~ -name ‘*.doc’ | cpio –o |gzip > `date +~/%Y%m%d.cpio.gz` the find lists the name of all doc files in your home folder. This is passed to cpio (copy I/O) which is a simplified unix version of Stuffit. The output option (-o) tells t yor want to create an archive. That output is sent to gzip, which compresses it and write it to a file whose name is made up out of the date command: your home folder (~), the exact date (%Y%m%d), followed by the typical file extension attached to a file that has passed through cpio and gzip (cpio.gz). You can restore individual files by reversing the process as next shown. gzip -dc YYYYMMDD.cpio.gz | cpio -idr ‘*filename*’ the YYYYMMDD is the date you made the archive; filename is part of the filename you want to restore. For each match you will be asked if you want to restore it and where. To restore the entire archive omit the –r. To look at all files and decide if you want to restore each one omit the ‘*filename*’ an Example of this archiving and dearchiving : [WeiherG4:~] weiher% find ~ -name '*Unix\ Notes*' /Users/weiher/Documents/unix stuff/Unix Notes for Tutorial I.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv2.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv3.doc [WeiherG4:~] weiher% find ~ -name '*Unix\ Notes*' | cpio -o | gzip > `date +~/%Y%m%d.cpio.gz` [WeiherG4:~] weiher% find ~ -name '*cpio.gz*' /Users/weiher/20031128.cpio.gz [WeiherG4:~] weiher% find ~ -name '*Unix\ Notes*' /Users/weiher/Documents/unix stuff/Unix Notes for Tutorial I.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv2.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv3.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv4.doc [WeiherG4:~] weiher% gzip -dc 20031128.cpio.gz \ cpio -idr `*Tutorial*` *Tutorial*: No match. gzip: illegal option -- i usage: gzip [-cdfhlLnNrtvV19] [-S suffix] [file ...] [WeiherG4:~] weiher% gzip -dc 20031128.cpio.gz | cpio -idr `*Tutorial*` *Tutorial*: No match.
Unix Notes for Tutorialv4.doc Page 9 JFW ATTENTION: cpio interactive file rename operation. -rw-r--r-- Nov 23 07:07 /Users/weiher/Documents/unix stuff/Unix Notes for Tutorial I.doc Input new name, or a "." to keep the old name, or a "return" to skip this file. Input > Skipping file.
ATTENTION: cpio interactive file rename operation. -rw-r--r-- Nov 27 16:12 /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv2.doc Input new name, or a "." to keep the old name, or a "return" to skip this file. Input > /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv2a.doc Processing continues, name changed to: /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv2a.doc
ATTENTION: cpio interactive file rename operation. -rw-r--r-- Nov 28 11:51 /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv3.doc Input new name, or a "." to keep the old name, or a "return" to skip this file. Input > Skipping file.
ATTENTION: cpio interactive file rename operation. -rw-r--r-- Nov 28 15:09 /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv4.doc Input new name, or a "." to keep the old name, or a "return" to skip this file. Input > Skipping file. [WeiherG4:~] weiher% find ~ -name '*Unix\ Notes*' /Users/weiher/Documents/unix stuff/Unix Notes for Tutorial I.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv2.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv2a.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv3.doc /Users/weiher/Documents/unix stuff/Unix Notes for Tutorialv4.doc [WeiherG4:~] weiher%
Misc Notes & Examples from Unix for OS-X and elsewhere
^stri1^str2 at new command line edits previous command & enters it ps -auxc get process status for all processes (a), extended info (u), without path (c), include those without controling terminal (x) ps -ax | grep -e "Microsoft" -e "PID" get ps lines for Microsoft with header kill n closes process with PID n kill -STOP n pauses process with PID n kill -CONT n continues process with PID n open
Unix Notes for Tutorialv4.doc Page 10 JFW open *.doc opens all *.doc with the default application (Microsoft Word) cat
Filenames with spaces need to be quoted. Apple missed a couple of quotes in iTunes once which opened up the possibility of a users getting all of his disk erased. ls * ls *.txt ls ? ls *.? ls [A-Z]*.txt ls -s report size in bytes ls -k report size in k ls -A almost all, . and .. files are excluded ls -R recursive ls -r reverse sort order find . -type d -and -name '*Doc*' find in current dir down directories (d) and (-and) with name containing Doc find . -type d -and -name 'Documents' find directories with exact name Documents sort -o
Unix Notes for Tutorialv4.doc Page 11 JFW shutdown now shutdown computer now shutdown n shutdown in n minutes shutdown < yymmddhhmm> shutdown at yymmddhhmm, several options available reboot flushes the disk cache and reboots, several options available; also a halt command date >> dates.dat ps -u
*****Sample terminal session showing use of background jobs (bold is user input) [localhost:~] weiher% top& [1] 1163 [localhost:~] weiher% fg %1 top
Processes: 38 total, 3 running, 35 sleeping... 127 threads 22:05:00 Load Avg: 0.42, 0.31, 0.17 CPU usage: 13.0% user, 12.2% sys, 74.8% idle SharedLibs: num = 102, resident = 23.6M code, 1.60M data, 6.39M LinkEdit MemRegions: num = 4522, resident = 141M + 8.04M private, 113M shared PhysMem: 74.1M wired, 76.8M active, 256M inactive, 407M used, 233M free VM: 2.36G + 45.9M 11707(0) pageins, 0(0) pageouts
PID COMMAND %CPU TIME #TH #PRTS #MREGS RPRVT RSHRD RSIZE VSIZE 1163 top 7.7% 0:02.24 1 19 15 248K 320K 504K 1.65M 1025 Microsoft 0.0% 0:02.97 2 74 93 1.83M 10.9M 3.11M 57.6M 989 Sherlock 0.0% 0:14.39 3 88 119 2.98M 9.15M 6.96M 57.6M …
Unix Notes for Tutorialv4.doc Page 12 JFW 2 mach_init 0.0% 0:05.85 1 123 12 56K 312K 144K 1.27M 1 init 0.0% 0:00.02 1 20 12 48K 300K 248K 1.26M ^Z [1] + 1163 Suspended top [localhost:~] weiher% bg %1 [1] top & [localhost:~] weiher% jobs [1] Running top [localhost:~] weiher% kill %1 [localhost:~] weiher% jobs [1] Terminated top [localhost:~] weiher% jobs [localhost:~] weiher% ***** bin directories contain the binary commands $path contains the search path /Users/weiher/bin/powerpc-apple-darwin ?? /Users/weiher/bin your personal commands /usr/local/bin your commands as admin /usr/bin other command delivered with OS-X /bin the minimum set of essentail commands /usr/local/sbin your system commands as admin /usr/sbin other system commands delivered with OS-X /sbin minimum set of essential system commands
Some History: 1969 Bell Labs Ken Thompson's Multics 4 yrs & dropped 1973 Dennis Ritchie Unix & Thompson? (pun on Multics) first portable OS based on C 1977 Berkley Software Distribution version 1 DARPA Defence Advanced Research Program Agency 1983 ARPAnet -> TCP/IP 1983 BSD 4.2 Bill Joy 1983 AT&T System V 1983± HP HP/UX 1985 Jobs Next 1991 Linus Torvalds version 0.02 of Linux 1992 Boll Jolitz 386/BSD no original AT&T code 1992 Sun Microsystems Solaris 1994 v1 Linux 1994 BSD -4.4 Lite 1999 Newton released
Grep example: [localhost:~] weiher% grep -ilr "gute Miene zum b.sen Spiel machen" /Users/weiher/0104121623
Unix Notes for Tutorialv4.doc Page 13 JFW [localhost:~] weiher% another grep example: [localhost:~] weiher% grep -E 'a?b' xyb xyb ab ab aaaa b b aaaaab aaaaab caaaab caaaab [localhost:~] weiher% grep -E 'ca?b' xyb xycb xycb xycabkk xycabkk ytcaabkk [localhost:~] weiher%
[localhost:~] weiher% egrep 'ca\?t' cat ct caaat c?t ca?t ca?t [localhost:~] weiher% grep 'ca\?t' cat cat ct ct caaat
[localhost:~] weiher% grep 'a?b' b ab a?b a?b a\?b [localhost:~] weiher% grep
Unix Notes for Tutorialv4.doc Page 14 JFW