1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities...

52
1 Table of Contents Introduction to Unix. Basic Utilities Sending and Receiving Electronic Mail. Getting Help Creating and Editing Files. Advanced Utilities. Directory Structure. Files and Access Permissions grep Processes sed and awk Shell Variables and Commands .
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    225
  • download

    1

Transcript of 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities...

Page 1: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

1

Table of Contents

Introduction to Unix. Basic Utilities Sending and Receiving Electronic Mail. Getting Help Creating and Editing Files. Advanced Utilities. Directory Structure. Files and Access Permissions grep Processes sed and awk Shell Variables and Commands.

Page 2: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

2

Introduction

What is an operating system– It is a control program for a computer; it allocates

resources and schedules tasks.

Page 3: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

3

Overview

UNIX is a time-sharing system. Multi-user (vs. Single user) Multi-tasking (each user may run more than one

job). Interactive (vs. batch) Command-line prompt, or Graphical user interface through X-Windows

Page 4: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

4

Overview 2

Utilities Command interpreter is called a SHELL.

– Can also be used as a programming language -- Shell Scripts.

File structure Complaints: unfriendly, terse, treacherous,

unforgiving, and inconsistent. – TRUE? yes. It is designed for advanced programmers,

Page 5: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

5

Using UNIX

Need: Login Name and Password. The following is a set of special keys and

commands to use:– BACKSPACE deletes one character.– CONTROL--U deletes an entire line.– CONTROL--W deletes a word.– CONTROL--R redraws a line.– CONTROL--C aborts program execution.– CONTROL--D exits the shell (logs you out).

Page 6: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

6

Basic Utilities

Simple Utilities– ls: The "list" command – displays all files in your

current directory Option "-l" – long listing; shows all information. Option "-a“ – “all files”; show hidden files as well

– mv <filename1> <filename2>: The "move" command – move one file to another (effectively “rename”).

– cp: <filename1> <filename2>: The "copy" command – copy one file to another.

– Cat <filename>: The "conCATenate" command – display the file on the screen.

Page 7: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

7

Receiving Electronic Mail

$ Mail– ? save [msglist] file– ? ? – ? type [msglist] – ? next – ? edit [msglist] – ? from [msglist] – ? delete [msglist] – ? undelete [msglist] – ? save [msglist] file – ? reply [message] – ? Reply [msglist]

– ? preserve [msglist] – ? mail user – ? quit – ? xit – ? header – ? ! – ? cd [directory] – ? list – ? top [msglist] – ? z [-]

Page 8: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

8

Sending Mail

Interactively:– $ Mail mjm– <text of letter goes here; CR's OK>– <CTRL>--D to end message

Note is in a file:– $ Mail mjm < lab1.txt

– DO NOT USE ">" INSTEAD OF "<"

Page 9: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

9

Pine

“pine” is a screen-base mail handler– Commands are entered directly, or as <CTRL>

sequences.– Handles attachments:

Text files Binary files “uuencode”d files

– To read your mail: $ pine

– To send mail, optionally give a username: $ pine [email protected]

Page 10: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

10

Pine’s main menu (top)

After executing pine, you see the following menu:

? HELP - Get help using Pine C COMPOSE MESSAGE - Compose and send a message I MESSAGE INDEX - View messages in current folder L FOLDER LIST - Select a folder to view A ADDRESS BOOK - Update address book S SETUP - Configure Pine Options Q QUIT - Leave the Pine program

Page 11: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

11

Pine’s main menu (Bottom)

This is the bottom of the main menu:

Copyright 1989-1998. PINE is a trademark of the University of Washington.

? Help P PrevCmd R RelNotes O OTHER CMDS > [ListFldrs] N NextCmd K KBLock

Page 12: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

12

Reading your mail with pine

From the main menu, type “l”, or use the arrow keys to highlight “Folder List” and press <enter>

Select “inbox” from the folder list– pine displays all current messages. Again, use the

arrow keys to move to the note you wish to read, then press <enter> to see it displayed.

Page 13: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

13

Commands used in message index:

These command appear at the bottom of the screen when viewing your inbox (or any other message index):

? Help < FldrList P PrevMsg - PrevPage D Delete R Reply O OTHER CMDS > [ViewMsg]N NextMsg Spc NextPage U Undelete F Forward

Note: always look at the bottom of the screen to see

what commands are currently available.

Page 14: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

14

Sending Mail

To start:– $ pine <username> from the Unix prompt– <select “Compose Message” from pine’s main menu

and enter the username.

Enter a Subject line

Go to the “message text” area and type your message.– Note the editing commands at the bottom of the screen.

Page 15: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

15

Other pine Features

