35
1 IPC144 Sessio n 8 UNIX

1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

Embed Size (px)

Citation preview

Page 1: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

1

IPC144Session 8

UNIX

Page 2: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

2

Objectives

Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machineTo be prepared for Lab#1

Page 3: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

3

UNIX

UNIXUNIX is an operating system that is multiuser, timesharing operating system.

We will begin by looking at two aspects of UNIX:Command line interfaceFile structure

Command line interfaceUnlike Graphical User Interface (GUI) operating systems such as MAC/OS or Windows, the typical UNIX system has a command line interface.

Any operation that you wish to have the UNIX operating system perform, requires a command to be typed into the keyboard. The command may be followed by various options and / or required parameters:

command [options] [parameters]

All commands entered are case-sensitive ('ls' is not the same as 'Ls')

Page 4: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

4

UNIX

Command line interface, continuedFor example, here is a UNIX command that contains no required parameters or options:

ls

This is the same command, with one of the options selected. In UNIX these options are also referred to as switches. A switch is always preceded by a minus sign. Switches modify the behavior of the command:

ls -l

Finally, here is a command followed by two switches as well as a required parameter. Multiple switches can be specified, with a minus sign in front of each switch, or one minus sign followed by all the switches together:

rm -R -f /usr/myDataor

rm -Rf /usr/myData

Page 5: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

5

UNIX

UNIX File StructureThe UNIX operating system's file system is hierarchical in nature. Other hierarchies that you may have seen or experienced:Family treeCompany organizational chartWindows file structure

The hierarchy in the UNIX file system consists of one of two elements:A directoryA file

Page 6: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

6

UNIX

DirectoryA directory in UNIX is something like a filing cabinet. The cabinet contains drawers, the drawers contain folders. It is where you store something.

In UNIX the drawers and folders are each a directory. These directories may contain other directories.

The top-most directory is called 'root', and has the symbol '/'.

An example file structure for UNIX may look like:

Page 7: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

7

UNIX

Directory

To specify a name of a directory, you must list all directories between the root and the directory you are trying to name, each separated by a '/' character.

For example, to name the 'sys' directory:

/opt/include/sys

. . .

/

var opt etc tmp

include cde

types sys

Page 8: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

8

UNIX

FileWithin the directories defined above, are files. The files contain data or software. The file is what you store in a directory.

Files may be found at any level of the directories.

UNIX file and directory namesThere are rules for the names you assign to files and directories:May not contain a space character (contemporary versions allow spaces- use with caution)May not contain a '/' characterMust not exceed 255 charactersThe names are case-sensitiveIf the name begins with a period '.', it will not show in normal directory listings. It is hidden.

Page 9: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

9

UNIX

The structure of a UNIX commandWhen reading the format of the UNIX commands, there are some conventions that are used:

{ } - enclose required parameters[ ] - enclose optional parameters| - separate a list of one word parameters, one of which is used in the command... - use to represent the previous definition can be repeated 0 or more times

For example, a stop signal in traffic could be a stop light or stop sign:stop {light | sign}

A computer system might have 0 or more peripherals:computer [pointingDevice] [scanner] [printer] [modem]

The optional pointingDevice can be further specified, and there can be many occurrences of these peripherals:

computer [mouse | trackball | tablet] [ scanner] [printer] [modem] ...

Page 10: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

10

UNIX

The commands for IPC144The commands that are presented in this section are not a complete list. They will provide enough information to perform some basic tasks.

The optional parameters and switches for the commands presented in this section are also not a complete list. The most common switches that you may expect to use are presented.

Page 11: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

11

UNIX

Getting help from the systemUse the 'man' command to access the on-line manual

man [-k] name...

Examples:to see the manual entry for the manual, enter the command

man man

A detailed explanation of the command will be shown.

to display all manual entries that contain the keyword 'mail'man -k mail

The command, followed by a brief explanation will be shown.

Page 12: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

12

UNIX

