0% found this document useful (0 votes)
6 views

Part 1 Linux Chapter 2 File Systems

hust

Uploaded by

Phuc Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Part 1 Linux Chapter 2 File Systems

hust

Uploaded by

Phuc Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

References

• Based on slides of Department of Computer


Science of NIU
http://www.cs.niu.edu

1
Linux’s File System

1. File and Directory


• In linux and most of OS data are stored in files
• File
– Contains data
– Stored in (hard) disk
• Directory
– Contains files
– Stored in (hard) disk
– Makes easy for data organizing
• Hierarchy of directories and files = file system
• Single file system for all logical disks
4

2
A Directory
Hierarchy

Directory Types
• Root Directory: /
– The first directory in any UNIX file structure
– Always begin with the forward slash (/)
• Home Directory: $HOME or ~
• Created by system administrator
• This is where you are when you first log in!
• Under $HOME, you may create your own directory structure
• Type: cd [Return] takes you $HOME
• Current Working Directory: .
– The Directory you are currently working in
– Also called Current Working Directory (cwd)
• Parent Directory: ..
– The directory immediately above your current working directory.
6

3
2. Path

Two ways of locating a file or a directory:


• By Using Absolute Path
– Full path
– Traces a path from root to a file or a directory
– Always begins with the root (/) directory!
– Example: /home/ux/krush/unix/assignments/assign1.sp04
• By Using Relative Path
– Traces a path from the ‘cwd’ to a file or a directory
– No initial forward slash (/)
– Two dots (..) goes up one level on file structure
– Dot (.) points to current working directory (cwd)
– Example: unix/assignments/assign1.sp04

Absolute Path for file3

Absolute Pathname:

/usr/staff/joan/file3

4
Relative Paths for file3

3. Directory Operations

10

5
Display Current Directory’s Full Pathname
• To determine the full pathname of the current
working directory, use the command named
“pwd”
• pwd stands for print working directory
Example: To display the full pathname of the current
working directory
ux% pwd
/home/ux/krush/unix

11

The ls Command

12

6
The ls Command
• ls
List the content of the current directory
• ls path_name
List the content of the directory in path_name.
• ls –l
Long list
• ls –a
List all hidden files
• ls -la
Combine two options –l and –a together
13

Long List Option

14

7
List Contents of a Specific Directory
Listing contents of a subdirectory named
ux% ls -l unix/grades “unix/grades”
total 10
-rwxr-xr-x 3 krush csci 72 Jan 19 19:12 330assign-graderun
-rwxr-xr-x 1 krush csci 70 Jan 19 19:13 330exam-graderun
-rwxr-xr-x 2 krush csci 70 Jan 19 19:12 330quiz-graderun
-r-x------ 1 krush csci 468 Feb 1 11:55 test-330grade
-r-x------ 1 krush csci 664 Feb 1 11:55 test-330grade,v

15

File Name Expansion & Wildcards


Allows you to select files that satisfy a particular
name pattern (wildcards)
Character Description Example

* Match zero or more char. ls *.c

? Match any single character ls conf.?

[list] Match any single character in list ls conf.[co]

[lower-upper] Match any character in range ls lib-id[3-7].o

str{str1,str2,…} Expand str with contents of { } ls c*.{700,300}

16

8
The mkdir
Command

17

Directory Names
• Use the following characters:
– Uppercase letters (A-Z)
– Lowercase letters (a-z)
– Numbers (0-9)
– Underscore ( _ )
– Period/dot ( . )

18

9
Directory Names
• When naming a directory, avoid the following
characters:

& * \ | [] {}

$ <> () # ? /

“ ‘ ; ^ ! ~

Space Tab

19

Example: Create a Directory Creation


et
dev home usr
c

tty null skel mp ux bin local ucb

z036473
You are here
csci330 .cshrc .logout Temp

Data
Create a directory called Data under csci330
a) Using Absolute Pathname:mkdir /home/mp/z036473/csci330/Data
b) Using Relative Pathname: mkdir csci330/Data
c) Make also missing parent directory, directory Data does not exist.
mkdir -p csci330/Data/subData

20

10
The cd Command

21

Changing Directory

dev etc home usr

tty null skel mp ux bin local ucb

z036473

csci330 .cshrc .logout Temp

You are here Data

In the Data directory, go to $HOME directory


a) Using Absolute Pathname:
cd /home/mp/z036473
a) Using Relative Pathname:
cd $home cd ../.. cd cd ~ cd ~z036473
22

