comp 104: intro to unix

95
COMP 104: Intro to Unix Week 2

Upload: shino

Post on 02-Feb-2016

56 views

Category:

Documents


0 download

DESCRIPTION

COMP 104: Intro to Unix. Week 2. Review of Last Week. History of Unix Unix design philosophy The Unix shell, variables and options Unix commands: alias, cat, date, echo, exit, finger, hostname, login, lp, ls, man, more, passwd, set, setenv, uname, wc, whatis, whereis, who, whoami. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COMP 104: Intro to Unix

COMP 104: Intro to Unix

Week 2

Page 2: COMP 104: Intro to Unix

Review of Last Week History of Unix Unix design philosophy The Unix shell, variables and options Unix commands: alias, cat, date, echo, exit, finger,

hostname, login, lp, ls, man, more, passwd, set, setenv, uname, wc, whatis, whereis, who, whoami

Page 3: COMP 104: Intro to Unix

Agenda – Activity 1 Introduction to the Unix File

System Unix file system File Types Directory File Paths Access Permissions Demonstration of file system

Page 4: COMP 104: Intro to Unix

Agenda – Activity 2

UNIX Commands Navigating the File System:

pwd, ls, touch, cd Demonstration of pwd, ls, touch, and cd

Working With Files: cp, mv, rm, mkdir, rmdir Demonstration of cp, mv, rm, mkdir, rmdir

Page 5: COMP 104: Intro to Unix

Agenda – Activity 2 Continued

More UNIX Commands File Permissions:

id, umask, chmod Demonstration of id, umask, chmod

In class assignment Break (10 minutes)

Page 6: COMP 104: Intro to Unix

Agenda – Activity 3

vi Editor Introduction to the vi editor Practice with the editor

Preview of next week

Page 7: COMP 104: Intro to Unix

Activity 1

Page 8: COMP 104: Intro to Unix

The Unix File System

Everything is a File in Unix

Page 9: COMP 104: Intro to Unix

Types of Unix Files

There are Three Types of Files: Ordinary / Regular Files Directories Special Files – Internal representation

of a physical device (keyboard, printer, terminal)

Page 10: COMP 104: Intro to Unix

The Tree-Structured File System

root (/)

bin dev etc lib lost+found sys tmp usr

bin games lib local spool

Page 11: COMP 104: Intro to Unix

Another ExampleThe following directory tree, and files are located under

/export/home/smith/comp110  |_[assignment1] | \_assign1-1.doc | |_[assignment2] | \_assign2-1.doc | |_[lab1] \_[doc] | \_bubblesort.man | |_[report] | \_lab1.report | |_[source] \_sort.cpp sort.o

Page 12: COMP 104: Intro to Unix

Common Unix Directories

/bin stores basic Unix programs/dev contains files that represent

devices/etc files for managing the system/lib contains libraries of programs /lost+found contains ‘misplaced’ files/sys contains system source files/tmp temporary storage/usr important directory – contains

many things

Page 13: COMP 104: Intro to Unix

Unix Directories

Root Directory /

Your Home Directory /export/home/{userid}

$HOME variable Shows your current home directory print $HOME - display variable setting

Page 14: COMP 104: Intro to Unix

Unix Directories

Present Working Directory

Your current location

-or-

Current Directory

Page 15: COMP 104: Intro to Unix

Unix Commands: pwd

Use pwd to display the name of your current working directory

/export/home/morris07/> pwd

Page 16: COMP 104: Intro to Unix

Present Working Directory

Page 17: COMP 104: Intro to Unix

Absolute Path

Absolute Path

/export/home/morris07/labs

NOTE: These always start with a “/” from root.

Page 18: COMP 104: Intro to Unix

Unix Commands: cd

Use cd to change your working directory

/export/home/morris07/> cd {directory name}

Page 19: COMP 104: Intro to Unix

Absolute Path

Page 20: COMP 104: Intro to Unix

Absolute Path

Page 21: COMP 104: Intro to Unix

Relative Path

If your pwd was

/export/home/morris07/

You could do:cd examples To move into the examples

subdirectory

Page 22: COMP 104: Intro to Unix

Relative Path

Page 23: COMP 104: Intro to Unix

Relative Path (Shorthand)

Single dot . Your current directoryDouble dot .. Your parent directory

cd . Takes you to where already are!

cd .. Takes you to the pwd’s parent directory.

cd ~ Takes you to your home directory

cd - Takes you to the previous directory

Page 24: COMP 104: Intro to Unix

Relative Path

Page 25: COMP 104: Intro to Unix

Relative Path

Page 26: COMP 104: Intro to Unix

Relative Path

Page 27: COMP 104: Intro to Unix

Unix Commands: ls

Use ls to list the contents of a directory

