Вы находитесь на странице: 1из 26

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

1
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

2
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 beginners 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

3
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 GUIs allow term, xterms

4
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 elses

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

5
Files and Directories

Root Directory
70user1 Home Directory
dev

/ home 70user4 nussbaum code pointer.c

mail
bin
70user5

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

6
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 Permission
Read Permission
Only allows reading not change

Write Permission
Allows File creation, modified or deletion

Execute Permission
Execute file, open directory

7
Viewing permissions
list command with -l:
ls l <filename(s)>

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

Lets examine these one by one.

Viewing permissions
drwxr-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)

8
File Access Permissions
drwxr-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 Permissions
drwxr
- xr- x 2 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

9
File Permissions
drwxr
- xr- x 1 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 permissions
chmod 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.

10
Changing permissions
chmod a+rx pointer.c
(+ add) ( remove) (= only)
chmod g-x, o-r pointer.c

Permission Symbol
Owner (User) u
Group g
World o
All of above a

Changing Permissions
Also specify by octal - interpreted as
binary
chmod 421 pointer.c
100010001
User : Read ; Cant Write/Execute
Group : Write ; Cant Read /Execute
World : Execute ; Cant Read / Write
chmod 630 pointer.c
110011000
chmod 766 pointer.c
111110110

11
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 directory
For example, if I issue the pwd command
while I am at code directory:
pwd
Ill get on the console
home/70user4/john/code/

12
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)

13
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

14
Deleting Files - Flags
To remove all the subdirectory trees,
you pass the -rf options to the rm
command
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

15
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 /tmp
cp 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

16
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 file1
cat file1 file2

Displays the files to Standard Output

17
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 > outfile
command < infile
command > outfile
command >> outfile

18
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 everything
ls g* lists all files beginning with g
ls go.* lists files named go with any extension
ls * lists all files

19
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

20
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

21
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://ecn.www.ecn.purdue.edu/ECN/Documents
/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.

22
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 edit
mode.

vi mode
When you are in Command mode
type:
i to begin inserting text
a to begin inserting text
These two will switch you to edit mode!

Done editing? Hit Esc key to get to


command mode.

23
vi Editing - Example
1. 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!'.

24
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

25
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

26

Вам также может понравиться