Part 1 Linux Chapter 2 File Systems
Part 1 Linux Chapter 2 File Systems
1
Linux’s File System
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
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
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
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
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
z036473
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
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.
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
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.
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
18
Finding Files
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
• 2 types:
– Hard link
– Symbolic link
39
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
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:
46
23
47
48
24
49
Linking Files
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
57
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
62
31
Creating New Files
63
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
66
33
Editing Text Files
Editing Text
Files
67
Display Text
File contents
68
34
The cat Command
69
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
71
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.
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
75
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”
78
39
The cut Command
79
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
82
41
Example
■ carnet.txt
maurice:29:0298334432:Crozon
marcel:13:0466342233:Marseille
robert:75:0144234452:Paris
yvonne:92:0133444335:Palaiseau
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