Listing directory contentsTo LiSt the contents of a directory, the ls command is used. The DOS equivalent is 'dir'.

ls [ -a ] [ -l ] [ File ... ]

The ls command by itself will simply list the files in a format similar to what you see with the DOS 'dir /w' command. In order to see more detail, use the -l switch. This will provide a listing similar to the standard 'dir' command in DOS.

The format of the output from the ls -l command will look like:

-rw-r----- 1 tmander staff 589 Aug 12 2000 v2.cdrwx------ 2 tmander staff 512 Jun 04 19:41 vi-tutor-rw------- 1 tmander staff 494 May 24 2000 w3s1.c-rwx--x--x 1 tmander staff 494 Jun 28 2000 prog1-rw------- 1 tmander staff 1052 Jul 25 2000 wt.c

Page 13: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

13

UNIX

Listing directory contents, continued-rw-r----- 1 tmander staff 589 Aug 12 2000 v2.cdrwx------ 2 tmander staff 512 Jun 04 19:41 vi-tutor-rw------- 1 tmander staff 494 May 24 2000 w3s1.c-rwx--x--x 1 tmander staff 494 Jun 28 2000 prog1-rw------- 1 tmander staff 1052 Jul 25 2000 wt.c

The first column is the file type and file permissions. The file type is 'd' for directory or '-' for file. The permissions are based on:

The third column is the owner of the file (tmander)The fourth column is the group name (staff)The fifth column is the file size in bytes (589 for the first one)The sixth column is the last modified date/time or creation date/timeThe last column is the file name.

Owner Group Public

R W X R W X R W X

Page 14: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

14

UNIX

Moving around in the hierarchy of directoriesFor the following discussion, assume the following directory structure:

/

var opt etc tmp

include cde lib

types sys

fileA

stdio.h

math.h

Page 15: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

15

UNIX

Moving around in the hierarchy of directoriesWhile you are connected to the UNIX system, you are always considered to be 'in' a directory. This directory is your working directory. Any actions that you perform will be applied against this directory, unless you specify otherwise.

To navigate between directories use the Change Directory command 'cd'. The same command that is used in DOS.

You can specify your destination directory by either:Absolute pathRelative path

Absolute pathAbsolute path is used to name a file or directory relative to the top of the hierarchy. The name is specified starting at the root and specifying each directory on the path to the destination:

cd /opt/include/sys

Page 16: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

16

UNIX

Relative pathRelative path is used to name a file or directory relative to the user's position in the hierarchy. The name is specified starting at the current directory

cd optcd includecd sys

Two special directories are defined: . and .. "." is the current directory".." is the parent directory (the one above the current directory)

If you are in /opt/include/sys and enter the command 'cd ..', your new directory would be /opt/include

Starting at /opt/include/sys, where would you be after: cd ../..cd ../../cdecd .

Page 17: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

17

UNIX

Creating a directoryTo create a directory is to MaKe DIRectory (mkdir). The DOS equivalent is 'mkdir' or 'md'.

mkdir Directory

Page 18: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

18

UNIX

Removing a directoryTo remove a directory is to ReMove DIRectory (rmdir). The DOS equivalent is 'rmdir' or 'rd'.

rmdir Directory

The directory must be empty (no files or other directories), otherwise the command will fail.

Page 19: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

19

UNIX

Determining current working directoryOn some UNIX systems, there is no hint as to what directory you are currently in. The Print Working Directory is a command to tell you where you are:

pwd

The DOS equivalent is 'cd'

Page 20: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

20

UNIX

Wildcard charactersThe next slides deal with the manipulations of files and directories. There are times when it is appropriate to refer to a group of files. By using wildcards, many files can be referenced based on some similarity in their names.

There are two wildcard characters:? - is used to represent any one character within the filename* - is used to represent any number of characters in the filename

assuming the following filenames: W1S1.ppt W1S2.PPT W3S1.PPT test.Ppt

