chapter 1 unix for nonprogrammers by u ğ ur halıcı

Post on 26-Dec-2015

236 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CHAPTER 1UNIX FOR NONPROGRAMMERS

By Uğur Halıcı

Unix for non programmers

When you connected via terminal to a machine running Unix, you will have a window to enter you commands

$$

system promptsystem prompt

Terminal windowTerminal window

Enter your command hereEnter your command here

2

MANUAL

The man command is used to display the manual entry associated with word entered as argument.

The -k option is used to display a list of manual entries that contain entered keyword.

man [chapter] word

man -k keyword

3

CREATING A FILE

Use editors vi, emacs, pico or the cat command

$ cat >myfile # $ is system prompt$ cat >myfile # $ is system prompt

4

CREATING A FILE

Use editors vi, emacs, pico or the cat command

$ cat >myfile # $ is system prompt$ cat >myfile # $ is system prompt

all characters that follow up # to a new line are comment

all characters that follow up # to a new line are comment comman

dcommand

output redirectionoutput redirection

5

CREATING A FILE

Use editors vi, emacs, pico or the cat command

$ cat >myfile # $ is system promptAliAhmetCan^D$

$ cat >myfile # $ is system promptAliAhmetCan^D$

^D is used to indicate end of input

^D is used to indicate end of input

6

CREATING A FILE

Use editors vi, emacs, pico or the cat command

$ cat >myfile # $ is system promptAliAhmetCan^D$

$ cat >myfile # $ is system promptAliAhmetCan^D$

system prompt appears to enter a new command

system prompt appears to enter a new command

A file with name “myfile” is createdwhose content is :AliAhmetCan

A file with name “myfile” is createdwhose content is :AliAhmetCan

7

CREATING A FILE

Use editors vi, emacs, pico or the cat command

$ cat >myfile # $ is system promptAliAhmetCan^D$

$ cat >myfile # $ is system promptAliAhmetCan^D$

8

home 122home 122

halicihalici

myfilemyfile

Current directoryCurrent directory

AliAhmetCan

AliAhmetCan

LISTING THE CONTENTS OF A DIRECTOTY : ls

 ls –adglR {filename}* {directoryname}*  

note: * means zero or more and + means one or more

Options are:a : list also hidden files, i.e. the filenames

starting with .d : directoriesg : include info about file groupl : long listingR: recursively list the contents of subdirectories 

9

LISTING THE CONTENTS OF A DIRECTOTY : ls

 

$ ls

myfile

 

$ ls –l myfiler w - r - - r - - 1 halici 14 April 15 11:41

myfile

 

$ lsmyfile

 $ ls –l myfile- r w - r - - r - - 1 halici 14 April 15 11:41 myfile

$ lsmyfile

 $ ls –l myfile- r w - r - - r - - 1 halici 14 April 15 11:41 myfile

10

LISTING THE CONTENTS OF A DIRECTOTY : ls

 

$ ls

myfile

 

$ ls –l myfiler w - r - - r - - 1 halici 14 April 15 11:41

myfile

 

$ lsmyfile

 $ ls –l myfile- r w - r - - r - - 1 halici 14 April 15 11:41 myfile

$ lsmyfile

 $ ls –l myfile- r w - r - - r - - 1 halici 14 April 15 11:41 myfile

file type and

permissions

file type and

permissions

# of links # of links

owner owner

date

date

length

length

file name

file name

time time

11

file type and permissions

- r w - r - - r - -

file type

permissions for

owner

permissions for

group

permissions for

others

12

LISTING A FILE: cat/more/page/head/tail

cat : concatanate

more, page : to display in parts without scroll

head : first n lines, for default n=10

tail : last n lines, for default n=10

 $ cat myfileAliAhmetCan$ head -2 myfileAliAhmet$ tail -2 myfileAhmetCan$

$ cat myfileAliAhmetCan$ head -2 myfileAliAhmet$ tail -2 myfileAhmetCan$

13

RENAMING A FILE : mv

mv –i oldFile newFile

mv –i {file name}* directoryName

mv –i oldDirectory newDirectory The mv command in the first form renames oldFile as

newFile. The second form moves collection files to a directory. The last form is used to move the files in oldDirectoty to

newDirectory. The option -i prompts confirmation if newFileName

