(精品)Unix5(en).ppt
Chaper 5-Intro to UNIX File SystemlFiles are the basic system used by UNIX to store&organize your informationlUNIX divides the system into smaller units of storageDirectoriesSub-Directories(nested under directories)lThis sets up the basic file system used by UNIX(and most other OS)1File TypeslA file is a sequence of byteslUNIX has three categories of files:Regular Files-code,text,data,etc.Directory Files-info about other filesSpecial Files-info for I/O and peripherals*l*UNIX treats I/O devices as files,so each system device has a file2DirectorieslA simple system of organizing files on your UNIX disklUNIX organizes directories in levels“Hierarchical”structureThe top directory is called rootAll other directories branch from rootlDirectories dont contain your files,they provide a reference path for the fileslSee Figure 5.1(as following slide)3Figure 5-1Figure 5-1 Directory Structure Directory Structure4Parent&ChildlThe levels in the hierarchy are described using a parent/child relationshipParent is above the child in the hierarchyA parent may be the child of another directoryOnly the root directory has no parent directoryAll directories trace back to rootSee Figure 5.2(as following slides).5Figure 5-2Figure 5-2 Parent and Child Relationship Parent and Child Relationship6Important Directoriesl/Root directoryl/usrHolds many other user-oriented directoriesl/usr/docs,/usr/man,/usr/bin.In UNIX also hold users home directories,but linux dont.l/binBasic UNIX program filesl/devHolds device filesl/sbinHolds system files that usually are are automatically by the UNIX system.l/etcHolds UINX configuration files7The Home DirectorylA particular directory associated with user accountlWhen log on,you are placed automatically into your home directory.lYou can create files and subdirectories under it.lUsually has the same name as your accountlOther user cant access it.lSee Figure 5.3(as following slide)8Figure 5-3Figure 5-3 Directories,Subdirectories,and Files Directories,Subdirectories,and Files9Working DirectorylThe working directory is the directory you are currently working in lAccessing files in another directory requires that you Make that directory the working directoryORReference that directory when accessing the files in that directory(examples will be shown later)10Paths&PathnameslPathname a reference to a files directory within the file systemThe pathname traces a path for the file from the root directory downlA path ends with a fileOrdinary file ORDirectory file,which can have another branch of directories underneathlSee Figure 5.4(as following slide)11Figure 5-4Figure 5-4 Pathnames in a Directory Structure Pathnames in a Directory Structure12PathnameslAbsolute PathnamePath from root (/)downward/usr/local/stuff/file.txtlRelative PathnamePath from working directorystuff/file.txt which is only valid if the working directory happens to be/usr/local13Filenames&Directory NameslSection 5.3.5 covers the rules for naming files in UNIX(p90)These apply to any version of UNIX,including FreeBSD&LINUXRemember,UNIX names are case sensitivelfile.txt,File.Txt&FILE.TXT are differentSince UNIX names can be very long,use them to describe the file/directory contentsNO SPACES!EVER!WE MEAN IT!14ExtensionslExtensions further help to organize filenamesDifferent types of files have different extensions(.txt for text,.c for C,)Multiple extensions can be usedfile.has.more.txt15Directory CommandslSo how do you navigate directories in UNIX?The shell has specific commandsTo try these out,login to your systemSee Figure 5-5 for an example file system(as the following slide)16Figure 5-5Figure 5-5 An Example of a Directory Structure An Example of a Directory Structure17Where Am I?lpwd-print working directoryshows absolute pathnote:in most UNIX systems,the working directory you login to usually has the same name as your usernamethis doesnt show the files in the directory,just the absolute path$pwd Return.Display your HOME directory path./usr/david$_.Prompt for next command.18Where Am I Going?lcd-change directorycd source goes to the directory named source which is a child of the current directorycd/usr/david goes to the directory named david,which is a child of usr,which is a subdirectory of rootcd goes to the users home directorySee examples as the following two slides19Example 1$pwd Return.Check your current directory./usr/david$cd source Return.Change to source directory.$pwd Return.Display your working directory./usr/david/source$_.Prompt for the next command.20Example 2Assuming you have permission,you can change your working directory to/devby using the following command sequence:$cd/dev Return.Change to/dev directory.$pwd Return.Check your working directory./dev.Your current directory is/dev.$_.Prompt for next command.21I Need A Directory!lLets make a directory A fresh UNIX account has no subdirectories in the users home directoryDirectories are useful for grouping filesSome planning makes it easier to organize and handle your files in the long runlUse the mkdir command to make a new directory22mkdirlmkdir memos make the memos directory,a child of the current working directorylmkdir/usr/david/text make a directory under/usr/david named textlmkdir memos/old make a directory under memos named old,assuming memos is a child of the current working directorylmkdir-p xx/yy/zz make a the entire path specified(assumes they dont exist)lSee the following slides23Figure 5-6Figure 5-6 Your Directory Structure at the Beginning Your Directory Structure at the BeginningDirectory StructureLets start with the directory structure presented in Figure 5.6.Depending on your system configuration and administration requirements,you might have other files or subdirectories in your HOME directory.245.4.4 Directory Creation:The mkdir CommandThe mkdir(make directory)command creates a new subdirectory under your workingdirectory or any other directory you specify as part of the command.Create a directory called memos under your HOME directory:$cd Return.Make sure you are in your HOME directory.$mkdir memos Return.Create a directory called memos.$pwd Return.Check your working directory./usr/david$cd memos Return.Change to memos directory.$pwd Return.Check your working directory./usr/david/memos.Your current directory is memos.$_.Prompt for next command.25Figure 5-7Figure 5-7 Your Directory Structure After Adding the memos Subdirectories Your Directory Structure After Adding the memos SubdirectoriesBeforeAfter26While you are in your HOME directory,create a new subdirectory called important inthe memos directory.$cd Return.Make sure you are in your HOME directory.$mkdir memos/important Return.Specify the important directory pathname.$cd memos/important Return.Change to important directory.$pwd Return.Check your working directory./usr/david/memos/important$_.Now your working directory is important.Figure 5.8 shows your directory structure after adding memos and important subdirectories.27Figure 5-8Figure 5-8 Your Directory Structure After Adding the memos and important Subdirectories Your Directory Structure After Adding the memos and important Subdirectories28Figure 5-9Figure 5-9 Creating the source Directory Creating the source DirectoryFigure 5.9 shows how to create a directory called source under your HOME directory.Figure 5.10 shows your directory structure after adding the source subdirectory.A directory structure can be created according to your specific needs.4429Figure 5-10Figure 5-10 Your Directory Structure After Adding the source Directory Your Directory Structure After Adding the source DirectoryBeforeAfter30The mkdir Command:-p Option The-p option creates levels of directories under your current directory.Create a directory structure three levels deep,starting in the HOME directory:$cd Return.Make sure you are in your HOME directory.$mkdir-p xx/yy/zz Return.Create a directory called xx;in xx create a directory called yy,and in yy create a directory called zz.$_.Ready for next command.Figure 5.11 depicts the directory structure after this command sequence has beenapplied.-parents Option The alternative option in Linux.Like the-p option,-parents creates levels of directories under your current or the specified directory.The command line for using the-parents is:$mkdir-parents xx/yy/zz Return.Create a directory called xx;in xx,create a directory called yy;and in yy,create a directory called zz.31Figure 5-11Figure 5-11 Your Directory Structure After Adding the Three-Levels Deep Subdirectories Your Directory Structure After Adding the Three-Levels Deep SubdirectoriesBeforeAfter32I Dont Want It Anymore!lrmdir remove directorylBut,there are conditions must be in a parent directorycannot remove a directory unless it is empty(no files)lrmdir will return an error if the directory does not exist,or if it isnt emptylSee examples as the following two slides33Example 1Remove the important directory from your memos directory:$cd Return.Make sure you are in your HOME directory.$cd memos Return.Change your working directory to memos.$pwd Return.Make sure you are in memos./usr/david/memos$_.Yes,you are in memos.$rmdir important Return.Remove the important directory.$_.Ready for next command.1.You were able to remove the important subdirectory because it was an empty directory.2.You must be in a parent directory to remove a subdirectory.4434Example 2From the david directory,remove the source subdirectory:$cd Return.Change to david directory.$rmdir source Return.Remove the source directory.rmdir:source:Directory not empty$rmdir xyz Return.Remove a directory called xyz.rmdir:xyz:Directory does not exist$_.Ready for next command.1.You could not remove the source subdirectory because it was not anempty directory.2.rmdir returns an error message if you give a wrong directory nameor if it cannot locate the directory name in the specified pathname.3.You must be in the parent directory or a higher level of directoryto remove subdirectories(children).4435Whats In Here?lls list contents of directorylists files in current directory by name,in alphabetical order(files&directories)ls code lists contents of the child directory code,or any files named code in the current working directoryls has numerous options(see Table 5-1,as the following slides)lmake sure there is a space between the command name and the optionSee examples as the following slides 36Assuming your current directory is david,show the contents of your HOME directory by typing ls Return.$ls123Draft_1REPORTmemosmyfirstphonessourcexx$_37In some systems,the output of the ls command is not vertical in one column and the default format is set to display filenames across the screen.$ls123 Draft_1 REPORT memos myfirst phones source xx$_38You may want to list the contents of directories other than your current directory.While in your HOME directory david,list files in the source directory:$cd Return.Make sure you are in the david directory.$ls source Return.While in david,display list of files in the source directory.first.cfirst.cpp$_.Ready for next command.While in your HOME directory,check whether first.c exists in the source directory:$ls source/first.c Return.Display the first.c filename in the source directory to see whether it exists.It does exist,so the file-name is displayed.source first.c$ls xyz Return.Display a file called xyz if it exists.If it does not exist,you get the error message.xyz:No such file or directory$_.You get the prompt sign again.39Table 5.1 The ls Command Optionsls OptionsWhen you need more information about your files or you want the listing in a different Format,use the ls command with options.40The wonders of lslls-l long formatlls-a show hidden fileslls-F designate directories using/and executable files using*41File PermissionslAs a multi-user operating system,UNIX must setup a way to restrict access to files based on user preferenceslEvery file has an attribute,or permissionRead permission(r)Write permission(w)Executable permission(x)lPermissions are set for owners,groups and the world(everybody)lSee the following slides42Figure 5-13Figure 5-13 The The ls ls Command and the-l Option Command and the-l Option43Figure 5-14Figure 5-14 The The lsls Command-Long Format Command-Long FormatFile type-Ordinary filed directory fileb block-oriented special(device)file,such as diskc character-oriented special(device)file,such as printerl a link to another file44More about fileslUNIX has utilities to set permissionschmod we will cover this laterlNote that every file has a modification date,owner(user name),group and sizethese are only seen in long(-l)formatlWhat about hidden files?These start with a period(.)What do they do well hit that in Chapter 745Whats In My File?lcat displays file on screenTheres no scrolling or movement control,so cat just lets the ASCII fly!lWell,and will stop/start scrolling on some terminals,but its not an exact scienceHow to avoid this well use more later$cat myfirst Return.Display the myfirst file.$cat myfirst yourfirst Return.Display the myfirst and yourfirst files.46Putting Bits On Paperllp line printersends files to the printer,if your system has one installed(Section 5.6.2,p113)lnot your local PCs printer,but the network printer attached to the UNIX serverlp fred.txt send fred.txt to the printerlpstat print status(queue)lpstat-d name of default printercancel lp1 kill jobs on printer lp1llpr printing command in Linux47Cleaning Out The Fileslrm remove(delete)filesrm text.txt delete the file text.txtrm fred.txt iso9000.txt delete the files fred.txt and iso9000.txtlrm will give an error if a specified file isnt thereby default,rm does not confirm a delete job48Remove group of files lWanna clean out a whole group of files in one pass use a wildcardrm*.txt delete all files with the extension.txtrm*delete all files in the working dirrm-r*remove all files in this directory or any subdirectories49Whoops!lBe very very,very careful using rmno confirmation&no recovery-i should be used often-r should be used rarelywildcards are very dangerous,yet extremely useful50HomeworklP120Review Exercises 57,951