Download - The UNIX File System
![Page 1: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/1.jpg)
The UNIX File System
![Page 2: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/2.jpg)
The UNIX File• A file is a container for storing information and data.
• Filename limited to 255 characters. Can’t contain / or NULL character. Avoid $, ‘, ?, -, “ in the filename. Extensions are optional.
• Filenames are case-sensitive; chap and Chap are two different filenames because c and C have different ASCII codes.
• Group of filenames held together in a directory.
• Directory contains the name of the file (name and size are not stored in the file itself).
• Both files and directories are subject to access control.
![Page 3: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/3.jpg)
File Types
• Ordinary or regular file: Contains data. This file can be a• text file (program sources, configuration files).• binary file (executables, graphic and multimedia files).
• Directory: Contains the filename and a number (inode number).
• Device file: Contains no data whatsoever.
• Symbolic link: Contains the location of another file.
![Page 4: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/4.jpg)
/
bin dev home lib tmp etcsbin usr var
dsk fd0 pr1
austin julie
bin lib sbin local
progs login.sql .profile
root
Typical File Hierarchy in UNIX
![Page 5: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/5.jpg)
The Hierarchical Structure of the File System
• A single hierarchical structure that contains all files.
• Top signified by root (/).
• Existence of a parent-child relationship.
• Parent of any file must be a directory.
• Files accessed with pathnames (e.g., /etc/passwd).
![Page 6: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/6.jpg)
What Goes Where (Usually)
• / the root (comparable in Windows to c:\)
• / bin commonly used binaries (programs)
• /sbin superuser programs• /usr user-related commands
(and random stuff)• /usr/include header files for C
programs• /usr/share/man manual pages
• /lib libraries of reused files (for programmers)
• /dev device files• /home traditionally, where
user accounts are stored• /etc startup and
configuration files• /var traditionally, where
mailboxes are stored• /tmp temporary files
![Page 7: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/7.jpg)
Some Important File-Related Commands in UNIX
• cd – change directory• mkdir – make a directory• rmdir – delete a directory• ls – list files in a directory• pwd – print the working
directory• dos2unix (unix2dos)
– convert between dos & unix
• chown (chgrp) – change ownership of file
• cp – copy a file• mv – move (rename) a file• rm – delete a file• cat – display a file• more (less) – page a file• lp (lpr) – print a file• od – octal dump a file• tar (gzip. zip) – compress
and/or archive files• find – locate files• chmod – change file permissions
![Page 8: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/8.jpg)
Navigating the UNIX Directory Structure
• You are always located at one location in the tree• cd changes your current directory
– cd / moves you to the root directory– cd without any arguments moves you to your home directory
• Special notation for navigating directories:– . means the current directory e.g. cd ./progs– .. means the parent directory of the current directory e.g. cd ../brother/progs – ~ means your home directory e.g., cd ~/myprogs
• You can “stack” this notation e.g., cd ../..
![Page 9: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/9.jpg)
Two Types of Pathnames
• Absolute Pathname: Specifies location with reference to the file system root e.g., cat /etc/passwd
• good for configuration files and command not in your PATH
• Relative Pathname: Specifies location with reference to the user’s current location e.g., cd ../include
• good for files that are impossible/ inconvenient to access in an absolute manner
• Both commands and filename arguments can be represented in either form.
![Page 10: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/10.jpg)
The Home Directory• Directory where user is placed on login.
• Determined by sixth field in /etc/passwd file: romeo:x:500:100:romeo vincent:/home/romeo:/bin/bash
• Can also be referred to by• the environment variable $HOME e.g., cat $HOME/foo• tilde expansion in most shells: ~ e.g., cat ~/foo
• cd command used without arguments returns user to home directory.
• user can create and remove files in their home directory but not in other directories.
![Page 11: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/11.jpg)
Inodes (Index Nodes)
• System of organizing file attributes separately from file content.
• Lets files have multiple names.
• Files and directories are identified by a unique inode number.
• Inode number displayed by ls -i.
• Possible to consume all inodes even when there is adequate disk space.
![Page 12: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/12.jpg)
File Attributes Stored in Inode• Type: Whether ordinary, directory, device, etc.
• Permissions: Determines who can read, write or execute a file.
• Links: Number of names a file can have. A program can be designed to behave differently depending on the name by which it is invoked.
• Owner: A file is owned by a user, by default, its creator. The owner can change many file attributes and set the permissions.
• Group Owner: The group which owns the file. Owner belongs to this group.
• File Size: Number of bytes of data contained.
• File Time Stamps:• Date and time of last modification• Date and time of last access
![Page 13: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/13.jpg)
Users and File Permission Types
• A file has three types of permissions: read, write and execute.• read: file can be read, but not modified• write: file may also be modified• execute: file may be run (as in a program file)
• Available to three categories of users: user, group and the world.• user: the creator of the file (also called the owner)• group: a set of users grouped together• world: everyone else not in group
• Only file owner or superuser (su) can change file permissions.
![Page 14: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/14.jpg)
Look at the Output of ls -l
-rwxrwxrwx
- rwx rwx rwx
File Type Owner Group World
- = ordinary filed = directory
ls –l-rw-rw-r-- 1 bournique instructors 143 Sep 1 08:45 grades
permissions links owner group owner size last mod filename
![Page 15: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/15.jpg)
What Permissions Mean on a Directory
• Read: User can get a listing of that directory using, e.g., ls
• Write: Users can create/ remove files in the directory.
• Execute (or search permission): Users can pass through the directory to search for filenames.
• Desirable permission setting for directories: 755 or rwxr-xr-x
![Page 16: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/16.jpg)
How a Directory Influences File Permissions
Examining only the user category
File Directory Significance
r--r--r-- rwxr-xr-x A write-protected file; can’t be modified but can be removed.
rw-r--r-- r-xr-xr-x A write-protected directory; file can’t be removed but can be modified.
r--r--r-- r-xr-xr-x A write-protected file and directory; file can’t be modified or removed.
rw-r--r-- rwxr-xr-x Normal setting; file can be modified and removed.
rw-r--r-- rw-r-xr-x File can’t be removed even though directory is writable. (An unusual setting)
How a Directory Influences File Permissions
Examining only the user category
File Directory Significance
r--r--r-- rwxr-xr-x A write-protected file; can’t be modified but can be removed.
rw-r--r-- r-xr-xr-x A write-protected directory; file can’t be removed but can be modified.
r--r--r-- r-xr-xr-x A write-protected file and directory; file can’t be modified or removed.
rw-r--r-- rwxr-xr-x Normal setting; file can be modified and removed.
rw-r--r-- rw-r-xr-x File can’t be removed even though directory is writable. (An unusual setting)
![Page 17: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/17.jpg)
Changing Permissions With chmod
General Form: chmod <SETTINGS> <FILE>
Example: chmod a-x testFile (relative change)
u = user
g = group
o = other
a = all
+ (add)
- (remove)
= (set)
r = read
w = write
x = execute
![Page 18: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/18.jpg)
More chmod Examples
• Set read and write access for all chmod a=rw <FILE>• Add executable access for others chmod o+x <FILE>• Remove all access for owner chmod u-rwx <FILE>• Set read, write & execute access for all chmod a=rwx <FILE>• 777 permissions (absolute permission change)
o Octal Representation of -rwxrwxrwxo Three binary digits or bits corresponds to one octal digit:
Read=4, Write=2 Execute=1 i.e., rwx=4+2+1 = 7
chmod 750 <file> same as chmod u=rwx,g=rx,o= <file>
![Page 19: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/19.jpg)
The User Mask Command, umask
• Reassigns default file and directory permissions.
• Default permissions before applying mask are completely insecure: 666 for files and 777 for directories
• System wide default changed by umask (a shell builtin).
• umask statement placed in a startup script (typically, the file /etc/profile).
• 022 is a good umask value which mean 644 for files and 755 for directories .
![Page 20: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/20.jpg)
Links (also Called Hard Links)
• Mechanism by which a file is allowed to have multiple names.
• Linked filenames share inode but have separate directory entries.
• Each link increments link count in inode by 1 and adds an entry to the directory.
• File considered to be deleted and inode freed only when link count drops to 0.
• Linked filenames equivalent in all respects.
![Page 21: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/21.jpg)
Advantages/ Disadvantages of Hard Links
• Advantages• Backup: Prevention from accidental deletion.
• Allows the same file to be executed as two similar but separate programs.
•Disadvantages• Can’t link directories
•Can’t link across file systems
![Page 22: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/22.jpg)
Symbolic Links (also Called Symlinks)
• Separate file type (identified by l in listing) and having its own inode.
• Contains the pathname of another file or directory.
• Can link directories (unlike hard links).
• Can link across file systems.
• Link and file pointed to are not equivalent (they have different inode numbers).
• Dangling symlinks are possible if you remove original file.
![Page 23: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/23.jpg)
File Ownership and Group Ownership
• chown - Change the ownership of a file (restricted to su on BSD- based systems) Example: $ su Password: ****** # chown rick grades
•chgrp – Change the group owner of a file (if you’re not a member of the group, only su can change on BSD UNIX) Example: $ chgrp csinstructors grades
• Superuser can do both actions with just chown. Example: chown rick:csinstructors grades
![Page 24: The UNIX File System](https://reader035.vdocuments.us/reader035/viewer/2022081501/56813a38550346895da22282/html5/thumbnails/24.jpg)
Locating Files with find• find recursively finds files in a directory tree and takes an action.
• General form: find <path> <selection criteria> <action>
• Some Examples:
find / -name csgrades –print Starting at root,
look for the file(s) csgrades and display them on screen.
find . –atime +365 –ls Starting in current
directory, look for all files more 365 days without access
and list them.