already exists

14

RENAMING A FILE : mv

$ mv myfile myNewFile$ lsmyNewFile$ cat myNewFileAliAhmetCan$

$ mv myfile myNewFile$ lsmyNewFile$ cat myNewFileAliAhmetCan$

15

home 122home 122

halicihalici

myfile

myfile myNewfilemyNewfile

Current directoryCurrent directory

MAKING A DIRECTORY: mkdir

$ mkdir class$ ls –l-rw-r--r-- 1 halici 14 April 15 11:41 myNewFiledrwxr-xr-x 2 halici 512 April 15 11:50 class/

$ mkdir class$ ls –l-rw-r--r-- 1 halici 14 April 15 11:41 myNewFiledrwxr-xr-x 2 halici 512 April 15 11:50 class/

mkdir newDirectoryName

16

halicihalici

myNewfilemyNewfile classclass

Current directory

Current directory

MAKING A DIRECTORY: mkdir

$ mkdir class$ ls –l-rw-r--r-- 1 halici 14 April 15 11:41 myNewFiledrwxr-xr-x 2 halici 512 April 15 11:50 class/$ mv myNewFile class$ lsclass$ ls classmyNewFile$ ls –Rclassclass: myNewFile

$ mkdir class$ ls –l-rw-r--r-- 1 halici 14 April 15 11:41 myNewFiledrwxr-xr-x 2 halici 512 April 15 11:50 class/$ mv myNewFile class$ lsclass$ ls classmyNewFile$ ls –Rclassclass: myNewFile

mkdir newDirectoryName

17

halicihalici

myNewfilemyNewfile classclass

myNewfilemyNewfile

Current directory

Current directory

MOVING TO A DIRECTORY: cd, chdir

$ pwd # print working directory/home122/halici$ pwd # print working directory/home122/halici

mkdir newDirectoryName

18

halicihalici

classclass

myNewfilemyNewfile

Current directory

Current directory

MOVING TO A DIRECTORY: cd, chdir

$ pwd # print working directory/home122/halici$ cd class$ pwd/home122/halici/class$

$ pwd # print working directory/home122/halici$ cd class$ pwd/home122/halici/class$

mkdir newDirectoryName

19

halicihalici

classclass

myNewfilemyNewfile

Current directory

Current directory

COPYING A FILE : cp

$ cp myNewFile mySecondFile$ lsmyNewFilemySecondFile$

$ cp myNewFile mySecondFile$ lsmyNewFilemySecondFile$

cp –i oldFileName newFileNamecp –ir {file name}* directoryName

options: i: confirmr: recursively copy subdirectories

20

halicihalici

classclass

myNewfilemyNewfile

Current directory

Current directory

mySecondfile

mySecondfile

DELETING A DIRECTORY: rmdir

$ pwd/home122/halici/class$ pwd/home122/halici/class

21

halicihalici

classclass

myNewfilemyNewfile

Current directory

Current directory

mySecondfile

mySecondfile

DELETING A DIRECTORY: rmdir

$ pwd/ home122/halici/class$ cd .. # change to parent directory$ pwd/ home122/halici$ lsclass$ rmdir classrmdir: class: directory not empty$

$ pwd/ home122/halici/class$ cd .. # change to parent directory$ pwd/ home122/halici$ lsclass$ rmdir classrmdir: class: directory not empty$

22

halicihalici

classclass

myNewfilemyNewfile

Current directory

Current directory

mySecondfile

mySecondfile

DELETING A DIRECTORY: rmdir

$ pwd/ home122/halici/class$ cd .. # change to parent directory$ pwd/ home122/halici$ lsclass$ rmdir classrmdir: class: directory not empty$

$ pwd/ home122/halici/class$ cd .. # change to parent directory$ pwd/ home122/halici$ lsclass$ rmdir classrmdir: class: directory not empty$

An error message by the system is displayed. The directory is not deleted since it is not empty

An error message by the system is displayed. The directory is not deleted since it is not empty

23

halicihalici

classclass

myNewfilemyNewfile

Current directory

Current directory

mySecondfile

mySecondfile

DELETING A FILE : rm

$ lsclass$ ls classmyNewFilemySecondFile

$ lsclass$ ls classmyNewFilemySecondFile

