introduction to unix bent thomsen institut for datalogi aalborg universitet

36
Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

Upload: maud-francis

Post on 27-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 2

Unix Philosophy

• Designed by programmers for programmers• Toolbox approach• Flexibility and freedom• Networked – designed for server use• Multi-user / Multitasking• Conciseness

– Everything is a file or a process• File system has places• Processes have life

Page 3: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 3

Unix Structure

Hardware

KernelSystem Calls

Programs

Page 4: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 4

Interacting with Unix

• Sometimes through a GUI interface

Page 5: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 5

OpenLook on Sun

Page 6: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 6

Common Desktop Environment

Page 7: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 7

MacOS X

Page 8: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 8

Interacting with Unix• But most likely through a shell

– Xterm, telnet, Secure Shell– A shell is the command line interpreter (like the

DOS or command prompt in Windows)

• A shell is just another program– There are several shells (sh, csh, tcsh, bash …)

• A program or command– Interacts with the kernel– May be any of:

• Built-in shell command• Interpreted script• Compiled object code file

Page 9: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 9

Telnet

Page 10: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 10

SSH Secure Shell

Page 11: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 11

Getting started - login

• The login is the user’s unique name• Password is changeable

– Only known to user, not to system staff

– Except initial issued password

• Unix is case sensitive• Login and password prompt• System messages – you have new mail• The command prompt % $ [machine]>

Page 12: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 12

Example of login

Page 13: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 13

The command prompt

• Commands are the way to do things in Unix

• Commands are typed at the prompt

• Commands, as everything else, are case sensitive in Unix

• A command consists of a name, options (or flags) and sometimes arguments

[prompt]> <command> <flags> <args>

Page 14: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 14

Two Basic Commands

• The most useful commands you’ll ever learn:– man (short for “manual”)– help

• They help you find information about other commands– man <cmd> retrieves detailed information about <cmd>– help lists useful commands

Page 15: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 15

Who am I?• Commands that tell you who you are:

– whoami displays your username– id displays your username and groups

• Commands that tell you who others are:– finger [<name>] displays info for <name>– id [<username>] displays info for <username>

• Commands that change who you are:– su <username> “switch user” to <username> – login login as a different user

Page 16: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 16

Files and Directories• In Unix, files are grouped together in other files called directories,

which are analogous to folders in Windows• Directory paths are separated by a forward slash /

– Example /home/bt/FIT/docs• The hierarchical structure of directories (the directory tree) begins

at a special directory called the root, or /– Absolute paths start at /

• Example /home/bt/FIT/docs– Relative paths start in the current directory

• Example FIT/docs (if you’re currently in /home/bt)• Your home directory is where your personal files are located, and

where you start when you log in.– Example /home/bt

Page 17: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 17

The File System

Page 18: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 18

Directories (cont’d)

• Handy directories to know~ Your home directory

.. The parent directory

. The current directory

• Other important directories/bin

/tmp

Page 19: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 19

Simple commands• ls– LiSts the contents of specified files or directories (or

the current directory if no files are specified)– Syntax: ls [<file> … ]– Example: ls backups

• pwd– Print Working Directory

Page 20: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 20

More commands• cd

– Change Directory (or your home directory if unspecified)

– Syntax: cd <directory>– Examples:

•cd backups/unix-tutorial•cd ../class-notes

• mkdir– MaKe DIRectory – Syntax: mkdir <directories>– Example: mkdir backups class-notes

Page 21: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 21

More commands• rm

– ReMove– Syntax: rm [<options>] <files>– Example: rm class-notes.txt– Example: rm –ir backups

• rmdir– ReMove DIRectory, which must be empty– Syntax: rmdir <directories>– Example: rmdir backups class-notes

Page 22: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 22

Files (cont’d)• cp

– CoPies a file, preserving the original– Syntax: cp <sources> <destination>– Example: cp tutorial.txt tutorial.txt.bak

• mv– MoVes or renames a file, destroying the original– Syntax: mv <sources> <destination>– Examples:

• mv tutorial.txt tutorial.txt.bak• mv tutorial.txt tutorial-slides.ppt backups/

Note: Both of these commands will over-write existing files without warning you!

Page 23: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 23

File Permissions• Every file has three access levels:

– user (the user owner of the file)

– group (the group owner of the file)

– other (everyone else)

