Professional Documents
Culture Documents
File System PPT (Module 2)
File System PPT (Module 2)
UNIX
FILES
• Files are the building blocks of any
operating system.
user executes a command in UNIX
the UNIX kernel fetches the
corresponding executable file and loads
its instruction text to memory
and creates a process to execute the
command on your behalf.
THE UNIX FILE SYSTEM
hierarchical organization of files
contains directories and files
basic commands to list and manipulate files
independent of physical file system organization
always single tree – hierarchical file system
UNIX FILE SYSTEM LAYOUT
DIRECTORY TERMINOLOGY
Root Directory: /
top-most directory in any UNIX file structure
Home Directory: ~
directory owned by a user
default location when user logs in
Current Directory: .
default location for working with files
Parent Directory: ..
directory immediately above the current directory
FILE TYPES
• A file in a UNIX or POSIX system may be
one of the following types:
– Regular file
– Directory file
-- FIFO file
– Character device file
-- Block device file
REGULAR FILE
– Text file or binary file
i.e both can be executable files (.exe)
with suitable permissions. Ex :pdf, .txt
etc
How to create?
HOW TO delete?
-- rmdir /home/soja/file1
CREATE?
• mkfifo /usr/soja/fifo_pipe
• mknod p
/usr/soja/fifo_pipe
DEVICE
FILES
• Block device files.
ex: floppy disk, hard disk.
• Character device files.
• Ex: line printer, keyboard, modem
etc.
CREAT
E?
Syntax: path c/b major minor
$mknod name device # device #
Ex: $ mknod /home/soja/file1 C 115 20
DEVICE FILES
2
•Major device number
– An index to a kernel table that contains the addresses of
all device driver functions known to the system. Whenever a
process reads data from or writes data to a device file, the
kernel uses the device file’s major number to select and invoke
a device driver function to carry out actual data transfer with a
physical device.
Absolute Pathname
Tracesa path from root to a file or a directory
Always begins with the root (/) directory
Example: /home/soja/unix/assign1.txt
Relative Pathname
Traces a path from the current directory
No initial forward slash (/)
dot (.) refers to current directory
two dots (..) refers to one level up in directory hierarchy
Example: unix/assign1.txt
PATHNAMES FOR FILE3
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
pwd display current dir path
BASIC COMMANDS(2)
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a directory
cat file display a file
more file display a file a page at a time
who list users currently logged in
* match any number of characters
? match one character
man command read the online manual page for a command
BASIC COMMANDS(3)
command > file redirect standard output to a file
command >> file append standard output to a file
command < file redirect standard input from a file
wc file count number of lines/words/characters in file
% wc -w science.txt
PATH TO CURRENT DIRECTORY
“pwd” gives the full pathname of the current working
directory
pwd = print working directory
Example:
$ pwd
/home/soja/3b/unix
THE UNIX AND POSIX FILE ATTRIBUTES
In addition to the above attributes, UNIX systems also store the
major and minor device numbers for each device file.
All the above attributes are assigned by the kernel to a file
when it is created.
The attributes that are constant for any file are:
File type
File inode number
File system ID
Major and minor device number
THE OTHER ATTRIBUTES ARE CHANGED BY
THE FOLLOWING UNIX COMMANDS OR
SYSTEM CALLS
LS: LISTING FILES
26
LONG LIST OPTION
LIST EVERYTHING IN DIRECTORY
List contents of the current . is current
$ ls -la directory in long format dir.
total 126 .. is parent dir.
drwxr-xr-x 13 ray csci 1024 Apr 26 15:49 .
dot (.)
drwxr-xr-x 15 root root 512 Apr 24 15:18 ..
names
-rwx------ 1 ray csci 1120 Apr 12 13:11 .cshrc
are
-rwxr--r-- 1 ray csci 885 Dec 2 13:07 .login
hidden
-rw-r--r-- 1 ray csci 141 Mar 14 13:42 .logout
files
-rwx------ 1 ray csci 436 Apr 12 11:59 .profile
drwx------ 7 ray csci 512 May 17 14:11 330
directories
drwx------ 3 ray csci 512 Mar 19 13:31 467
drwx------ 2 ray csci 512 Mar 31 10:16 Data
-rw-r--r-- 1 ray csci 80 Feb 27 12:23 quiz.txt
plain file
LIST ALL IN A SPECIFIC DIRECTORY
$ ls -l unix/grades
total 10
-rwxr-xr-x 3 ray csci 72 Jan 19 19:12 330assign-graderun
-rwxr-xr-x 1 ray csci 70 Jan 19 19:13 330exam-graderun
-rwxr-xr-x 2 ray csci 70 Jan 19 19:12 330quiz-graderun
-r-x------ 1 ray csci 468 Feb 1 11:55 test-330grade
-r-x------ 1 ray csci 664 Feb 1 11:55 test-330grade,v
PERMISSIONS: TERMINOLOGY
super user
“root” user, also known as system administrator
has user id “0”
has the maximum set of privileges in the system, i.e. no restrictions
apply to what “root” can do
user
Anyone who has account on the system
recognized via a number called “user id”
Group
users are organized into groups
user can belong to multiple groups
Others
30
Those who are neither the owner nor the group
PERMISSIONS: CORE CONCEPTS
user info is stored in file /etc/passwd
userid,
user name, group, home directory, shell
passwords is listed in separate file: /etc/shadow
31
ACCESS PERMISSION TYPES
32
ACCESS PERMISSION TYPES
Access Type Meaning on File Meaning on Dir.
r (read) View file contents List directory contents
(open, read)
33
CATEGORIES OF USERS
3 categories of users want access
34
CHECKING PERMISSIONS
To check the permissions of an existing file or an
existing directory, use the command: ls -l
Example:
$ ls -l unix
total 387
drwxr--r-- 1 z036473 student 862 Feb 7 19:22 grades
-rw-r--r-- 1 z036473 student 0 Jun 24 2007 uv.nawk
-rw-r--r-- 1 z036473 student 0 Jun 24 2007 wx.nawk
-rw-r--r-- 1 z036473 student 0 Jun 24 2007 yz.nawk
35
THE CHMOD COMMAND
36
CHANGING PERMISSIONS:
SYMBOLIC MODE
37
Chmod
1. Relative Mode
2. Absolute Mode
CHANGING PERMISSIONS:
SYMBOLIC MODE
chmod who operation permissions filename
Examples:
$ chmod ug=rwx,o=rx sort.c
$ chmod ugo+rx,go+w sort.c 39
THE CHMOD COMMAND: OCTAL MODE
40
CHANGING PERMISSIONS: OCTAL MODE
Step Perform… Settings
1 List the desired setting rwx|rwx|r-x
2 List octal values for the
corresponding binary 1’s 421|421|401
$ ls -l sort.c
41
-rwxrwxr-x 1 ege csci 80 Feb 27 12:23 sort.c
PRACTICE PROBLEMS – RELATIVE
ASSIGNMENT, ABSOLUTE ASSIGNMENT AND
OCTAL NOTATION
Initial Final Permission Relative Absolute Octal
Permission String
String
rw-r--r-- rw-r--rw-
rwxrwxrwx r--r--r--
rwxrw - - wx ---------
rw-r- - r - - r--------
CHANGING PERMISSIONS: EXAMPLE
Goal: set mode of file “myfile”
Read, write, and execute permissions to owner
Read and execute permissions to group
Execute permission to others
We want: rwx|r-x|--x
Syntax:
mkdir [ -p ] directory-list
MKDIR EXAMPLES
Can create one or more directories at a time
-p = creates intermediate directories if necessary
Examples:
$ mkdir csci330
$ mkdir dirOne dirTwo
$ mkdir /home/turing/ray/unix/demo
(intermediate directories must already exist)
$ mkdir –p /home/turing/ray/unix/demo
(creates intermediate directories if needed)
FILE AND DIRECTORY NAMES
Use the following characters:
Uppercase letters (A-Z)
Lowercase letters (a-z)
Numbers (0-9)
Underscore ( _ )
Period/dot ( . )
FILE AND DIRECTORY NAMES
avoid the following characters:
& * \ | [] {}
$ <> () # ? /
“ ‘ ; ^ ! ~
Space Tab
EXAMPLE: CREATE A DIRECTORY
Data
$ mv unix csci330
$ rm unix/assign/old-assign
LINKING FILES
Allows one file to be known by different names
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:
Hardlink
Symbolic link (a.k.a. “soft link”)
THE LN COMMAND
hard link:
ln shared-file link-name
symbolic link:
ln –s shared-file link-name
LINK ILLUSTRATION
home
create entry “bb” in
z036473 “dir3” as link to file
“aa” in “dir1”
dir1 dir2
aa dir3
bb
HARD LINK EXAMPLE
Contents of dir1
.
1076
home
. 2406
2083
.. 2407
z036473 aa 2407 2408
.
dir1 dir2
.
Contents of dir3
aa dir3
1070
.
bb 2050
..
bb 2407
SYMBOLIC LINK EXAMPLE
Contents of dir1
.
1076
home
. 2598
2083
.. 2599
z036473 aa 2407 2600
.
dir1 dir2
.
Contents of dir3
aa dir3 /
1070
.
3
6 47
03 a
e/z r1/a
m i
ho d
bb 2050
..
bb 2599
HARD LINK VS. SYMBOLIC LINK
Hard Link Symbolic Link
• Does not create a new inode •Create a new inode
• Cannot link directories,
unless this id done by root • Can link directories
• Cannot link files across file
systems • Can link files across file
systems
• Increase the hard link count
of the linked inode • Does not change the hard
link count of the linked inode
FINDING FILES
The command named “find” can be used to locate a file
or a directory