* would represent all the filesW?S?.* would represent all the files, except test.PptW?S?.p* would represent only W1S1.ppt*.P* would represent W1S2.PPT, W3S1.PPT and test.Ppt

These wildcard characters can be used in any of the file manipulation commands.

Page 21: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

21

UNIX

Copying filesTo make a copy of a file the 'cp' or 'copy' command is used. The DOS equivalent is 'copy'.

{cp | copy} sourceFile targetFile

The sourceFile is the path of the file or directory to be copied, the targetFile is the path of the file or directory to be copied to.

For example to copy a file called myData in the opt directory to the etc directory the command would be:

cp /opt/myData /etc/myDataor

cp /opt/myData /etcor (to also change the name to yourData while copying the file)

cp /opt/myData /etc/yourData

What do you use to copy stdio.h from /opt/include to /opt/lib, assuming your pwd is /opt/include/sys and you are using relative paths only?

Page 22: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

22

UNIX

Renaming filesTo rename a file is to MoVe a file. The DOS equivalent is 'move' or 'rename'. The command structure is:

{mv | move} sourceFile targetFile

The sourceFile and targetFile are the same as in the cp command. However, a rename or move can be used to move a file from one directory to another.

For example:mv /opt/include/stdio.h /opt/lib

results is stdio.h being located in /opt/lib and no longer in /opt/include.

Page 23: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

23

UNIX

Deleting filesTo delete a file is to ReMove a file. The DOS equivalent is 'del' or 'erase'. The command structure is:

{ rm | delete } [ -f ] [ -r ] File ...

The file is either an absolute path or relative path.

-f forces the deletion of write protected files without prompting you-r removes any directories and their contents if the File parameter is a directory

rm /opt/include/stdio.h

rm -rf /opt/include

Page 24: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

24

UNIX

Displaying the contents of a fileThere are two ways to display the contents of a file. The first is to catenate the contents to the monitor. The DOS equivalent is 'type'.

cat File

This is a simple command that will display the contents without providing the user any ability to pace the output. If there is more than a screenful of data, the beginning of the file will scroll off the screen before you can read it.

Page 25: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

25

UNIX

Displaying the contents of a file, continuedAnother means to display the contents of a file and have the output paced so that you can read it is to use 'more'.

more File

A screen of data will be displayed. At the bottom of the screen will be a prompt indicating how much of the file has been viewed so far. There are two options at this time:Press 'enter key' to advance by one linePress 'space bar' to advance by one page.

Page 26: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

26

UNIX

Create or edit a fileAn editor is an application that allows the user to enter and maintain a text file on the computer. Editors are NOT word processors. Editors typically do not support multiple fonts/sizes/typefaces, colours, justification settings...

Editors are used to create and edit files that are used to configure the computer, as well as for writing programs (source code) that is to be submitted to the compiler.

There are three editors that we will look at, although there are many more.nledvipico

Page 27: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

27

UNIX

NLEDThis editor (Nifty Little EDitor) was created by Evan Weaver of the day school in the 1980s. The advantage of this editor is that it is simple to use and has some fairly good features. The disadvantage is that it is unlikely that you will encounter this editor outside of Seneca.

To create/edit a file enter:

nled file

The editor will open in a full screen, data entry mode. You can begin entering text, and moving around the screen using conventional cursor control keys. Although you cannot use a mouse to control the cursor and select text for cut and paste operations, there are ways to perform these operations through keyboard commands. They are referred to as block commands.

To access the editor commands, hit the <ESC> key followed by a one-letter command. To see the list of commands, press '?' after hitting the <ESC> key.

Page 28: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

28

UNIX