11
Remove Directories
• To remove an empty directory – a directory that
does not contain user-created files, use the
command named “rmdir”
Example: To remove a directory called “test”,
which does not contain user-created files.
ux% rmdir test
• To remove a non-empty directory, use the command
named “rm –r”
Example: To remove a non-empty directory called
“old-data”
ux% rm –r old-data

23

4. Operations Common to Directories


and Regular Files

24

12
Copying Files
• To copy a file, use the command named “cp”
• Syntax: cp source-file new-file
• Commonly used options:
-i if “new-file” exists, the command cp prompts for
confirmation before overwriting
-p preserve permissions and modification times
-r recursively copy files and subdirectories

25

Copying Files
• “source-file” must have read permission.
• The directory that contains “source-file” must have
execute permission.
• The directory that contains “new-file” must have
write and execute permissions.
• Note that if “new-file” exists, you do not need the
write permission to the directory that contains it,
but you must have the write permission to “new-
file”.

26

13
Moving Files
• To move files from one directory to another
directory, or to re-name a file, use the command
named “mv”.
• The directory that contains the source file and the
destination directory must have write and execute
access permissions.

27

Moving Files
• Syntax: mv source-file destination-file
• If the destination file exists, “mv” will not
overwrite exiting file.

Example: Move “assign1.txt” a different directory


and rename it to “assign1.save”
ux% mv assign1.txt ~/archive/assign1.save
ux% mv assign1.txt ~/archive

28

14
Moving a File

29

Rename Directories
• To change the name of an existing directory, use
the command named “mv”
Example: To rename the file called “unix” to
“csci330”
ux% mv unix csci330

• For the above example, what happens if “csci330”


already exists in the current directory and it is the
name of a directory?

30

15
The mv Command

31

Removing/Deleting Files
• You should remove un-needed files to free up disk
space.
• To remove/delete files, use the command named
“rm”.
• Syntax: rm file-list
• Commonly used options:
-f force remove regardless of permissions for “file-
list”
-i prompt for confirmation before removing
-r removes everything under the indicated directory

32

16
Removing/Deleting Files
• If “file-list” contains pathname, the directory
components of the pathname must have execute
permission.
• The last directory that contains the file to be
deleted must have execute and write permissions.

Example: Remove the file named “old-assign”


ux% rm unix/assign/old-assign

33

Examples
$ ls -l
-rw-r--r-- 1 tuananh user1 16 Feb 10 19:12 test.txt
drwxr-xr-- 2 tuananh user1 512 Feb 10 19:14 mydir
$ cp test.txt mydir
$ ls -l mydir
-rw-r--r-- 1 tuananh user1 16 Feb 12 20:03 test.txt
$ rm –R mydir
$ ls -l
-rw-r--r-- 1 tuananh user1 16 Feb 10 19:12 test.txt
$ rm test.txt
$ ls -l
$

34

17
Recap: Common Operations on Files

35

Finding Files

• The command named “find” can be used to locate


a file or a directory.
• Syntax: find pathname-list expression
• “find” recursively descends through pathname-list
and applies expression to every file.
• Expression
• -name file_name
• -perm permission_mod
• -type d/f/...
• -size N: N is the minimum number of block (512B)
• -atime N, -mtime N, -ctime N, where is by defaut the number of
day.
36

18
Finding Files

Example 1: Find all files, in your directory hierarchy,


that have a name ending with “.bak”.
ux% find ~ “*.bak” –print

Example 2: Find all files, in your directory hierarchy,


that were modified yesterday.
ux% find ~ –mtime –1 -print

37

Finding Files
■ $find /usr -name toto
◻ Find a file named toto in folder /usr (including all sub-
folders of /usr)
■ $find /usr -name "*.c"
◻ List all files terminated by ".c "
■ $find / -mtime 3
◻ List all files that is changed in the last 3 days
■ $find / -size 2000
◻ Find all files more than 1 GB (= 2000 block 512 KB)
■ $find / -type f -user olivier -perm 755
◻ Find all files owned by Olivier, and their access right
is equal to 755

38

19
The “ln” command

• Allows file to listed in multiple directories

• 2 types:
– Hard link
– Symbolic link

• First: understand Unix file storage and organization

39

5. Unix file organization

• Computer has one or more physical hard drives


• Hard drive is divided into partitions

• Partition holds file system


