26
1 Introduction to Linux Comp 1002/1402 Objectives Teach Basics of Linux Operating System • Teach ownership and permissions of the files and directories. Explain why these issues exist. How to set permissions files/directories How to manipulate files/directories list files, create, delete, and move just about anything on the file system • Mention vi - a standard Unix text editor

Introduction to Linux Objectives

Embed Size (px)

Citation preview

1

Introduction to Linux

Comp 1002/1402

Objectives• Teach Basics of Linux Operating System• Teach ownership and permissions of the files

and directories. • Explain why these issues exist. • How to set permissions files/directories• How to manipulate files/directories

– list files, create, delete, and move just about anything on the file system

• Mention vi - a standard Unix text editor

2

Overview• Linux is the kernel of an operating system• In family of the UNIX operating systems • Has all of the features of a modern, full-

fledged operating system – true multitasking, threads, virtual memory– shared libraries, demand loading– shared, copy-on-write executables– proper memory management– video frame buffering, and TCP/IP networking.

Overview• Linux began as a command line OS

– Most applications are run from command line– type a command with or without parameters– contrasts with a GUI-driven operating system, or

window-based operating system• There are Linux applications to configure your

system through a GUI.• Will teach Linux commands to develop,

compile and run simple “C” programs

3

Scope of Course; On-line help• Details of how Linux works

– Outside the scope of this course. – To know more about Linux, do it on your own..

• SCS help webpage:– http://www.scs.carleton.ca/help– “Getting Started with Unix” link

• Work from home:– Putty– Winscp

Help from the Manual

• Like all Unix variants, tends to be cryptic• It is case sensitive• A good beginner’s book is usually handy• Online documents on almost all commands

and functions used in Linux• Access these documents via the man

command • Brings up a “Manual” page

4

Help from the Manual• To get a “manual entry” type:

man <item>

At the command prompt

• For example, typing :man man

– Tells you more about the “man” command• Typing

man ls

– Tells you more about the “ls” command

Concepts : term, shell, console

• Operating System != operating environment

• GUI Environment (Gnome, KDE,…)

• Shell=Command Prompt(sh, csh, ksh, bash)

• Even GUI’s allow term, xterms…

5

Your Account & Files

• Each student has – An account – A corresponding “home directory”– Directory the user is placed in on logging in.– Users have full permission & free reign here.

• Hierarchical file structure from "/"– ~ is your account – ~<username> is someone else’s

Linix : Directories and Files• Linux file systems are organized in a tree• "/"as the root directory• The rest of the directories/subdirectories

continue down from it• The line from the root directory to the file you

seek (e.g., pointer.c) is called a path– /home/70user4/nussbaum/code/pointer.c.

• Access protection for each file/directory

6

Files and Directories

/ home 70user4 nussbaum pointer.c

70user5

70user1

code

Home DirectoryRoot Directory

mailbin

dev

Users & Groups

• Every User has a unique id (login id)

• Every User can also belong to a group

• Your id and your groupid are important– They enable security

7

File Ownership

• Every file and directory has an Owner

• Owner : Usually the creator of the file!

• The Owner can decide who can:– Read, Write and Execute the file/directory

Access PermissionRead Permission• Only allows reading not change

Write Permission• Allows File creation, modified or deletion

Execute Permission• Execute file, open directory

8

Viewing permissions

list command with -l:ls –l <filename(s)>

oommen@Sigma01> ls –l codedrwxr-xr-x 1 nussbaum faculty 367 Sept 8 11:37 code

Let’s examine these one by one….

Viewing permissionsdrwxr-xr-x 1 nussbaum faculty 367 Sept 8 11:37 code

• First Column– "d" indicates that code is a directory and not a file.

– After "d", the access permission (broken down into three categories)

– Permissions associated with file - letters rwx refer to read, write, and execute respectively

• Third Column– tells the owner of the file or directory (nussbaum)

• Fourth Column– The name of group for the file/directory (faculty)

9

File Access Permissionsdrwxr-xr-x 1 nussbaum faculty 367 Sept 8 11:37 code

