Linux File System Notes
Linux File System Notes
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
Figure 3-3
A Directory Hierarchy
3
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.
4
Path
Absolute Pathname:
/usr/staff/joan/file3
6
File System
Directory Operations
8
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
9
Figure 3-14
The ls Command
10
ls
• 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
11
Figure 3-15
12
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
13
File Name Expansion & Wildcards
Allows you to select files that satisfy a particular
name pattern (wildcards)
Character Description Example
14
Figure 3-17
15
Directory Names
• Use the following characters:
– Uppercase letters (A-Z)
– Lowercase letters (a-z)
– Numbers (0-9)
– Underscore ( _ )
– Period/dot ( . )
16
Directory Names
• When naming a directory, avoid the following
characters:
& * \ | [] {}
$ <> () # ? /
“ ‘ ; ^ ! ~
Space Tab
17
Example: Create a Directory Creation
Data
mkdir -p csci330/Data/subData
Figure 3-18
The cd Command
19
Changing Directory
Exercises
• In your home directory (usually /home/students/2017xxxx)
create directory folder1 then folder2 inside folder1
• List the contents of folder1 then of folder2
• Move to folder2
• Print the current working directory
• Get back to your home directory
• Print the current working directory
• Remove folder1 and folder2
• list the content of your home directory again.
Figure 3-22 Operations Common to Directories and
Regular Files
23
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
24
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”.
25
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.
26
Moving Files
• Syntax: mv source-file destination-file
• If the destination file exists, “mv” will not
overwrite exiting file.
27
Figure 3-31
Moving a File
28
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
29
Figure 3-30
The mv Command
30
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
31
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.
32
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
$
Recap: Common Operations on Files
34
Finding Files
36
Example
• $find /usr -name toto
• $find / -mtime 3
Exercise
• Write command for:
• Search for the files with name test in your personal folder
• Search for the files with name .c in the whole disk
• Search for the files with name ending with .conf in /etc
• Search for the files with size greater than 100 MB in disk
• Search for the files with name .lib and size greater than 5MB in the
whole disk
The “ln” command
• 2 types:
– Hard link
– Symbolic link
39
Unix file organization
40
Figure 3-5
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
Figure 3-6
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
File System
Inode structure
I-node Structure
blocks
triple indirect 42
block
Directory representation
Directory is a file:
– Has inode like regular file, but different file type
– Data blocks of directory contains simple table:
46
File System
Example structure
.
I-node
list 2763
Contents of dir1
2764
. 1076
2765
.. 2083 .
myfile 2764 .
Data blocks
on disk
44
Example: user view vs. system view
48
File System
Output: ls -li
I-node
46
Linking Files
50
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
Hard Link
Link
z036473
From dir3, link to the file ‘aa’ in dir1
name it ‘bb’:
dir1 dir2
% ln /home/z036473/dir1/aa bb
aa dir3
bb
54
52
Figure 3-32
The ln Command
53
Figure 3-8
A Hard Link
54
Figure 3-8
A Hard
Hard Link
Link
Contents of dir1
.
home
. 1076
2406
z036473 .. 2083 2407
2408
aa 2407
dir1 dir2 .
aa Contents of dir3 .
dir3
. 1070
bb
.. 2050
bb 2407
57
55
File System
Exercise
• Use nano or gedit for creating a file in your personal
folder, name it file1
• Make a hardlink to file1, name the new file file2
• Show the content of file2 on screen by using
• $ cat file2
• Modify the content of file2 by using nano
• Check the content of file1 with cat
• Delete file1
• Check the content of file2.
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
symbolic linked file
Allows the creation of a link to a
directory
57
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
• link-path: relative path to the shared-file
58
Figure 3-10
59
Examples
$ echo « Test content » > test.txt
$ 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
User’s Disk Quota
61
File System
Exercise
• Use nano or gedit for creating a file in your personal
folder, name it file1
• Make a symbolic link to file1, name the new file file3
• Show the content of file3 on screen by using
• $ cat file2
• Modify the content of file3 by using nano
• Check the content of file1 with cat
• Move file1 to another folder
• Check the content of file3.
• Move file1 back
• Check the content of file3