Comp 104 intro to unix
Download
1 / 95

COMP 104: Intro to Unix - PowerPoint PPT Presentation


  • 140 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' COMP 104: Intro to Unix' - shino


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Review of last week
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


Agenda activity 1
Agenda – Activity 1

  • Introduction to the Unix File System

    • Unix file system

    • File Types

    • Directory File Paths

    • Access Permissions

    • Demonstration of file system


Agenda activity 2
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


Agenda activity 2 continued
Agenda – Activity 2 Continued

  • More UNIX Commands

    • File Permissions:

      • id, umask, chmod

      • Demonstration of id, umask, chmod

  • In class assignment

  • Break (10 minutes)


Agenda activity 3
Agenda – Activity 3

  • vi Editor

    • Introduction to the vi editor

    • Practice with the editor

  • Preview of next week



The unix file system
The Unix File System

Everything is aFile in Unix


Types of unix files
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)


The tree structured file system
The Tree-Structured File System

root (/)

bin dev etc lib lost+found sys tmp usr

bin games lib local spool


Another example
Another Example

The 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


Common unix directories
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


Unix directories
Unix Directories

Root Directory /

Your Home Directory /export/home/{userid}

$HOME variable

  • Shows your current home directory

  • print $HOME - display variable setting


Unix directories1
Unix Directories

Present Working Directory

  • Your current location

    -or-

  • Current Directory


Unix commands pwd
Unix Commands: pwd

Use pwd to display the name of your current working directory

/export/home/morris07/> pwd



Absolute path
Absolute Path

Absolute Path

/export/home/morris07/labs

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


Unix commands cd
Unix Commands: cd

Use cd to change your working directory

/export/home/morris07/>

cd {directory name}




Relative path
Relative Path

If your pwd was

/export/home/morris07/

You could do:

cd examples

To move into the examples subdirectory



Relative path shorthand
Relative Path (Shorthand)

Single dot . Your current directory

Double 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





Unix commands ls
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 “.”


Unix commands ls1
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


Unix commands ls2
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






Using relative path in ls
Using Relative Path in ls

  • ls -al ..

    Lists your parent directory

  • ls –al ~

    Lists your home directory




Unix commands touch
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


Unix commands id
Unix Commands: id

Use id to display your userid and groupid

/export/home/morris07/>id



Unix security
Unix Security

  • Login name and a password

  • Encryption on important files

  • Access permission


Encryption of files
Encryption of files

  • Text page 334

  • crypt

    • Description will make more sense after next week

    • Requires a key – do not forget the key


Access permissions
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


Directory permissions
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


How permissions are managed
How Permissions are Managed

There are three Permission Groups:

  • Owner:

  • Owner’s Group:

  • Everyone Else/Other:


Permissions
Permissions

-rwxrwxrwx 1 morris07 student 512 Jan 12 14:07 file.exe

-rw-rw- rw- 1 morris07 student 812 Jan 12 14:22 file.name

drw-rw-rw- 1 morris07 student 812 Jan 12 14:22 labs

-----------------------------------------------------------------------------------------

r read permission

w write permission

x execute permission

- permission not granted

-----------------------------------------------------------------------------------------

ownergroupeverybody At the far left, 1’st character

rwx rwx rwx shows type of file. “-” ordinary

“d” is directory


Unix commands chmod
Unix Commands: chmod

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

>chmod{mode} {file}

>chmod777 file.name


Numeric value of permissions
Numeric Value of Permissions

FILE MODE, or MODE

read permission = 4

write permission = 2

execute 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


Chmod calculating the mode
chmod: Calculating the Mode

Number 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


Numeric value of permissions1
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


Symbolic mode file permissions
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





Unix commands umask
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



Umask calculating the umask
umask: Calculating the umask

File Type Beginning File Mode

Non-executable files 666

Executable files 777

Directories 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.




Unix commands cp
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)


Unix commands mv
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


Unix commands rm
Unix Commands: rm

Use 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 ownerof thefile or have write permissions to the directory containing the file!


Unix commands mkdir
Unix Commands: mkdir

Use mkdir to make a directory

>mkdir {directory}

> mkdir newdir

> mkdir –p newdir1/newdir2/newdir3

NOTE: You have to either be the ownerof thefile or have write permissions to the directory containing the new directory!


Unix commands rmdir
Unix Commands: rmdir

Use rmdir to remove a directory

> rmdir {directory}

>rmdir newdir

>rmdir –p newdir1/newdir2/newdir3

only works if the directories become empty






Unix file editors
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


The vi editor
The vi Editor

Use vi to edit files

>vi {file}

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


The vi editor modes
The vi Editor: Modes

vi has two different modes:

  • Command Mode

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

  • Input Mode

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


The vi editor modes1
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.


The vi editor inserting data
The vi Editor: Inserting Data

From Command Mode:

i changes to Input Mode: insert before current position

achanges to Input Mode: insert aftercurrent position

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


The vi editor inserting data1
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 belowcurrent line

Ochanges to Input Mode: open abovecurrent line


The vi editor saving and exiting
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 saving

ZZ quits and saves


The vi editor moving the cursor
The vi Editor: Moving the Cursor

In Command Mode:

h,  move cursor one position to the left

j , move cursor one position down

k , move cursor one position up

l , move cursor one position to the right


The vi editor moving the cursor1
The vi Editor: Moving the Cursor

:set number

:set nonumber

:n <Return> Jump to line number n

nG Jump to line number n

1G Jump to first line

G Jump to the last line


The vi editor moving the cursor2
The vi Editor: Moving the Cursor

wmove 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


The vi editor moving the cursor3
The vi Editor: Moving the Cursor

0move 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


The vi editor deleting data
The vi Editor: Deleting Data

x delete character at cursor

Xdelete character to left of cursor

D delete from cursor to end of line

dw delete one word

dd delete the entire current line


The vi editor replacing data
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”

cwreplace the word by typing over


The vi editor copy and paste
The vi Editor: Copy and Paste

yy copies (yanks) the whole line to the buffer

p pastes data, insert before/above cursor

P pastes data, insert after/below cursor

3yy

* Copies (yanks) 3 lines

p

* would paste the previously yanked lines at the current cursor position!


The vi editor cut and paste
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


The vi editor searching
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 direction

N repeats search in opposite direction


The vi editor replacing
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


The vi editor undo and repeat
The vi Editor: Undo and Repeat

u undo last command

U restores the current line

. Repeat last command


The vi editor bonus
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.


The vi editor bonus1
The vi Editor: Bonus

Here 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=6

    or

    :set wm=6


The vi editor bonus2
The vi Editor: Bonus

This 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 (:).


The vi editor bonus3
The vi Editor: Bonus

  • ~ (tilde) changes the case of the current position


In class assignment 4
In Class Assignment #4

Login to einstein and copy file

/export/home/morris07/examples/vitext

into your home directory. Open the file

using ‘vi’. Follow the directions in the file.

Save the file and exit vi.

Change the permissions on file you

created to allow rwx to user, and r to

group/others.



Wrap up what we covered
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


Next week
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


Assignments for next week
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



ad