Owner Permissions• First three letters (rwx) in the sequence of bits• Owner (nussbaum) can read, write and execute code.Group• The next three letters (r-x) are the permissions to everyone in the group. • The "-" means that the permission corresponding to the missing letter is not granted. • The r-x means :

– Everyone who is in the group "faculty"– can read and execute code but cannot write to it.

World• The last three letters (r-x) are the permissions for everyone else. The permission is

the same as for the group "faculty "– All can read and execute code, but cannot write to it.

File Permissionsdrwxr- xr- x2 nussbaum faculty 367 Sept 8 11:37 code

• Permission are displayed as a sequence of 9 permission flags• 3 sets of permissions

– Owner Permissions - the first three flags– Group Permissions - the second three flags– World Permissions - the last three flags

• Each permission sets consists of– Read permission– Write permission – Execute permission

10

File Permissionsdrwxr- xr- x1 nussbaum faculty 367 Sept 8 11:37 code

In the above example the permission sequence is rwxr-xr-x• Owner Permissions - Owner (nussbaum) can read, write and execute code.• Group Permissions -

– The "-" means that the permission corresponding to the missing letter is not granted.

– The r-x means :• Everyone who is in the group "faculty"• Can read and execute code but cannot write to it.

• World Permissions -– The permission is the same as for the group "faculty "– All can read and execute code, but cannot write to it.

Changing permissionschmod a+rx pointer.c

(+ add) (– remove) (= only)a : Mean you add to “all” (owner, group and world)The permission groups are represented as shown in the

table: • Operator `+' causes the permissions selected to be

added• Operator `-' causes permissions selected to be