Maintain multiple message indexes (folders) by simply saving your e-mail notes to a different index.– To create a new folder or append to an old one, simply

save an e-mail note to the folder

Keep commonly used e-mail addresses in the “address book”.– Enter the address book from the main menu and follow

the directions.– May also “take addresses” while reading a note.

Page 16: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

16

Getting Help

$ man --- Run command interactively

$ man <Topic> --- Get help on <Topic> $ man -k <Keyword> --- Find all Topics which

contain <Keyword>

Page 17: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

17

Creating And Editing Files

$ vi <filename> Modes:

– vi Input mode– vi Command mode– vi last line mode– ex Command mode– ex input mode

Page 18: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

18

Starting and Stopping

$ vi lab2.txt Commands to exit (Note Case):

– ZZ -- save and exit from vi– :wq -- save and exit from vi– :w lab2.bak -- save as "lab2.bak"– :q -- QUIT, no changes have been made– :q! -- QUIT, destroy all changes– $ vi --r lab2.txt -- start & recover

Page 19: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

19

Basic Commands

h -- move one character to the left. l -- move one character to the right. j -- move one line down. k -- move one line up. w -- move one word right. b -- move one word left. i -- enter input mode before current point. I -- enter input mode at beginning of current line. a -- enter input mode after current point. A -- enter input mode at end of current line.

Page 20: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

20

Basic Commands 2

O -- open a new line before the current line and enter input mode. o -- open a new line after the current line and enter input mode. <ESC> -- switch from input mode to command mode. r <char> -- replace the current character with <char>. R <text> -- replace characters with text (end with <ESC>). u -- undo the command just executed. U -- undo all changes to the current line. /<text> -- search for the next occurrence of <text> in the buffer. ?<text> -- search for the last occurrence of <text> in the buffer. :s/<old>/<new> -- substitute new for old text on the current line. :s/<old>/<new>/g -- substitute new for old text globally.

Page 21: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

21

Basic Commands 3

cw back last delete before current -- change to end of word. c3w -- change to end of 3rd word. dd -- delete current line. 5dd -- delete next five lines. dw -- delete current word. p -- bring back last delete after current character. P -- bring character.

Page 22: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

22

PICO

“pico” is another editor whose format looks suspiciously similar to pine’s.

To run– $ pico <filename>

Commands appear at the bottom of the screen (as with pine). Most are self-explanatory.

Page 23: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

23

Advanced Utilities

who: Displays ``who'' is currently logged into the system.

write <User>: Allows you to write to another user. mesg: Turn on and off "write" access. echo <text>: Displays <text> on the screen. date: Displays the current date. lpr <filename>: Prints <filename>.

Page 24: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

24

Even More Utilities

head <filename>: Displays the first 10 lines of <filename>.

tail <filename>: Displays the last 10 lines of <filename>.

uniq: Prints only unique rows (duplicates omitted). diff <filename 1> <filename 2>: Compares files. sort <filename>: sorts <filename>. finger <Username>: displays information about

<Username>.

Page 25: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

25

Directories

Special Directories:– HOME directory -- where you initially log in to.

Manipulating directories:– Creating Directories -- use mkdir <directory name>.– Deleting Directories -- use rmdir <directory name>.– Changing Directories -- use cd <directory name>.

Page 26: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

26

THE PATH

echo $PATH gave you your PATH. All the places to look for programs to execute.

Page 27: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

27

Files

Special files:– .profile (or .login)

Commands to be executed every time you log in.

– .shrc (or .cshrc, or .bashrc) Commands to be run every time you spawn a new shell.

– .plan A text file to be displayed when another user “finger”s your

account.

Note: all filenames that begin with a “.” are hidden.

Page 28: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

28

ACCESS PERMISSIONS

Owner, Group, All Access May be Read, Write, Execute for all three. Current settings found with

ls -l:

-rwxr-x-r-- mjm mjm Sept 10 myfile.txt

Page 29: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

29

GRANTING ACCESS

$ chmod 604 .plan– each different user group:

1 is execute privilege 2 is write privilege 4 is read privilege

– 604: owner has read and write group has no privileges all has read privilege

Page 30: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

30

GRANTING ACCESS 2

$ chmod a+r .plan– First character is:

o owner g group a all users

– Second character is: + add this privilege - remove this privilege = set privilege

– Third character is: r read w write x execute.

Page 31: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

31

FILE WILDCARDS

* -- matches any string. ? -- matches exactly one character. [ ... ] -- matches any element of a list.

Page 32: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

32

INPUT/OUTPUT REDIRECITON

Output Redirection:– $ who -b > .lastboot

Input Redirection:– $ Mail marty < MyNote.txt

Page 33: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

33