– File system is set of data blocks
– Data blocks contain
• general information
• actual file data
• directory information

40

20
Blocks in a file system

41

inode
• Index (or information) node: one inode per file
• Each inode has unique number
• contents:
– File type, access permissions, link count
– UID, GID
– Date and time of the file’s last
• Data access (read and execute)
• Data modification (written)
• I-node modification (permission change)
– Data blocks assigned to the file

42

21
Inodes in a
filesystem

43

inode Contents: where is the file data ?


Inode may store:
– 10 addresses of data blocks that belong to file
– 1 address of a block that contains data block addresses
– 1 address of a block that contains addresses of blocks
that contain data block addresses
– 1 address of a block that contains addresses of blocks
that contain addresses of blocks that contain data
block addresses

44

22
Inode
structure

45

Directory representation
Directory is a file:
– Has inode like regular file, but different file type
– Data blocks of directory contains simple table:

Name Inode number

46

23
47

Example: user view vs. system view

48

24
49

Linking Files

• To share a single file with multiple users, a link can


be used.
• A link is:
– A reference to a file stored elsewhere on the system.
– A way to establish a connection to a file to be shared.
• Two types:
– Hard link
– Symbolic link (a.k.a. “soft link”)

50

25
Hard Link

Advantages Disadvantages
Allow access to original file name
via the file name or the I-node Cannot link to a file in a different
number file system
The original file continues to exist
as long as at least one directory
contains its I-node Prevents owner from truly deleting
it, and it counts against his/her disk
Checks for the existence of the quota
original file

51

Hard Link

52

26
The ln Command

53

A Hard Link

54

27
A Hard Link

55

Symbolic Link

Advantages Disadvantages
Allow access to original file name Created without checking the
existence of the shared file
Can use either relative or absolute
path to access the original file Cannot access the shared file if its
path has restricted permissions
Can cross partition and drives
Can be circular linked to another
Allows the creation of a link to a symbolic linked file
directory

56

28
Symbolic Link
• A hard link may not be created for a file on a
different file system
• Use symbolic link
• The linked files do not share the same I-node
number

Syntax: ln –s shared-file link-name

Also called source-file Also called target-file

57

Symbolic Links to Different File Systems

58

29
Examples
$ ls -l
-rw-r--r-- 1 tuananh user1 8 Feb 10 1:12 test.txt
$ ln test.txt link1
$ ln -s test.txt link2
$ ls -l link*
-rw-r--r-- 2 tuananh user1 16 Feb 10 1:12 link1
lrw-r--r-- 1 tuananh user1 16 Feb 10 1:13 link2-
>test.txt

59

Exercise 3
• In your home directory (usually
/home/students/2017xxxx) create directory dir3 then
dir4 inside dir3
• Create file file1 inside dir4
• Create file file2 inside dir3 linked hardly to file1 (*)
• Create file file3 inside dir3 linked symbolically to file1
(**)
• View the content of file2 and file3
• What happens if file1 is deleted?
• Redone steps (*) and (**) with relative paths
60

30
6. Operations Unique to Regular Files

Display
Create Edit Print Others
Contents

61

User’s Disk Quota

• A disk quota is set for each user account


• The command: quota –v
displays the user’s disk usage and limits
• 2 kinds of limits:
– Soft limit: ex. 3MB
• Maybe exceeded for one week
• System will nag
– Hard limit: ex. 4MB
• Cannot be exceeded

62

31
Creating New Files

See Text Editors See the C shell


Section 03 Section 06

63

Creating A File With


cat

Example:
mp% cat > myfile
This is line 1 of input
Line 2 of input
^d
mp%
mp% cat myfile
This is line 1 of input
Line 2 of input

64

32
Create a File with Touch Command
• touch [OPTION]… FILE…
Create a new empty file (with 0 length) if the file
does not exists
Otherwise, update it’s last modification and access
times
Example: %un touch abc.txt
• touch –a [filename]
Only change the access time of the file
• touch –m [filename]
Only change the modification time of the file
65

Create a File with Echo Command


• echo <string> > filename
Duplicate whatever you specify in the command and
put the copy into a file

Example: %un echo ’hello world’ > abc2.txt


Verify:
%un ls
%un cat abc2.txt

66

33
Editing Text Files

Editing Text
Files

vi nano sed awk

67

Displaying Contents of Text Files

Display Text
File contents

cat more less head tail

68

34
The cat Command

69