removed• Operator `=' causes them to be the only permissions

that the file has.

11

Changing permissionschmod a+rx pointer.c

(+ add) (– remove) (= only)chmod g-x, o-r pointer.c

aAll of aboveoWorldgGroupuOwner (User)SymbolPermission

Changing Permissions

Also specify by octal - interpreted as binary …

chmod 421 pointer.c100010001

User : Read ; Can’t Write/ExecuteGroup : Write ; Can’t Read /ExecuteWorld : Execute ; Can’t Read / Write chmod 630 pointer.c

110011000chmod 766 pointer.c

111110110

12

Creating Directories

mkdir name

• The following command creates a directory called “courses” as a subdirectory of ‘John’ directory:

mkdir courses

• You can also specify a path, when creating directories as follows:

mkdir /home/70user4/john/courses

Current Working Directory

Where am I in the directory hierarchy? Use the command pwd –

prints current working directoryFor example, if I issue the pwd commandwhile I am at code directory:pwd

I’ll get on the consolehome/70user4/john/code/

13

List

• The "ls" command lists files as the DOS dir. • Issue the ls command

– You see the files/directories in current directory. • Files and Directories are distinguished by

affixing the name of directory with slash. – For a directory: “code/ “ – For a file: “pointer.c”

Switches for ls - List

• There are many switches or options for ls These include:

ls –la (all files)

ls –lc (sort by change time)

14

Deleting Files

• The command, rm, is used to remove a file from the current working directory.

• You must own the file to remove it. • To remove a single file, specify its name

when you run rm as follows:•rm myfile

• Careful there is no undelete!

Deleting Files - Flags• You can also use full pathways to remove a file

if the file is in another directory. • For a file to be deleted, its write permissions

must have not been removed. Otherwise, you may be “denied permission”.

• To force the deletion of the file no matter what, use the -f option as follows:

• rm -f myfile•rm -i myfile

15

Deleting Files - Flags

• To remove all the subdirectory trees, you pass the -rf options to the rmcommand

• Be very careful as this will wipe out everything under the “birds” directory– rm -rf birds

Removing Directories• Use rmdir to remove a directory as :

rmdir <name>

• Usually you remove a directory from the directory you are in.

• You can also use full pathways. • You must own the directory to remove it. • It must not have other files/subdirectories.

rmdir mail

16

Copying Files• Use the cp command to copy

files/directories. • The syntax of the copy command is :

cp [options] source destination• source and destination are different file

names. For example, the following command copies the pointer.c file from the current directory to the /tmp directory:

cp pointer.c /tmpcp pointer.c /tmp/newpointer.c

Copying Files• Many Flags - See Manual• If the destination is also a file, then it copies the

source file onto the destination file. • It is an error if the destination is not a directory

and more than two files are given. • cp command does not copy directories. • To recursively copy the contents of the John

directory to a tmp directory, you use : • cp -R John /tmp

17

Rename Files

• The command mv moves files/directories. • The syntax of the command as follows:

mv source destination

• You must name the source and destination.• A common use :mv myfile home/70user4/John/mail

Concatenate : Cat

cat file1cat file1 file2

Displays the files to “Standard Output”

18

More/Less

more file.txt

Displays the file “piecewise”– “space” advances a screen– “return” advances a line– b to backup a screen– h shows you the all commands– q to exit

Redirection

• Standard Input is from keyboard• Standard Output is the monitor

• Able to change this!command < infile > outfilecommand < infilecommand > outfilecommand >> outfile

19

Pipes

• Output of one command “piped into” another

command1 | command2

• Do the first command command1 • Feed its output as input for command2

Wildcards

Group of files accessed using wildcards

* matches everythingls g* lists all files beginning with gls go.* lists files named go with any extensionls * lists all files

20

Other Commands

•grep search a file or files for a string.

>grep hello file_id

•spell spell-checks a file

>spell file_id

Other Commands

• history gives a history of your commands

• date displays date >date• who who is on the system• finger get more info about another user.• jobs one of a number of job control

commands. Gives the job No. of your jobs.– kill kill a specified job number

21

Introduction to Editing

• How to edit a text file in linux?

Most primitive:

cat > file

Type results. End with ^d

Editing files

• Several programs available to edit with:– pico

• basic editing only• Unix version of notepad• Terminal only

– emacs• More advanced editor• GUI and terminal versions• Can be memory intensive

22

Editing Files

• The other alternative:– vi (alternate versions: vim gvim)

• Available on most systems• “strange” to use first time• very powerful editor

– We will briefly cover vi in this course– Get a document on VI from the web page:http://http://ecn.www.ecn.purdue.eduecn.www.ecn.purdue.edu/ECN/Documents/ECN/Documents

/VI//VI/

vi Modes

• vi is a fairly complex program • Lots of command/configuration options. • For the present :

– Open a file, move around, edit the file, and quit.– It is a modal editor- there are different modes of

operation controlling the actions of the editor:• Command mode, in which you execute commands• Edit mode, which allows you to insert text.

23

vi Modes

• When vi is in command mode, you can – move within the document, merge

characters/lines– search, cut, and so on. – You can carry out ALL of the functions of vi

from command mode except enter new text.

– Text can only be entered when in editmode.

vi mode

• When you are in Command mode type:

– i to begin inserting text– a to begin inserting textThese two will switch you to edit mode!

• Done editing? Hit “Esc” key to get to command mode.

24

vi Editing - Example1. Type vi hello.c and press the Enter key. 2. Press the "i" key. You are now in insert mode.

3. Type the following text into the editor.

#include <stdio.h>int main() {

printf("Hello to the Whole World!\n");return(0);

}4. When done, press the ESC key to be in command

mode.

vi Editing - commands

• Saving the file– First get into the command mode (press ESC)– Press “:” to reach the file handling commands – Type 'w‘ – save the changes to the file.– Type ‘wq’ – to save changes and quit the file

• Aborting the file– Type ':q!'.

25

vi Editing - Deleting• Delete text, only in the command mode.

– First get into the command mode (press ESC)

– Go to that text and hit the 'x' key to delete one character at a time.

– To delete a word, position the cursor on the word and press "dw"

– to delete a whole line press "dd”– At any time to undo the delete command

press the "u" key.

vi Editing - Navigating• To navigate through the document.

– You must be in the command mode to do this.

– In this mode, press “j” to move down“k” to move upward“h” to move left “l” to move right.

– For more power, use vi's extensive help

26

vi commands

• x delete a character• dd delete a line• dw delete word• :w write the file• :q quit• :q! quit without saving• :r read existing file