370 likes | 387 Views
Learn about the directory structures and navigation in the Linux file system. This chapter covers topics such as file naming rules, absolute vs relative pathnames, creating and removing directories, accessing permissions, and more.
E N D
Agenda The Linux File System (chapter 4 in text) • Directory Structures / Navigation • Terminology / File Naming Rules • Relative vs Absolute pathnames • mkdir, mkdir -p, rmdir, rm -r • ls, ls -a, ls -F, ls -l, ls -ld • Setting Access Permissions • chmod / umask • Linking Files • Hard Links / Symbolic Links
File System • A File System is a structure used to organize programs and data on a computer’s storage device • Linux (Unix) OS has a special file called a directory file used to store ordinary files as well as other “directories” • Directories allow a computer’s file system to be better organized.
Hierarchical File System • In the Linux (Unix) OS, the “root directory” /is the starting directory, and other “child directories”, “grandchild directories” …etc. are created • The hierarchical structure resembles an “upside-down tree” root / home etc a b c 1 2
Typical UNIX Directories • /Root directory (ancestor to all directories) • /home Used to store users’ home directories • /usr/bin Common utilities (commands) for user • /usr/sbin Common utilities for user administration • /etc General System Admin. Files (eg passwd) • /varDynamic files(log files) • /tmpUser’s temporary file for programs • /devDevice files (terminals,printers, etc…)
File Naming Rules • The following rules apply to naming ordinary files or “directory files”: • Some file systems restrict filename size to 14 characters, other file systems allow for 255 characters (best to select filename size of 14) • Can use letters (upper & lower case), numbers, period , comma or underscore _ characters • Upper case different than lower case • Period at beginning of filename hides file
Pathnames • A pathname is a listing of directories that will lead to a directory or a file. • Examples: • Directory pathname: • /home/username/ics124/assignments • File pathname: • /home/username/ops224/assignments/assn1.txt
Absolute vs Relative Pathnames • Absolute Pathname • A pathname that begins from root. • The pathname usually begins with a slash / or ~ • eg. /home/msaul/ops224 • ~/ics124/sample_tests(where ~ represents /home/username) • Relative Pathname • A pathname that is “relative” to the location of another directory
Relative Pathnames • Rules: • Relative pathname does NOT begin with a slash. • Following symbols can be used: • .. parent directory • . current directory
Relative Pathnames • Examples: • Change to another directory branch from parent directory: cd ../ops224 • copy sample.c file from your professor’s directory to your current directory:cp /home/profid/oop244/sample.c .
Making Directories • Building directories is similar in approach to building a house • Begins from a foundation (eg home directory) • Need to build in proper order (add on addition to house in right location) • when building directories from different locations, must provide proper absolute or relative pathname!!
Where do we want to build directory? • Want to build a directory calledtmp that branches-off of your home directory • Verify which directory you are located (either look at directory from command prompt or issue the command pwd • Type mkdir tmp at the Unix prompt, followed by ENTER • Always verify that directory has been created (e.g. use lsor ls -ld command)
Creating Parent Directories • To create directory paths with parent directories that do not exist you can use the command • mkdir -p pathname • eg. Mkdir -p mur/dir1 • (This would create the parent directory mur and then the child directory dir1)
Removing Directories Removing directories is reverse order of building directories • Issue command rmdir • Cannot remove directories containing files or other subdirectories (unless using rm -r) • Need to “step-back” to at least parent directory to remove empty directory or related child, grandchild, etc… directories
Listing Directory Contents • ls Compact listing on non-hidden files • ls -a Compact listing of ALL files • ls -l Detailed listing of non-hidden files • ls -F Displays symbols to mark directories and executable files • ls -ld Detailed listing of specified directory • ls -i Displays i-node number (I.d. number of files)
Access Permissions • Limiting unauthorized access to your directories and files is a very important concern for ALL Linux (Unix) users. • Consequences of Unauthorized Access: • Copying your assignments (cheating) • Using your account for illegal activity • Using your account to send obscene messages • Tampering with files
File / Directory Permissions • The Linux (Unix) OS can allow the user to specify read, write and execute permissions to the user, group or all others (UGO) for files. • A user can also specify read, write and execute permissions for a directory. The executepermission for a directory allows the person to view files in that directory
chmod Command(Relative Method) • Used to change the access permissions of a file or directory • Format: • chmod [option] [who] [operation] [permission] filechmod [option] [permission] file-list • who relates to user (u) , group (g) or all others (o) • operation relates to adding (+), removing (-) orsetting (=) permissions • permissions are read (r), write (w) and execute (x)
chmod Command(Relative Method) • Examples: • Add Permission • chmod g+rw file.name • chmod o+x file.name • Remove Permission • chmod g-w file.name • chmod a-w file.name (removes write for ugo) • Set Permission • chmod o=rx file.name • chmod go=rx filename Note: you can use wildcard symbols (eg *) to match particular files
chmod Command(Absolute Method) • You can use the chmod command with octal number to represent (in binary) a permission (1) or removal of a permission (0) for the file or directory • This is referred to as an Absolute command, and many prefer this “short-cut” method to changing file / directory permissions
Relationship of a Binary to an Octal Number Notice the Pattern: • Largest 3 digit binary is 111 • 1 octal digit will represent a 3 digit binary number • Highest Octal digit is 7 • Therefore: 1112 = 78
Binary to Octal Relationship: Octal Binary 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111 Therefore: Octal number 755 is equal to: 1 1 1 1 0 1 1 0 1 in binary This can be related to the permissions: r w x r - x r - x
chmod - Example(Absolute Method) • Applying octal values of rwx use the absolute chmod command: • chmod 777 filename - r w x r w x r w x • chmod 755 filename - r w x r - x r - x • chmod 711 filename - r w x - - x - - x • chmod 644 filename - r w - r - - r - -
Practical Applications ofchmod Command • Pass-Through Permissions • Pass-Through Permissions allow users to pass-through the home directories and other subdirectories until they reach a directory that provides read and execute permissions to read files. (pass-through permissions drwx--x--x) • To deny any access other than yourself, you can remove pass-through permissions of your home directory (drwx------)
Practical Applications ofchmod Command • Linking & Sharing Files • Set up directory and file permissions to allow users to modify a file or set up permissions of file to allow user to view, but not modify a file. • Webpages • Allow or deny other access to files. For example, use chmod command to allow group & others read and execute permissions to “pass-through” your directories.
Creating a Mask • Are you tired of continually changing access permissions for newly-created files or directories? • The umask command automatically sets the file permissions upon creation of the file. • This process is useful, since user may sometimes forget to change the permissions of newly-created files or when they transfer files via the FTP application
umask Command • Used to automatically establish file permission upon creation • Format: • umask [mask] • wheremaskrepresents a 3-digit octal number regarding UGO and permissions to be assigned. • Note: The rules vary between setting file masks and directory masks
Setting Directory Mask • To change directory mask: • Determine octal number that would set directory permission • Subtract octal number 777 from octal number determined above to get result • issue the command : • umask [octal number] • Note: should also be able to use “relative method” with umask command - may be easier
Setting Directory Mask • Example: • To set mask for newly-created directories to:r w x r - - r - - • Determine octal number1 1 1 1 0 0 1 0 0 = 744 • Subtract 777 from 744 = 033 • Issue command umask 033 • Issue command umask to verify change
Setting File Mask • To change directory mask: • Determine binary number that would set directory permission • Subtract above binary number from 110110110 and convert result to octal number to determine umask value • issue the command : • umask [octal number]
Setting File Mask - Example1 • Example: • To set mask for newly-created files to:r w - r - - r - - • Convert to binary110100100 • Subtract above from 110110110 110110110 - 110100100= 000010010 (which is 022) • issue umask 022 (enter umask to verify)
Setting File Mask - Example2 • Example: • To set mask for newly-created files to:r w x r - - r - - • Convert to binary111100100 • Subtract above from 110110110 110110110 - 111100100= 000010010 (which is 022 octal) • issue umask 022 (enter umask to verify) Cannot subtract 1 from 0
File Identification • Files in UNIX and Linux contain contents (e.g. source code) but each file also contains information regarding the file itself. • Each file is assigned a i-node number. To view i-node information, you can issue the UNIX command ls -i • 705900 testfor706640 chevy703817 thefile Note that each file is unique since they have been assigned a differenti-node number for identification.
Linking Files • Since UNIX is a multi-user operating system, it makes sense to allow users to share files to collaborate on projects (such as programming projects, reports, etc.) • In order to avoid duplication and inefficiency, it is better to provide links between files to give user illusion that what is edited on their file affects contents on other groups’ files.
Linking Files There are two major types of links: Hard Link:ln [existing_file] [linked_file] • A directory entry containing the same “i-node number” of a file. All files have at least one hard-link - when removed, the link is removed Symbolic Link:ln -s [existing_file] [linked_file] • A directory entry containing pathname to file (i.e. a pointer). Unlike hard links, i-node numbers can be different, but possess other useful features.
Linking Files • Remember to set appropriate permissions for: • your directories (such as pass-through permissions and permissions for appropriate directory) • the file to link (which groups can modify the link file, which groups can view, but not modify file, and which groups are not permitted to modify or view linked file)
Hard Link Features • Cannot make hard links to directories • Cannot cross different file systems (since other file system may use i-node number for a already existing file…) • User can allow access to file to link via directory access permissions and will still allow access if user later block group & other access. • When “original file” that other user’s linked to is removed “linked file(s)’ will still exist.
Soft Link Features • Can be used by users to link directories • Can link across different file systems since they are considered to be “pointers” • “Broken Links” can occur if original file is removed and link “points” to a file that does not exist.