Sei sulla pagina 1di 22

Lab 1:

Working with Linux

1
Login and logout

 Account
 username & password
 Note:
 Linux is case-sensitive
 Administrator: username = root

 Logout: exit, Ctrl+D

2
Linux File System
(C) (D) (/)

WINDOWS Program Files boot home etc root


Data Music

Fonts System32 …. …. khoa student1


OS ….

…. ….

Linux File System


Windows File System

3
Directory/file commands
 List contents of directory :
ls [-a] [-l] [directory_name]
 Print working directory: pwd
 Change working directory :
cd directory_name
E.g. cd /home
 Create new directory :
mkdir directory_name
 Remove a directory:
rm -r directory_name
 Some special symbols :
~ : home directory
.. : parent directory

4
Directory/file commands

 Display file content :


cat filename or more filename
head filename or tail filename
 Copy file(s) or directory:
cp [-r] source_file destination_file
 Remove file or directory
rm –r file_name
 Move (rename) file(s)/directory
mv old_path new_path

5
File system and permissions

 Each user may owns one or more directories/files


 Each user has different access rights in different
directories/files
 users can share their data together
 users also can protect their private data

6
File system and permissions

 Access right on directory/file


 read (r)
 write (w)
 execute (x)

 Each directory/file has 9 access-right bits, divide into 3 groups as


follow :
 owner

 group (e.g. people the same project team)

 others (people in public domain)

7
File system and permissions

8
Changing access rights (1)

 Symbolic
chmod who op mode [-R] file(s)
 Who: u : owner
g : group
o : others
a : all
 Mode: r : read
w : write
x : execute
 Op + : grant more rights
- : revoke rights
= : reset rights
9
Changing access rights (2)

 Example
$ touch temp
$ ls –l temp
-rw-r--r-- 1 user1 staff 0 Jun 11 11:44 temp
$ chmod o-r temp
$ ls -l temp
-rw-r----- 1 user1 staff 0 Jun 11 11:44 temp
$ chmod u+x, o+r temp
$ ls -l temp
-rwxr--r-- 1 user1 staff 0 Jun 11 11:44 temp

10
Changing access rights (3)

 Numeric: chmod xyz [-R] file(s)


read = 4 write = 2 execute = 1
Octal value Access right
7 rwx
6 rw-
5 r-x
4 r--
3 -wx
2 -w-
1 --x
0 ---
11
Changing access rights (4)

 Example: some common access rights of


directory/file(s)
Octal value Access right
600 rw-------
644 rw-r--r--
700 rwx------
751 rwxr-x--x
775 rwxrwxr-x
777 rwxrwxrwx

12
Changing access rights (5)

 Example
$ touch abc
$ ls –l abc
-rw-r--r-- 1 user1 staff 0 Jun 11 11:44 abc
$ chmod 555 abc
$ ls -l abc
-r-xr-xr-x 1 user1 staff 0 Jun 11 11:44 abc
$ chmod 775 abc
$ ls -l abc
-rwxrwxr-x 1 user1 staff 0 Jun 11 11:44 abc

13
Advanced utilities (1)

 Who is who?
who [option]
 Print current host name
hostname
 Where do they come from?
which [filename]
 How much disk usage?
df [option]
 Clear screen
clear OR Ctrl + L

14
Advanced utilities (2)

 Find a specified file :


find path –name filename

 Find lines in file matching a pattern


grep pattern file_name

 Mount and unmount file system


mount -t filesystem device_file mount_point
umount mount_point

15
vi editor

 Interactive simple editor

 Can not use mouse

 Text editing on a buffer

 Appears on most Unix or Unix-like system

16
vi usage

syntax meaning
vi filename open/create file

a or i change to edit mode

ESC → wq! save and quit

ESC → q! not save and quit

17
Cursor movement in vi
Cmd Meaning
n move cursor left n character(s)
n move cursor down n character(s)
n move cursor up n character(s)
n move cursor right n character(s)
Enter move cursor to beginning of next line
G move cursor to the last line
nw move cursor left n word(s)
nW move cursor right n word(s)

18
Text manipulation commands
Cmd Meaning
nx delete n character(s) from current cursor
position
nX delete n character(s) immediately preceding
current cursor position
D, d$ delete all characters from current cursor
position to end of line
d0, d| delete all characters from left collumn of
screen to character preceding current cursor
position on current line

19
Text manipulation commands
Cmd Meaning
ndd delete n line(s) beginning at current line
dG delete all lines, starting with current line, through
end of file

d1G delete all lines, starting with current line, through


beginning of file
ndw delete from cursor position through end of n
following word
ndb delete from nearest preceding word through
character before current cursor position
J join the next line at the end of current line

20
Text manipulation commands
Cmd Meaning
~ change case of current character
r replace single character at cursor position

ncc change n line(s) beginning at current line


cG change all lines from current line through end of file

ncw change n word(s) on the left of cursor


ncb change n word(s) on the right of cursor

21
Text manipulation commands
Cmd Meaning
nY, nyy copy (yank) n line(s) into buffer
nyw copy (yank) n word(s) into buffer

y$ copy (yank) from current cursor position through


end of line into buffer
yG copy (yank) from current line through end of file
into buffer
p Insert buffer content at the line after current line
P Insert buffer content at the line above current line

22

Potrebbero piacerti anche