/export/home/morris07/> ls

/export/home/morris07/> ls –l * (long format)

/export/home/morris07/> ls –la * (long format, and list all entries including those

that begin with a “.”

Page 28: COMP 104: Intro to Unix

Unix Commands: ls

/export/home/morris07/> ls –F * Flags directories with a “/” and executables with a

“*”

Using Wildcards:* Any string of characters? Any one character (not space)

[ ] Match any character in the brackets

Page 29: COMP 104: Intro to Unix

Unix Commands: ls

Examples

ls *.c Lists all files ending with ‘.c’

ls file? Lists any file with file and one

character at the end

ls v[6,7]file Lists v6file and v7file

Page 30: COMP 104: Intro to Unix

Unix Commands: ls

Page 31: COMP 104: Intro to Unix

Unix Commands: ls

Page 32: COMP 104: Intro to Unix

Unix Commands: ls

Page 33: COMP 104: Intro to Unix

Unix Commands: ls

Page 34: COMP 104: Intro to Unix

Using Relative Path in ls ls -al ..

Lists your parent directory

ls –al ~ Lists your home directory

Page 35: COMP 104: Intro to Unix

Question/Answer Session

Page 36: COMP 104: Intro to Unix

Activity 2

Page 37: COMP 104: Intro to Unix

Unix Commands: touch

Use touch to change a file’s access time and modification time to the current date

/export/home/morris07/> touch {file name}

NOTE: If the file does not exist, touch will create a new file

Page 38: COMP 104: Intro to Unix

Unix Commands: id

Use id to display your userid and groupid

/export/home/morris07/> id

Page 39: COMP 104: Intro to Unix

Unix Commands: id

Page 40: COMP 104: Intro to Unix

Unix Security

Login name and a password Encryption on important files Access permission

Page 41: COMP 104: Intro to Unix

Encryption of files Text page 334 crypt

Description will make more sense after next week

Requires a key – do not forget the key

Page 42: COMP 104: Intro to Unix

Access Permissions

Ordinary File Read: you can read from the file

Write: you can write to the file Execute: you can execute the file key

Page 43: COMP 104: Intro to Unix

Directory Permissions

Directory Read

You can read the directory Write

You can create, move, copy or remove directory contents

Execute: You can search the directory

Page 44: COMP 104: Intro to Unix

How Permissions are Managed

There are three Permission Groups:

Owner: Owner’s Group: Everyone Else/Other:

Page 45: COMP 104: Intro to Unix

Permissions-rwxrwxrwx 1 morris07 student 512 Jan 12 14:07 file.exe-rw-rw- rw- 1 morris07 student 812 Jan 12 14:22 file.namedrw-rw-rw- 1 morris07 student 812 Jan 12 14:22 labs-----------------------------------------------------------------------------------------

r read permissionw write permissionx execute permission- permission not granted

-----------------------------------------------------------------------------------------owner group everybody At the far left, 1’st character rwx rwx rwx shows type of file. “-” ordinary

“d” is directory

Page 46: COMP 104: Intro to Unix

Unix Commands: chmod

Use chmod to change the file-access permissions on an existing file

> chmod {mode} {file}

> chmod 777 file.name

Page 47: COMP 104: Intro to Unix

Numeric Value of Permissions

FILE MODE, or MODE

read permission = 4 write permission = 2execute permission = 1 no permission = 0

To calculate the proper permissions you want to assign, simply add the numbers together:read + write + execute = 4 + 2 + 1 = 7

read + write = 4 + 2 = 6 …

Page 48: COMP 104: Intro to Unix

chmod: Calculating the ModeNumber Meaning 400 Owner has read permission 200 Owner has write permission 100 Owner has execute permission 040 Group has read permission 020 Group has write permission 010 Group has execute permission 004 Everyone else has read permission 002 Everyone else has write permission 001 Everyone else has execute permission-------- 777

Page 49: COMP 104: Intro to Unix

Numeric Value of Permissions

chmod 777 lab1 Allows rwx to everyone!

chmod 755 lab1 Allows rwx to user, and rx to group/others.

Or to deny group and others rwx permissions:

chmod 700 lab1

Page 50: COMP 104: Intro to Unix

Symbolic-Mode File Permissions

Letters represent the groups and permissions:u = User, g = Group, o = Others+ or – To add or remove a permission from:r = Read, w = Write, x = Execute

chmod ugo+rwx lab1 Allows rwx to everyone!

Or to deny group and others rwx permissions:chmod go-rwx lab1

Page 51: COMP 104: Intro to Unix

Unix Commands: chmod

Page 52: COMP 104: Intro to Unix

Unix Commands: chmod

Page 53: COMP 104: Intro to Unix

Unix Commands: chmod

Page 54: COMP 104: Intro to Unix

Unix Commands: umask

Use umask to display or set the current value of the file-creation mask (default permissions, set in .profile)

/export/home/morris07/> umask

/export/home/morris07/> umask 022

Page 55: COMP 104: Intro to Unix

Unix Commands: umask

Page 56: COMP 104: Intro to Unix

umask: Calculating the umask

File Type Beginning File ModeNon-executable files 666Executable files 777Directories 777

From this initial mode, Unix subtracts the value of the umask.

For example, if I want a file permission of 644 on a regular file, the umask would need to be 022.

Page 57: COMP 104: Intro to Unix

Unix Commands: umask

Page 58: COMP 104: Intro to Unix

Unix Commands: umask

Page 59: COMP 104: Intro to Unix

Unix Commands: cp

Use cp to copy the contents of one file to another file

cp {source file} {destination file}

> cp file1 file2 Copies the file to another file name

cp file1 ~/newdir/junk1 Copies the file1 to your home directory in the

directory newdir and renames the file to junk1 (newdir must already exist)

Page 60: COMP 104: Intro to Unix

Unix Commands: mv

Use mv to move files to another directory or to a new name in the current directory

> mv {source file} {destination file}

> mv file1 file2 * Moves the file to another file name

> mv file1 newdir* Moves the file to another directory

Page 61: COMP 104: Intro to Unix

Unix Commands: rmUse rm to remove files

> rm {file(s)}

> rm file1 file2

> rm –i file1 - Prompts for confirmation before removing the file

NOTE: You have to either be the owner of the file or have write permissions to the directory containing the file!

Page 62: COMP 104: Intro to Unix

Unix Commands: mkdirUse mkdir to make a directory

> mkdir {directory}

> mkdir newdir

> mkdir –p newdir1/newdir2/newdir3

NOTE: You have to either be the owner of the file or have write permissions to the directory containing the new directory!

Page 63: COMP 104: Intro to Unix

Unix Commands: rmdir

Use rmdir to remove a directory

> rmdir {directory}

> rmdir newdir

> rmdir –p newdir1/newdir2/newdir3only works if the directories become

empty

Page 64: COMP 104: Intro to Unix

Question/Answer Session

Page 65: COMP 104: Intro to Unix

In class assignment

Page 66: COMP 104: Intro to Unix

Break (10 Minutes)

Page 67: COMP 104: Intro to Unix

Activity 3

Page 68: COMP 104: Intro to Unix

Unix File Editors

vi (pronounced “vee-eye”) is a visual editor that was created by Bill Joy.

vi is a “right-handed” editor

Other Unix editors: pico, emacs

Page 69: COMP 104: Intro to Unix

The vi Editor

Use vi to edit files

> vi {file}

NOTE: If the file does not already exist, vi will create it for you.

Page 70: COMP 104: Intro to Unix

The vi Editor: Modes

vi has two different modes:

Command ModeIn Command Mode, the characters you type are interpreted as commands.

Input ModeIn Input Mode, everything you type is inserted into the editing buffer.

Page 71: COMP 104: Intro to Unix

The vi Editor: Modes

vi starts in Command Mode by default

Type <Esc> to change from Input Mode to Command Mode

Hint: If you forget which mode you are in, hit the <Esc> key twice to get to Command Mode.

Hint: :set showmode will display the input mode in the lower right hand corner of the screen.

Page 72: COMP 104: Intro to Unix

The vi Editor: Inserting Data

From Command Mode:

i changes to Input Mode: insert before current position

a changes to Input Mode: insert after current position

I changes to Input Mode: insert at start of current line

Page 73: COMP 104: Intro to Unix

The vi Editor: Inserting Data

From Command Mode:

A changes to Input Mode: insert at end of current line

o changes to Input Mode: open below current line

O changes to Input Mode: open above current line

Page 74: COMP 104: Intro to Unix

The vi Editor: Saving and Exiting

In Command Mode (colon commands)::w writes data to the file (saves changes):wq writes data to the file and quits:w filename writes buffer to the named file:q quits:q! quits without savingZZ quits and saves

Page 75: COMP 104: Intro to Unix

The vi Editor: Moving the Cursor

In Command Mode:

h, move cursor one position to the left

j , move cursor one position downk , move cursor one position upl , move cursor one position to the

right

Page 76: COMP 104: Intro to Unix

The vi Editor: Moving the Cursor

:set number:set nonumber:n <Return> Jump to line number nnG Jump to line number n1G Jump to first lineG Jump to the last line

Page 77: COMP 104: Intro to Unix

The vi Editor: Moving the Cursor

w move cursor forward to first character of next word

<Return> Move cursor to beginning of next line

^F Move down one screenful^B Move up one screenful

Page 78: COMP 104: Intro to Unix

The vi Editor: Moving the Cursor

0 move cursor to beginning of current line$ move cursor to end of current line

^ move cursor to first non-space/tab in the current line

- move cursor to beginning of previous line+ move cursor to beginning of next line

Page 79: COMP 104: Intro to Unix

The vi Editor: Deleting Data

x delete character at cursorX delete character to left of cursorD delete from cursor to end of linedw delete one worddd delete the entire current line

Page 80: COMP 104: Intro to Unix

The vi Editor: Replacing Data

r replace a single character at the cursor

ra* Replaces the current character with “a”

R replace characters by typing over

Rnew stuff* Replaces the text at cursor with “new stuff”

cw replace the word by typing over

Page 81: COMP 104: Intro to Unix

The vi Editor: Copy and Paste

yy copies (yanks) the whole line to the buffer

p pastes data, insert before/above cursorP pastes data, insert after/below cursor

3yy* Copies (yanks) 3 lines

p* would paste the previously yanked lines at the

current cursor position!

Page 82: COMP 104: Intro to Unix

The vi Editor: Cut and Paste

dd deletes a line (puts it in the buffer)p pastes data, after/below cursor

dd*Cuts

p*Pastes

Page 83: COMP 104: Intro to Unix

The vi Editor: Searching

/{pattern} searches forwards for pattern in file/<Return> repeats forward search for pattern?{pattern} searches backwards for pattern in file?<Return> repeats backward search for pattern

n repeats search in same directionN repeats search in opposite direction

Page 84: COMP 104: Intro to Unix

The vi Editor: Replacing

:s/{pattern}/{replace} Replaces a pattern*Only works on the first occurrence

:s/{pattern}/{replace}/g*Works on all occurrences in the current line

:%s/{pattern}/{replace}/g*Works on all occurrences in the file

:line#1,line#2,s/{pattern}/{replace} *Works on all occurrences between the line numbers

Page 85: COMP 104: Intro to Unix

The vi Editor: Undo and Repeat

u undo last commandU restores the current line

. Repeat last command

Page 86: COMP 104: Intro to Unix

The vi Editor: Bonus

J Join Lines

:!{command}<return> Pause vi, execute specified shell command.

:r !{command}<return> Insert output of command after current line

^L Redisplay the current screen.

Page 87: COMP 104: Intro to Unix

The vi Editor: BonusHere is a helpful hint on controlling the length of lines in “vi”   One-way to do this is to press <Enter> at the end of each

line. Pressing <Enter> will insert a newline character, which marks the end of the line.

You can automatically tell “vi” to let it break a line into two when it gets within “n” characters of the right margin. To have “vi” break your lines automatically when they get within 6 characters of the right margin, use the following command in “vi” (command mode):

 

:set wrapmargin=6or:set wm=6

Page 88: COMP 104: Intro to Unix

The vi Editor: BonusThis is especially helpful when you are

typing a long paragraphs and don’t want it to be one continuous line.

  If you don’t want to have to enter this “vi”

command every time you enter “vi”, you can put it in the .exrc file. This file, created by you, belongs in your home directory. “vi” will read and execute any commands that it finds in this file upon startup.

In the .exrc file you don’t need to start any commands with a colon (:).

Page 89: COMP 104: Intro to Unix

The vi Editor: Bonus ~ (tilde) changes the case of the

current position

Page 90: COMP 104: Intro to Unix

In Class Assignment #4

Login to einstein and copy file/export/home/morris07/examples/vitextinto your home directory. Open the fileusing ‘vi’. Follow the directions in the

file.Save the file and exit vi.

Change the permissions on file youcreated to allow rwx to user, and r togroup/others.

Page 91: COMP 104: Intro to Unix

Question/Answer Session

Page 92: COMP 104: Intro to Unix

Wrap Up: What We Covered Unix file system structure File types and access permissions Create and use directories and files Edit files with vi Use the following Unix commands:

cd, chmod, cp, id, mkdir, mv, pwd, rm, rmdir, touch, umask, vi

Page 93: COMP 104: Intro to Unix

Next Week Unix processes Redirection, pipes, and filters Unix utilities (e-mail, ftp, telnet) Use the following Unix commands:

elm, ftp, grep, jobs, kill, look, pine, ps, sort, spell, telnet, uniq, wc

Final Exam

Page 94: COMP 104: Intro to Unix

Assignments for next week

UNIX File Systems and vi Commands Using the vi Editor Optional:

Practice Exam http://cs.franklin.edu/~morrisok/comp104/PracticeExam.doc

Practice Exam answers http://cs.franklin.edu/~morrisok/comp104/AnswersPE.doc

Page 95: COMP 104: Intro to Unix

Question/Answer Session