Introduction to UNIX Unix File System Stores Files • Text, Programs, Images, etc. Made up of Many Files • User, Unix System, Unix Commands 2 Unix File System Top most directory in Unix root usr Home Directory var home etc rdefe jsmith djones data mbox, letter, prog1, prog2 jan feb file1, file2, file3, file4 file1, file2, file3, file4 save lib mbox, jandat info Directory containing all home directories x1, x2, x3, x4 3 Unix File System Home Directory • Personal Directory to store files • Usually the Directory Name = login ID • Current Directory after successful login root usr Home directories for: rdefe, jsmith and djones var home etc rdefe jsmith djones data mbox, letter, prog1, prog2 jan feb file1, file2, file3, file4 file1, file2, file3, file4 save lib mbox, jandat info x1, x2, x3, x4 4 Unix File System What Can You Do? • • • • Create New Files & Directories Organize Files Locate Files Based on a Directory Structure Change to a New Directory 5 Unix File System File & Directory Naming Guidelines • Don’t Use Meta Characters /\“`*;?{}()[]~!$<>|&# • Do Use a-z A-Z 0-9 ._- • Remember Unix is Case Sensitive! 6 Unix File System File & Directory Name Examples • • • • feb1998, 178987, my_data, my.data.new mbox, Mbox, mboX, MBOX, mBox prog1..2 .profile 7 Unix Commands ls • List Files & Directories $ ls 198 A aa.bb.cc.dd bin datefile etc mail mbox mbox.old unix $ 8 Unix Commands ls -p • -p Show Directories $ ls -p 198 A aa.bb.cc.dd bin/ datefile etc/ mail/ mbox mbox.old unix/ $ 9 Unix Commands ls [directory1] [directory2] ... • List the contents of a directory $ ls -p 198 A aa.bb.cc.dd bin/ datefile etc/ mail/ mbox mbox.old unix/ $ $ ls unix awk.data awk.prg bgraph cpy homework2 homework3 unix2test $ 10 Unix Commands ls -s • -s Show File Size in Blocks • Block Sizes $ ls -s 512 bytes 1024 bytes 2048 bytes total 19 1 198 1A 1 aa.bb.cc.dd 1 bin 1 datefile 1 etc 1 mail 2 mbox 1 unix 11 Unix Commands ls -l • -l Long Listing $ ls -l total 19 -rw-r--r--rw-r--r-drwxr-xr-x drwxr-xr-x drwxr-xr-x drwxr-xr-x -rw------drwxr-xr-x $ 1 1 2 3 2 2 1 2 rdefe rdefe rdefe rdefe rdefe rdefe rdefe rdefe unix unix unix unix unix unix unix unix 53 62 96 80 64 32 984 144 Sep Sep Sep Aug Aug Sep Sep Aug 12 12 4 29 29 2 12 29 21:46 21:47 20:58 20:54 20:54 1992 21:44 20:54 198 A bin db etc mail mbox unix 12 Unix Commands ls -a • -a List All Files Include Files That Begin with a ‘.’ Files that begin with a ‘.’ are usually configuration files $ ls -a . .. .profile .sh_history 198 A bin datefile db etc mbox unix $ 13 Unix Commands ls -r • -r Reverse Sort ls -R • -R List Contents of Subdirectories • List files in each subdirectory. • Subdirectories with Subdirectories will also be listed…and so on... $ ls -r unix mbox mail etc db datefile bin A 198 $ 14 Unix Commands ls -l -t or ls -lt • -t Sort On Modification Time $ ls -lt total 19 -rw-r--r--rw-r--r--rw------drwxr-xr-x drwxr-xr-x drwxr-xr-x drwxr-xr-x drwxr-xr-x $ 1 1 1 2 2 2 3 2 rdefe rdefe rdefe rdefe rdefe rdefe rdefe rdefe unix unix unix unix unix unix unix unix 62 53 984 96 64 144 80 32 Sep Sep Sep Sep Aug Aug Aug Sep 12 12 12 4 29 29 29 2 21:47 21:46 21:44 20:58 20:54 20:54 20:54 1992 A 198 mbox bin etc unix db mail 15 Unix Commands ls -l -t -r or ls -ltr • -tr Reverse Sort On Modification Time $ ls -ltr total 19 drwxr-xr-x drwxr-xr-x drwxr-xr-x drwxr-xr-x drwxr-xr-x -rw-------rw-r--r--rw-r--r-$ 2 3 2 2 2 1 1 1 rdefe rdefe rdefe rdefe rdefe rdefe rdefe rdefe unix unix unix unix unix unix unix unix 32 80 144 64 96 984 53 62 Sep Aug Aug Aug Sep Sep Sep Sep 2 29 29 29 4 12 12 12 1992 20:54 20:54 20:54 20:58 21:44 21:46 21:47 mail db unix etc bin mbox 198 A 16 Unix Commands ls -[ltrRa] [Filename1] [Filename2] … • UnixCommand Options Arguments $ ls -lt unix $ 17 Unix Commands cat [Filename1] [Filename2] … • • • • Concatenate Files Display Files to the Screen Ctrl-s Pause Screen Ctrl-q Unpause Screen $ cat sample.file This is a sample file that i'll use to demo how the pr command is used. The pr command is useful in formatting various types of text files. $ 18 Unix Commands more [Filename1] [Filename2] … • Display Files to the screen one page at a time $ more largefile This is a sample file that i'll use to demo how the pr command is used. The pr command is useful in formatting various types of text files. --More--(16%) Spacebar (next page) b (previous page) Enter Key (next line) q (Quit & return to unix) /string (search for string) :n (next file) :p (previous file) 19 Unix Commands pr [Filename1] [Filename2] … • Format & Display Files to the Screen $ pr sample.file 98-05-22 23:07 sample.file Page 1 This is a sample file that i'll use to demo how the pr command is used. The pr command is useful in formatting various types of text files. $ 20 Unix Commands pr -[dn] [Filename1] [Filename2] … • -d Double Space Output • -n Number Lines $ pr -d sample.file 98-05-22 23:07 sample.file Page 1 $ pr -n sample.file This is a sample file that i'll use to demo how 98-05-22 23:07 the pr command is used. sample.file Page 1 1 This is a sample file that i'll use to demo how the The pr command is useful in formatting 2 pr command is used. 3 The pr command is useful in formatting various types of text files. 4 various types of text files. $ $ 21 Unix Commands lpr [Filename1] [Filename2] … • Send Files to a Unix Printer • No Formatting (page numbers, etc.) $ lpr prog1 mbox $ $ pr sample.file Ideal solution would be to combine the lpr & pr to achieve formatted printed output 98-05-22 23:07 sample.file Page 1 This is a sample file that i'll use to demo how the pr command is used. ... 22 Unix Redirection Output Normally Displayed to the Screen is Redirected and Becomes the Input to Another Command $ pr prog1 | lpr $ 23 Unix Redirection The Output of One Command can be used as the Input to Another $ pr prog1 | lpr $ Command that generates screen output Command | Command Command that requires input 24 Unix Redirection Command | Command | Command ... $ ls | pr | lpr $ Directory Listing Formatted Using pr, Printed on Paper 25 Unix Redirection Output Normally Displayed to the Screen Can Be Redirected to a File $ pr prog1 > prog1.pr $ Note: Nothing is displayed to the screen The file prog1.pr contains the output of the pr command 26 Unix Redirection Redirecting Output • The Output of Command can be saved to a File $ pr prog1 > prog1.pr $ Command that generates screen output Command > File File used to capture (save) screen output Caution: Output File will be erased if it already exists. 27 Unix Redirection Redirecting Input • A Command Requiring Input Can Receive it’s Input From a File $ mail rdefe < mesg $ 28 Unix Redirection Redirecting Input $ mail rdefe < mesg $ Command that requires input Command < File File used to provide input (instead of typing it!) 29 Redirection Examples 1 $ pr data > data.pr $ lpr data.pr 2 $ cal 3 1993 > year Using arguments with redirection 3 $ ls -l data > stuff Using arguments & options with redirection Is the same as $ pr data | lpr 30 Redirection Examples 4 $ pr data > lpr 5 $ cat text let > mesg 6 $ pr f1 f2 > f1 1 2 Create the file f1 Erases f1 if it already exists pr f1 and f2, redirect to f1 31 Unix Commands rm [Filename1] [Filename2] … • remove files * is a Meta Char that matches anything $ rm prog1 mbox $ $ rm p* $ Caution $ rm * $ Unix Assumes You Know What Your Doing Be Sure You Know What Your Deleting No Undelete! 32 Unix Commands vi [Filename] • • • • Full Screen, Visual Editor Changes Displayed As They Are Made Edits A Copy of the File Changes Must Be Saved $ vi mesg emacs http://www.gnu.org/software/emacs/emacs.html 33 Unix Commands Vi Modes Command Mode • Start in command mode • Every Key Performs an Editing Command Insert Mode • Perform Text Entry/Input Insert Mode Command Command Mode Insert Mode ESC Key 34 Sample vi Edit Session $ vi mesg Welcome to UNIX Wnewtextelcome to UNIX This is an example of a text file in vi This is an example of ~ a text file in vi ~ ~ ~ ~ :wq $ Start in Command Mode Press a key for Append After Cursor Command Enter text (Input Mode) Press ESC key (Command Mode) Press :wq followed by Enter key to Save & Exit 35 Cursor Positioning Moving the Cursor in Command Mode k Key h l j w b Cntrl-d Cntrl-u Enter G 5G Command Forward One Word Backward One Word Scroll Down Scroll Up Down One Line Go To Last Line Go To Line 5 36 Text Input Mode vi Text Input Mode Commands Wnewtextelcome to UNIX a Append After Cursor Welcome to UNIXnewtext This is an example of A Append at End of Line a text file in vi is an example ofto UNIX ~ ThisnewtextWelcome i Insert Before Cursor ~ a text file in vi is an example of to UNIX ~ ThisnewtextWelcome I Insert at Beginning of ~ a text file in vi Line ~ This is an example of newtext ~ a text file in vi O Open New Line Above Cursor Welcome to UNIX ~ o Open New Line Below Cursor ~ This is an example of R Replace Text (typeover) a text file in vi ~ r Replace One Char ~ 37 Deleting Text Welcome to UNIX This is an example of a text file in vi ~ This is an example of ~ a text file in vi ~ ~ x Delete One Char X Delete Char Before Cursor dd Delete One Line Proceed Any Command with a Number to multiply effect • 4x Delete 4 chars • 5dd Delete 5 lines • 3dw Delete 3 words 38 Undo Changes Welcome to UIX Welcome to UNIXof This is an example a text file in vi ~ This is an example of WELcome ~ a text file in vi to UIX 123 ~ an example of ~ This is Welcome to UNIX a text file in vi ~ This is an example of ~ a text file in vi ~ ~ x Delete One Char u Undo Last Change U Undo All Changes on Line 39 Saving Your Changes Command Mode • • • • ZZ :wq :w data :q! Save & Exit vi Save & Exit vi Save to a file data Quit Without Saving Changes Vi Reference • http://www.eng.hawaii.edu/Tutor/vi.html 40 Other Goodies... /string Forward Search for `string` ?string Backward Search for `string` • n • N Ctrl-G J Repeat Search Repeat Search in reverse direction Display Current Location in File Join two lines together http://www.thomer.com/thomer/vi/vi.html 41