Viewing Contents of Text Files


• The command named “cat” can be used to
display/concatenate one or more files, displaying
the output all at once.

Example: Display the contents of a file called


“assign1.txt”.
ux% cat assign1.txt

70

35
Viewing Contents of Text Files
• The commands named “more” and “less” can be
used to display the contents of one or more files
one page at a time.
Space bar – to advance to next page
b – to go back a page
Enter Key – to advance to next line

Example: Display the contents of a file called


“assign1.txt” one page at a time.
ux% more assign1.txt

71

Viewing Contents of Text Files


• The command named “head” can be used to
display the beginning portion of indicated file(s);
the default head size is 10 lines.

Example: Display the first 20 lines of a file called


“assign1.txt”.
ux% head –20 assign1.txt

72

36
Viewing Contents of Text Files
• The command named “tail” can be used to display
the ending portion of indicated file(s); the default
tail size is 10 lines.

Example: Display the last 10 lines of a file called


“assign1.txt”.
ux% tail assign1.txt
ux% tail –10 assign1.txt

73

Exercise 4
• Create a new file using nano. Let’s make it more
than 30 lines
• Use cat to show the content of the file
• Use head to show the first 7 lines
• Use tail to show the last 7 lines.

74

37
Other Operations Unique to Text Files

Other File
Operations

Combi Extrac Comp


Compre
ne t are File
ss sort
conten conten conten size
contents
ts ts ts
Encry
Uniqu
pt/
e
decryp
lines
t

75

grep : Seeking lines in files


$grep [-options] expreg [files]
◻ Seeking in files the lines containing strings satisfying
certain conditions. The conditions are explained in a
regular expression.
■ options:
◻ -c : count the number of lines satifsfying the condition
◻ -I : show the file satisfying the condition
◻ -i : case insensitive
◻ -n : return the line numbers

76

38
Grep: expression
■ Following characters can be used in the
expression:
. For replacing any character
* Repeat the previous character
^ starting of a line
$ ending of a line
[...] a list or a range of characters
[^..] a list or a range of excluding characters
■ Attention: The expression should be between “”
77

Examples
■ $grep “^t" /etc/passwd
◻ Find in the /etc/passwd all lines starting with “t”
■ $grep [^t] /etc/passwd
◻ Find all lines which do not start with “t”

■ $grep “tuananh" /etc/passwd


◻ Find all lines containing “tuananh”

78

39
The cut Command

79

Fields Within A Line

80

40
cut : Extract fields from files
$cut -options [files]
■ options
◻ -c<character position> extract by character positions.
◻ -f<field number> extract by fields
◻ -d<delim symbol >
■ Example
◻ $cut -c5 file #extract the 5th characters of all lines
◻ $cut -c5-10 file #extract from the characters 5th to 10th
◻ $cut -d: -f1 /etc/passwd #show all the users in the
system
81

sort: sorting the content of a file


■ $sort -options file_name
■ option
◻ -b: ignore all spaces at the beginning of all fields
◻ -d : sort according on dictionary order (characters, digits, space)
◻ -r : reverse the order
◻ -f : case insensitive
◻ -t x : deliminator is the character x
◻ -n sort according to number order.
◻ -k x: specify the field to be used as sort key.

82

41
Example
■ carnet.txt
maurice:29:0298334432:Crozon
marcel:13:0466342233:Marseille
robert:75:0144234452:Paris
yvonne:92:0133444335:Palaiseau

■ $sort -n -t : -k2 carnet.txt


◻ Sort with key field 2.

83

The wc
Command

84

42
Exercise 5
• Create a file named dulieu.txt according to the following
structure:
• Name* Phone Number* StudentID
• File content as follows:
• -- begin file --
• Tran Viet Anh* 0912345678* 20090158
• Giang Tuan Anh* 0912345679* 20090077
• Bui Hung Cuong* 0112345678* 20090388
• Nguyen Huy Duc* 0112445678* 20090798
• Vu Binh Duong* 0914445678* 20090621
• Pham Đinh Le Hao* 38223344* 20090991
• -- end file –

85

Exercise 5 (cont.)
• Create a command in order to extract all names in the
file
• Create a command in order to extract all names and
corresponding phone number in the file
• Create a command in order to sort the file contents by
name in dictionary order
• Create a command in order to extract line containing a
student named X
• Create a command in order to extract ID of a student
named X
86

43
Thank you
for your
attentions!

87

44

You might also like