NLED, continued---------- NLED Programmer's Text Editor - Quick Online Reference -------------------- Copyright 1987-1994 Weaver Computer Development Services ----------<Esc> sequence summary: Press <Esc> to get the Command? prompt, then enter: A (*) to set Attention level (beep) O to run an Operating system command B to mark Beginning of block P (*) to Program a function key C to Copy block to current position Q to Quit without saving file D to Delete block R to find and Replace text E to mark End of block S to Save file to disk F to Find a text string T (*) to set number of spaces per Tab G to Go to a specific line U to Unmark block H (*) to cHange file access mode V (*) to saVe configuration I to Import another file into this X to save file and eXit K (*) to toggle tab c(K)ompression Y (*) to set case sensitivitY for find L to Load a new file into NLED Z to change case of current character M to Move block to current position ? to see this screen N to give a new Name to current file <Esc> or <Enter> to go back (*) - affects configuration which currently is: Attention level:2 Tab:8 [Compress On] File mode:Text Case Sensitive:No------------------------------------------------------------------------------Special keys (in addition to normal cursor movement): <Ctl/B>,<Ctl/E> Beg & End of line; <Ctl/F>,<Ctl/L> First & Last screen line; <Ctl/U>,<Ctl/D> Page Up & Down; <Ctl/T>,<Ctl/R> Top & bottom (Rear) of file; <Ctl/P>,<Ctl/N> Previous & Next word; <Ctl/X> Enter hex code; <Ctl/O> Ins/Ovr Command? (? for help, <Esc> to cancel) Line:15 Col:1 xx Ins

Page 29: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

29

UNIX

VIThe vi (VIsual) editor is on all UNIX systems. However, it is best described as a user-hostile editor, whereas nled is user-friendly.

There are two modes that the editor is in, either command mode or entry mode.

To get into command mode, hit the <ESC> key. This is where the similarity between nled and vi ends.

When vi starts, you are in command mode. While in command mode you can perform the following operations:move the cursorenter insert modesearch for textdelete lines / words / characterschange words / charactersperform file operations

Page 30: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

30

UNIX

Move the cursorThe cursor can be moved using the standard cursor control keys, as well ask - upj - downh - leftl - right

0 - Go to beginning of line$ - Go to end of line#G - Go to line # (line number followed by shift-g)G - Go to bottom of file (shift-g without a line number)

Enter insert modeThe following commands can be used to insert data:

i - insert beginning at the current cursor positionI - insert at the beginning of the linea - insert (append) after the cursorA - insert (append) at the end of the lineo - open a new line below the cursor, and begin insertingO - open a new line above the cursor, and begin inserting

Page 31: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

31

UNIX

Delete lines / words / charactersdd - delete current line#dd - delete # linesdw - delete to end of word (starting at cursor)x - delete character under cursorX - delete character before cursor

Search for textTo search for text, type a forward slash followed by the text to be located.

/search for this

Change words / characterscw - change to end of word (starting at cursor)r_ - replace character under cursor with the next character entered (the _)

Page 32: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

32

UNIX

Perform file operationsWhile in command mode, enter ':' to activate the command line. The following file commands are available:

q - quit vi, file is checked for changes.q! - quit without saving or checking for changesw - writew file - write (save) with a new file namee file - load (edit) file

Page 33: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

33

UNIX

PICOThis editor is available on “all” UNIX systems. Not as feature rich as NLED, however, considerably more user friendly than VI. This editor is introduced in the lab.

To activate a command, hold down the CTRL key (represented by the “^” character) and strike the (lowercase) character associated with the command:

Page 34: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

34

UNIX

Password managementTo change your password in UNIX, the command is:

passwd

You will be prompted to enter your current password, then you will be asked to enter your new password twice.

If you entered the current password correctly, and both entries of your new password match, then your password will be changed.

Page 35: 1 IPC144 Session 8 UNIX. 2 Objectives Review the structure of UNIX To recall basic UNIX commands to perform typical user functions on a UNIX machine To

35

UNIX

File redirectionThe output of a command, such as the ls command, can be redirected from the screen to a file. This symbol does exist in DOS.

The symbol to redirect output, and create/overwrite a file is:

>

For example, to redirect the contents of the ls command to a file called myFiles, the command is:

ls > myFiles

If you wish to append the output to the end of a file, the redirection symbol is:

>>