• At each level, there are three access types:– read (looking at the contents)

– write (altering the contents)

– execute (executing the contents)

Page 24: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 24

What You Can Do With Permissions

Permission File Directory

r (read) Read a file List files in …

w (write) Write a file Create a file in …

Rename a file in …

Delete a file in …

x (execute) Execute a file (eg shell script)

Read a file in …

Write to a file in …

Execute a file/shell script in …

Page 25: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 25

Changing Permissions• The “change mode” command:

chmod <level><op><permissions>[,…] <filename>

<level> string of: u, g, o, a (user, group, other, all)<op> one of +, -, = (gets, loses, equals)<permissions> string of: r, w, x, s, t, u, g, o

(read, write, execute, set-id, text,same as user, same as group, same as other),

• Examples:chmod u+rwx,go-w foobarchmod g=u,+t temp/chmod u=rwx,g=rwxs,o= shared/

Page 26: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 26

Process Management

• What can you do with it?– Start programs in the background– Run more than one program per terminal– Kill bad and/or crashing programs– Suspend programs mid-execution– List all jobs running in a shell– Move foreground jobs to the background– More …

Page 27: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 27

Three States of a Process• Foreground

– Attached to keyboard– Outputs to the screen– Shell waits until the process ends

• Background, running– Not attached to keyboard– Might output to the screen– Shell immediately gives you another prompt

• Background, suspended– Paused mid-execution– Can be resumed in background or foreground

Page 28: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 28

Background Processes• Listing jobs:

– jobs lists background “jobs” and job #’s– ps lists processes and their process id (“pid”)– %<job#> expands to the process id of the job

• Stopping foreground jobs– Press ^Z (Ctrl-Z) in the terminal window

• Starting a process in the background– Append a & character to the command line– Examples: ls –lR > ls-lR.out &

• Resuming a stopped job– In the foreground: fg [<pid>]– In the background: bg [<pid>]

Page 29: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 29

Killing Processes• The “kill” command:

kill [-<signal>] <pid>Send <signal> to process <pid>

• The “killall” command:killall [-<signal>] <command>Send <signal> to all processes that start with <command>

• Useful signals (kill –l for the complete list):TERM the default, “terminate”, kills things nicelyKILL will kill anything, but not nicelyHUP “hangup”, used to reload configurationsSTOP stops (suspends) a running process

Page 30: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 30

Redirecting input and output

• Simple!<program> < <FILE>

<program> > <FILE>• Examplesort < my_grades.txtls > dirlistNote a file called dirlist will be created if it doesn’t existDirlist will be overwritten. >> appends

Page 31: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 31

Piping• Piping is connecting programs together by using the

output of one program as the input to the next.• Syntax:

<program1> | <program2> | … | <programN>• A simple example (view a sorted file-listing a page at a

time):ls | sort | less

• By combining Unix utilities in a pipeline, you can build tools “on-the-fly” as you need them.

Page 32: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 32

Shell Shortcuts• Tab completion

– Type part of a file/directory name, hit <tab>, and the shell will finish as much of the name as it can

– Works if you’re running tcsh or bash

• Command history– Don’t re-type previous commands – use the up-arrow to access them

• Wildcards – Special character(s) which can be expanded to match other file/directory

names

* Zero or more characters

? Zero or one character

– Examples:• ls *.txt• rm may-?-notes.txt

Page 33: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 33

Editing Text• Which text editor is “the best” is a holy war. Pick one and

get comfortable with it.

• Three text editors you should be aware of:– vi – A lighter editor, used in programming– emacs – A heavily-featured editor commonly used in

programming– pico – Comes with pine (Dante’s email program)

Page 34: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 34

Printing

• Printing:– Use lpr to print– Check the print queue with lpq– lprm to remove print jobs– For the above commands, you’ll need to specify the

printer with –P<printer name>

Page 35: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 35

Exiting

• Logout – leave the system• Exit – leave the shell

• ^C interrupt

• ^D can log user off – often disabled

Page 36: Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet

September 2003 Bent Thomsen - FIT 1-2 36

Remember• In Unix, you’re expected to know what you’re

doing. – Many commands will print a message only if

something went wrong.– Most often there is no undo button– Make a backup copy if you are unsure– Some commands have interactive options

• E.g. rm –i

• Unix can be hard to learn, but it is loads of fun to use when you know what you are doing!