GREP and Regular Expressions

GREP: "Get Regular ExPression"– Searches files for a string.– For example:

$ grep Smith test.txt $ grep "Aaron, Mary" test.txt

Page 34: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

34

GREP Options

--c : COUNT matches. --i : IGNORE case. --l : LIST file names -n : precede each match with a line NUMBER. -v : reVERSE match.

Page 35: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

35

Regular Expressions

The "string" is a regular expression. May contain wild cards.

– A period: match exactly one character.– Square bracketes [...]: a character class– *: match zero or more occurences of the previous RE.

EXAMPLE: ab*c

– ^ : match the beginning of a line.– $ : match the end of a line.– \. \* \^ \$ : Match special character.

Page 36: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

36

Pipes

Take output of one command as input to another:– $ who | sort

This is equivalent to – who > temp– sort temp– rm temp

Page 37: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

37

"tee" And The Background

"tee”: send the output of one command to the input of another command

Example:– who | tee who.out | grep cs– Find people logged in with "cs" in their username– display it AND save it to a file called "who.out"

Page 38: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

38

Running In The Background

Multitasking Aspect of UNIX. One task is in the foreground; others in

background. To run in background: append an ampersand (\&)

to the command line. Example:

– ls -l | grep -i ”^d" > temp.txt &

Page 39: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

39

Terminating Processes

To kill all in the background,– kill 0

To kill One:– Find its PID (process ID) with "ps" (process status)– kill <PID>

Page 40: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

40

sed AND awk sed

sed: "stream editor"– A non-interactive editor– Input:

A list of editing commands. A file to operated on.

– Output: The lines of the file affected.

Page 41: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

41

Example:

Data File (called "new"):– Line one– The second line– The third– This is line four– Five– This is the sixth sentence– This is line seven– Eighth and last

Page 42: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

42

Example (cont)

sed '/line/p' new– Prints all lines containing "line".– Prints all lines it processes. – Lines 2, 4, and 7 will be printed twice.

Page 43: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

43

Example (cont)

sed --n '/line/p' new– The "--n" option tells sed to print just the lines that

match.

sed --n '3,6 p' new– This command prints out lines 3 through 6.

sed 's/line/sentence/g' new.txt– This command substitutes each occurrence of "line"

with "sentence" and prints the output.

Page 44: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

44

Editing Scripts

sfile: 2,4 c\ SED WILL INSERT THESE\ THREE LINES IN PLACE\ OF THE SELECTED LINES\

Page 45: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

45

Editing Scripts (cont)

sed -f sfile new.txt– Line one– SED WILL INSERT THESE– THREE LINES IN PLACE – OF THE SELECTED LINES– Five– This is the sixth sentence– This is line seven– Eighth and last

What if I use the --n option here?

Page 46: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

46

Observation

What goes in the script is a regular expression, preceded by an optional address range. If none is given, sed will work on every line in the file, one at a time. Also a command may be given. These are VERY similar to the command line inputs in VI (note the similarity with substitute, change, etc).

Page 47: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

47

awk

A pattern scanning and processing language. Constructs reminiscent of "C”. <PLACE CARS FILE HERE!!!!!>

Page 48: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

48

awk Examples

To find all chevys: – awk '/chevy/' cars

To print the year and make of every car:– awk '{print $3, $1}' cars

To print the year and make of all chevys:– awk '/chevy/ {print $3, $1}' cars

To print those cars whose make begins with a "h":– awk '/$1/^h/' cars

To print all cars made in 65:– awk '$3 == 65' cars

Page 49: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

49

Using awk Files

To print a header for the file, create a file called "awkfile" which contains:– BEGIN { print "Make model year mileage price"– print "----------------------------------------"}– {print}

awk -f awkfile cars

Page 50: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

50

Variables

Pre-Defined Shell Variables– HOME -- The path to your home directory.– PATH -- your current path.– MAILPATH -- your current mail path.– PS1 -- your current primary shell prompt.– PS2 -- your current secondary shell prompt.

To see any of these variables:– echo $<VAR>

Page 51: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

51

User Variables

Program to maintain a checkbook: $ awk 'NR == 1 {balance = $0} NR > 1

{balance+=$3; print $0, balance}' infile To save typing: $ docheck=awk 'NR == 1 {balance = $0} NR > 1

{balance+=$3; print \$0, balance}' infile To execute: $ $docheck To save command, put it in your ".profile" file.

Page 52: 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities n Sending and Receiving Electronic Mail. Sending and.

52

Command Substitution

Variables may be assigned the RESULT of a command:– $ currdir=`pwd`– $ echo You are currently using the $currdir directory

This can be done in one step:– $ echo You are currently using the `pwd` directory