rm –fir {filename}* f: inhibit error messagesi: inform each timer: recursivey (if filename is a directory)

24

halicihalici

classclass

myNewfilemyNewfile

Current directory

Current directory

mySecondfile

mySecondfile

DELETING A FILE : rm

$ lsclass$ ls classmyNewFilemySecondFile$rm class/* #remove all files in directory class$ls class

$ lsclass$ ls classmyNewFilemySecondFile$rm class/* #remove all files in directory class$ls class

rm –fir {filename}* f: inhibit error messagesi: inform each timer: recursivey (if filename is a directory)

25

halicihalici

classclass

myNewfilemyNewfile

Current directory

Current directory

mySecondfile

mySecondfile

DELETING A FILE : rm

$ lsclass$ ls classmyNewFilemySecondFile$rm class/* #remove all files in directory class$ls class $

$ lsclass$ ls classmyNewFilemySecondFile$rm class/* #remove all files in directory class$ls class $

rm –fir {filename}* f: inhibit error messagesi: inform each timer: recursivey (if filename is a directory)

All the files under the directory class are deleted, nothing remains to list by ls

All the files under the directory class are deleted, nothing remains to list by ls

26

halicihalici

classclass

Current directory

Current directory

PRINTING A FILE : lpr

$ cat >myclassAliAmetCan^D$ lsmyclass$ cat myclassAliAmetCan$ lpr myclass # send the content of the file class to printer

$ cat >myclassAliAmetCan^D$ lsmyclass$ cat myclassAliAmetCan$ lpr myclass # send the content of the file class to printer

27

COUTING WORDS IN FILE: wc

$ wc –w myclass3$ wc –c myclass14$ wc myclass

$ wc –w myclass3$ wc –c myclass14$ wc myclass

wc -lwc {filename}* options: l: lines, w:words,

28

myclassmyclass

AliAhmetCan

AliAhmetCan

COUTING WORDS IN FILE: wc

$ wc –w myclass3$ wc –c myclass14$ wc myclass

$ wc –w myclass3$ wc –c myclass14$ wc myclass

wc -lwc {filename}* options: l: lines, w:words,

no option is used, this is equivalent to –lwc all togetherno option is used, this is equivalent to –lwc all together

29

myclassmyclass

AliAhmetCan

AliAhmetCan

COUTING WORDS IN FILE: wc

$ wc –w myclass3$ wc –c myclass14$ wc myclass3 3 14 $

$ wc –w myclass3$ wc –c myclass14$ wc myclass3 3 14 $

wc -lwc {filename}* options: l: lines, w:words,

no option is used, this is equivalent to –lwc all togetherno option is used, this is equivalent to –lwc all together

30

myclassmyclass

AliAhmetCan

AliAhmetCan

COUTING WORDS IN FILE: wc

$ wc –w myclass3$ wc –c myclass14$ wc myclass3 3 14 $

$ wc –w myclass3$ wc –c myclass14$ wc myclass3 3 14 $

wc -lwc {filename}* options: l: lines, w:words,

no option is used, this is equivalent to –lwc all togetherno option is used, this is equivalent to –lwc all together

llww

cc

31

myclassmyclass

AliAhmetCan

AliAhmetCan

FILE TYPES

- regular file

d directory file

b buffered special file (such as disk drive)

c unbuffered special file (such as disk terminal)

l symbolic link

p pipe

s socket

32

FILE PERMISSIONS

r w – r - - r - -

user group others

33

FILE PERMISSIONS

regular file directory special file

rread

The process may read the contents

The process can read the directory (i.e. list the names of the files that it contains)

The process may read from the file using the read( ) system call

wwrite

The process may change the contents

The process may add or remove files to/from the directory

The process may write to the file using the write( ) system call

xexecut

e

The process may execute the file (which only makes sense if it is a program)

The process may access files in the directory or any of its subdirectories

No meaning

34

CHANGING FILE’S PERMISSIONS: chmod

chmod –R change{,change}* filename+

 

R: recursively change modes if filename is a directory

 

 

Change:

cluster selection

operation new permission

u (user) + (add) r (read)

g (group) - (remove) w (write)

o (others) = (assign) x (execute)

a (all)

35

CHANGING FILE’S PERMISSIONS: chmod Examples for change{,change}*

g+w add group write permission

u-wx remove user write and execute permissions

o+x add others execute permission

u+w,g-r add write permission for user and remove read permission from

group

g=r give group just read permission

36

CHANGING FILE’S PERMISSIONS: chmod

$ ls –l myclass-rw-r--r-- 1 halici 14 April 15 12:05 myclass$ chmod o-r myclass # remove read permission from others-rw-r----- 1 halici 14 April 15 12:05 myclass

$ ls –l myclass-rw-r--r-- 1 halici 14 April 15 12:05 myclass$ chmod o-r myclass # remove read permission from others-rw-r----- 1 halici 14 April 15 12:05 myclass

37

CHANGING FILE’S PERMISSIONS: chmod

The chmod utility allows you to specify the new permission setting of a file as an octal number

user group others

rwx rwx rwx

setting rwx r-x ---

binary 111 101 000

octal 7 5 0

38

CHANGING FILE’S PERMISSIONS: chmod

$ chmod 750 myclass$ ls –l myclass-rwxr-x--- 1 halici 14 April 15 12:05 myclass$

$ chmod 750 myclass$ ls –l myclass-rwxr-x--- 1 halici 14 April 15 12:05 myclass$

39

CHANGING FILE’S PERMISSIONS: chmod

$ chmod 750 myclass$ ls –l myclass-rwxr-x--- 1 halici 14 April 15 12:05 myclass$

$ chmod 750 myclass$ ls –l myclass-rwxr-x--- 1 halici 14 April 15 12:05 myclass$

Permission is set as desiredPermission is set as desired

40

CHANGING FILE’S PERMISSIONS: chmod

$cat >aaaa^D$ chmod u-w a # remove write permission from user$ ls –l a #see that it is removed-r--r--r-- 1 halici 4 April 15 12:10 a$ rm a #delete the file a$ ls

$cat >aaaa^D$ chmod u-w a # remove write permission from user$ ls –l a #see that it is removed-r--r--r-- 1 halici 4 April 15 12:10 a$ rm a #delete the file a$ ls

41

CHANGING FILE’S PERMISSIONS: chmod

$cat >aaaa^D$ chmod u-w a # remove write permission from user$ ls –l a #see that it is removed-r--r--r-- 1 halici 4 April 15 12:10 a$ rm a #delete the file a$ ls$

$cat >aaaa^D$ chmod u-w a # remove write permission from user$ ls –l a #see that it is removed-r--r--r-- 1 halici 4 April 15 12:10 a$ rm a #delete the file a$ ls$

The file is removed ! Deleting a file depends on not on the file’s write permission but the write permission of the directory that contains it (ie udating the content of the directory)

The file is removed ! Deleting a file depends on not on the file’s write permission but the write permission of the directory that contains it (ie udating the content of the directory)

42

GROUPS

Suppose that I am a member of the group “ee”

$ ls –lg myfile$ ls –lg myfile

43

GROUPS

Suppose that I am a member of the group “ee”

$ ls –lg myfile$ ls –lg myfile

option g stands for listing also file’s groupoption g stands for listing also file’s group

44

GROUPS

Suppose that I am a member of the group “ee”

$ ls –lg myfile-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile$ ls –lg myfile-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile

Group informationGroup information

45

GROUPS

Suppose that I am a member of the group “ee”

$ ls –lg myfile-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile$ groups #list my groupee

$ ls –lg myfile-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile$ groups #list my groupee

46

Group informationGroup information

GROUPS

Suppose that I am a member of the group “ee”

If I want to be added to a new group, say named “cls”, I should request the system administrator to do it.

$ ls –lg myfile-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile$ groups #list my groupee

$ ls –lg myfile-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile$ groups #list my groupee

47

Group informationGroup information

CHANGING FILE’S GROUP : chgrp

chgrp –R groupId {filename}*

R: recursively changes the group of the files in a directory

$ ls –lg myfile-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile$ chgrp cls myfile$ ls –lg myfile-rw-r--r-- 1 halici 14 cls April 15 12:20 myfile

$ ls –lg myfile-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile$ chgrp cls myfile$ ls –lg myfile-rw-r--r-- 1 halici 14 cls April 15 12:20 myfile

48

top related