240 likes | 419 Views
The UNIX File System. 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.
E N D
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 Chapare 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.
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.
/ root bin dev home lib tmp sbin etc usr var dsk fd0 pr1 bin lib sbin local austin julie Typical File Hierarchy in UNIX progs login.sql .profile
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).
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
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
Navigating the UNIX Directory Structure • You are always located at one location in the tree • cdchanges 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 ../..
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.
The Home Directory • Directory where user is placed on login. • Determined by sixth field in/etc/passwdfile: • 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.
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.
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
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.
Look at the Output of ls -l ls –l -rw-rw-r-- 1 bournique instructors 143 Sep 1 08:45 grades permissions links owner group owner size last mod filename -rwxrwxrwx - rwx rwx rwx Owner Group World File Type • = ordinary file • d = directory
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
How a Directory Influences File Permissions 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) 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)
Changing Permissions With chmod General Form: chmod<SETTINGS> <FILE> u = user g = group o = other a = all • + (add) • (remove) • = (set) r = read w = write x = execute Example: chmod a-x testFile(relative change)
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) Octal Representation of -rwxrwxrwx 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>
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). • umaskstatement 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 .
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.
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
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.
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: $ chgrpcsinstructors grades • Superuser can do both actions with just chown. • Example: chownrick:csinstructors grades
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.