intro unix/linux 07


Published on

creating and changing directories

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

intro unix/linux 07

  1. 1. Lesson 7-Creating and Changing Directories
  2. 2. Overview <ul><li>Using directories to create order. </li></ul><ul><li>Managing files in directories. </li></ul><ul><li>Using pathnames to manage files in directories. </li></ul><ul><li>Accessing files in remote directories. </li></ul><ul><li>Managing files from more than one directory. </li></ul><ul><li>Moving and removing directories and their contents. </li></ul>
  3. 3. Using Directories to Create Order <ul><li>A file is a collection of information stored electronically on the hard drive of a system. </li></ul><ul><li>An inode contains all the information about each file, including the location of the actual file on the disk. </li></ul>
  4. 4. Using Directories to Create Order <ul><li>The home directory. </li></ul><ul><li>Listing files in a directory. </li></ul><ul><li>Working with directories. </li></ul><ul><li>Distinguishing between files and directories. </li></ul><ul><li>Obtaining information about a directory or its contents. </li></ul>
  5. 5. The Home Directory <ul><li>The “pwd” command displays the path from root to the user’s home directory. </li></ul><ul><li>The /etc/passwd file holds the user’s home directory path. </li></ul><ul><li>A file is created and listed in the current home directory by default. </li></ul>
  6. 6. Listing Files in a Directory <ul><li>The “ls” command reads the names of the files listed in the current directory, and then outputs those names. </li></ul><ul><li>Each file in the directory contains only the name of that file and a number that leads to the inode for that file. </li></ul><ul><li>The “–i” option instructs ls to include the inodes in its output. </li></ul><ul><li>Inodes are small pieces of memory created on the hard disk when it is formatted. </li></ul>
  7. 7. Listing Files in a Directory <ul><li>The inode has a list of addresses of the block on the disk where the data that comprise the file is actually located. </li></ul><ul><li>It addresses up to 13 data blocks on the hard drive. </li></ul><ul><li>The first ten blocks addressed from the inode contains the actual file data. </li></ul><ul><li>If a file is too large, additional blocks are allocated to fit the data. </li></ul>
  8. 8. Working with Directories <ul><li>The “mkdir” command is used for creating a new directory. </li></ul><ul><li>Directories created in uppercase are listed first by the ls command. </li></ul><ul><li>Each new directory created is called a subdirectory. </li></ul><ul><li>The “cd” or “change directory” command instructs the shell to locate the directory listed as an argument. </li></ul>
  9. 9. Working with Directories <ul><li>The “pwd” command outputs the absolute path from the top of the file system (root or /) to the current directory. </li></ul><ul><li>A path is a list of directory names separated by the / (slash) character. </li></ul><ul><li>The topmost directory is called the root and is symbolized by the first forward slash (/) in the pathname. </li></ul>
  10. 10. Working with Directories <ul><li>The vi editor can be used for creating a file within a directory or subdirectory. </li></ul><ul><li>The cd command, without any directory name as an argument, returns to the home directory. </li></ul><ul><li>The “–R” (recursive) option used with ls descends through each subdirectory in the directory tree. </li></ul>
  11. 11. Distinguishing between Files and Directories <ul><li>The “ls –F” command displays directory names with a slash appended at the end. </li></ul><ul><li>Filenames displayed with an asterisk (*) at the end are executable files. </li></ul><ul><li>The “–C” option to ls instructs ls to make several columns in the output instead of one. </li></ul>
  12. 12. Obtaining Information About a Directory or its Contents <ul><li>The “ls –l” command displays the permissions and owners of files listed in a directory. </li></ul><ul><li>The “–d” option with ls command returns a listing of information about the contents of the target directory. </li></ul><ul><li>The “ls –ld” displays the permissions of the directory, and not the information about the files. </li></ul>
  13. 13. Managing Files in Directories <ul><li>Moving or copying files into a subdirectory. </li></ul><ul><li>Accessing a file in a subdirectory. </li></ul><ul><li>Avoiding mistakes when moving files into directories. </li></ul><ul><li>Renaming files. </li></ul><ul><li>Removing files from subdirectories. </li></ul>
  14. 14. Moving or Copying Files into a Subdirectory <ul><li>Moving a file or directory does not move the file electronically. </li></ul><ul><li>The name and the inode number are erased from the current directory and are written in the subdirectory. </li></ul>
  15. 15. Moving or Copying Files into a Subdirectory <ul><li>When a file is copied, a second electronic version is created. </li></ul><ul><li>The copied file does not have the same inode number as the original. </li></ul><ul><li>The copied file has its own inode, permission, and data blocks containing the new files actual contents. </li></ul>
  16. 16. Accessing a File in a Subdirectory <ul><li>To access a file in a subdirectory: </li></ul><ul><ul><li>Locate the inode number of the file. </li></ul></ul><ul><ul><li>Check the permissions on the directory. </li></ul></ul><ul><ul><li>Get the directory’s address from the inode. </li></ul></ul><ul><ul><li>Identify the inode associated with the file. </li></ul></ul><ul><ul><li>Read the files from the data blocks. </li></ul></ul>
  17. 17. Avoiding Mistakes When Moving Files into Directories <ul><li>The mv utility takes two arguments. </li></ul><ul><li>The first argument is the name of an existing file. </li></ul><ul><li>The second argument is a new name assigned to existing file, unless the second argument is a directory. </li></ul><ul><li>A slash at the end of the second argument interprets the argument as a directory name. </li></ul>
  18. 18. Renaming Files <ul><li>The command to move a file listing from the current directory to a new directory and also to change its filename is mv filename subdirectory/newfilename. </li></ul><ul><li>Moving a file just moves its listing from one directory to another. </li></ul>
  19. 19. Removing Files from Subdirectories <ul><li>The “rm” command can be used for removing files form a directory or subdirectory. </li></ul><ul><li>After the rm command, the entry of the file is removed, and the data blocks listed in the inode for that file are released. </li></ul>
  20. 20. Using Pathnames to Manage Files in Directories <ul><li>Accessing or creating a subdirectory. </li></ul><ul><li>Using pathnames with utilities. </li></ul><ul><li>Copying files into other directories using paths. </li></ul><ul><li>Using parent directory names. </li></ul><ul><li>Copying and moving multiple files to subdirectories. </li></ul><ul><li>Examining the full path from root to directories and files. </li></ul>
  21. 21. Using Pathnames to Manage Files in Directories <ul><li>Explicitly accessing a user’s home directory. </li></ul><ul><li>Including other users’ logins in directory paths. </li></ul><ul><li>Returning to the previous directory. </li></ul>
  22. 22. Accessing or Creating a Subdirectory <ul><li>Providing a full path to the cd command without any spaces can access a subdirectory. </li></ul><ul><li>Pathnames are the mechanism used to tell the shell what path to follow to access a file or directory not listed in the current directory. </li></ul><ul><li>Remote directories can also be created by specifying the relative or absolute pathname. </li></ul>
  23. 23. Using Pathnames with Utilities <ul><li>Pathnames can also be used as arguments to utilities. </li></ul><ul><li>A relative path can be specified for accessing a file without changing the current directory. </li></ul>
  24. 24. Copying Files into Other Directories Using Paths <ul><li>Pathnames are particularly useful with the “cp” and “mv” commands. </li></ul><ul><li>Any command that takes a filename or directory name as an argument can be given an explicit pathname argument. </li></ul>
  25. 25. Using Parent Directory Names <ul><li>A single dot is used for referring to the current directory. </li></ul><ul><li>In a directory, the .. (dot-dot) is the listing for its parent directory. </li></ul><ul><li>The parent directory is the directory located one level above the current directory. </li></ul>
  26. 26. Using Parent Directory Names <ul><li>The parent directory lists the current directory’s name and inode. </li></ul><ul><li>The . and .. are the two listings that get created when a new directory is created. </li></ul>
  27. 27. Copying and Moving Multiple Files to Subdirectories <ul><li>The move and copy commands are usually used with only two arguments. </li></ul><ul><li>The cp utility displays a usage error message when more than two arguments are specified and the last argument is not a directory. </li></ul><ul><li>In the cp command, if the third argument specified is a directory, then the rest of the file arguments are copied into the destination subdirectory. </li></ul>
  28. 28. Copying and Moving Multiple Files to Subdirectories <ul><li>The mv and cp commands can affect multiple files when the last argument is a directory. </li></ul><ul><li>The .. (dot-dot) listing can be used in the mv command for moving files into the parent directory. </li></ul>
  29. 29. Examining the Full Path from Root to Directories and Files <ul><li>A user can specify a directory’s location with the full path from the top of the file system. </li></ul><ul><li>The full path starting at root identifies a file explicitly. </li></ul><ul><li>The pathname for every file describes a particular file uniquely and absolutely. </li></ul><ul><li>The full pathname to a file is called its absolute pathname. </li></ul>
  30. 30. Explicitly Accessing a User’s Home Directory <ul><li>The “ls ~” command lists all filenames listed in the home directory. </li></ul><ul><li>The C shell and all shells define the tilde (~) as the path to the user’s home directory. </li></ul><ul><li>The shell replaces the tilde (~) with the value of the path to the user’s home directory. </li></ul><ul><li>The shell replaces the tilde (~) in any command line with the absolute path from root to the user’s home directory. </li></ul>
  31. 31. Including Other Users’ Logins in Directory Paths <ul><li>The “echo ~root” option displays the path to the home directory of the user root. </li></ul><ul><li>The contents of the home directory of a user are displayed if the user has appropriate permissions. </li></ul>
  32. 32. Returning to the Previous Directory <ul><li>The bash and ksh shells maintain the previous directory path as the value of a variable. </li></ul><ul><li>The tcsh shell maintains a list of previous directories in a list that can be accessed. </li></ul><ul><li>The value of the variable PWD is the path to the current directory. </li></ul>
  33. 33. Returning to the Previous Directory <ul><li>An alternative way of specifying the previous directory on many bash and ksh shells is to do it with a dash argument. </li></ul><ul><li>The exit command can be used for exiting from a child bash or ksh to the login shell. </li></ul><ul><li>The “popd” command instructs the shell to move the directory currently on the top of the list of directories that are added to the list. </li></ul>
  34. 34. Accessing Files in Remote Directories <ul><li>A filename that includes no relative path defaults to the current directory. </li></ul><ul><li>The ./ is the default relative path that is used. </li></ul><ul><li>Every file on the system has a unique pathname from / (root). </li></ul>
  35. 35. Accessing Files in Remote Directories <ul><li>The “pwd” command displays the absolute path of the present directory. </li></ul><ul><li>A path always starts at root (/) and includes the appropriate subdirectories. </li></ul>
  36. 36. Managing Files from More Than One Directory <ul><li>A listing for a file in a directory is a link to the file. </li></ul><ul><li>The ls –l command at the start lists the number of data blocks used by files and directories in the current directory. </li></ul><ul><li>In a long listing entry, or record, the information for each file or directory is divided into seven fields. </li></ul><ul><li>The second field in each entry of a file indicates the number of directories where the object is listed. </li></ul>
  37. 37. Managing Files from More Than One Directory <ul><li>Linking files: </li></ul><ul><ul><li>The “ln” command can be used for linking a file to the current directory. </li></ul></ul><ul><ul><li>The index card of each file keeps track of the number of directories that list it. </li></ul></ul><ul><ul><li>Each instance of the file listed in a directory is one link. </li></ul></ul><ul><ul><li>Removing a file removes it from the directory listing. </li></ul></ul>
  38. 38. Managing Files from More Than One Directory <ul><li>Linking files (continued): </li></ul><ul><ul><li>The ln utility can be used to link multiple files at a time. </li></ul></ul><ul><ul><li>The “ls –il” command lists the current files with link count and inodes. </li></ul></ul><ul><ul><li>The “–s” option to ln creates a file in the current directory that contains information required to locate the linked file. </li></ul></ul>
  39. 39. Managing Files from More Than One Directory <ul><li>Using symbolic links: </li></ul><ul><ul><li>A symbolic link is a small file in a directory on one file system partition that points to the correct file system and the correct inode for the linked file. </li></ul></ul><ul><ul><li>In the long listing of the symbolic link, the initial character is an “l” for directories and files. </li></ul></ul><ul><ul><li>An arrow and path at the end of the listing displays the actual directory where the link points. </li></ul></ul>
  40. 40. Managing Files from More Than One Directory <ul><li>Using symbolic links (continued): </li></ul><ul><ul><li>The ln utility provides a “–d” option for the root user to create hard links to directories. </li></ul></ul><ul><ul><li>Ordinary users cannot create directory hard links. </li></ul></ul><ul><ul><li>Ordinary users can create symbolic links to directories. </li></ul></ul>
  41. 41. Managing Files from More Than One Directory <ul><li>The administrator can divide the hard drive into partitions for installing the system. </li></ul><ul><li>The partition labeled / holds the core of the hierarchal directory tree. </li></ul><ul><li>A partition is further divided into small pieces called data blocks, which hold the actual data for the file. </li></ul>
  42. 42. Managing Files from More Than One Directory <ul><li>Each partition holds a single file system and has a set of unique inode numbers for that partition. </li></ul><ul><li>The inode contains the whole of a file, including its descriptive characteristics and the addresses of the blocks where the file data resides. </li></ul>
  43. 43. Moving and Removing Directories and their Contents <ul><li>Renaming a directory only changes the name of the directory, the inode number and its location remains unchanged. </li></ul><ul><li>A directory can also be moved from a different location in the hierarchal file structure without affecting the child contents of the directory. </li></ul>
  44. 44. <ul><li>The “rmdir” command is used for removing an empty directory. </li></ul><ul><li>The “–r” option instructs the rm command to recursively remove file. </li></ul>Moving and Removing Directories and their Contents
  45. 45. Summary <ul><li>Files reside in one or more data blocks on the hard drive. </li></ul><ul><li>Each file in a directory is assigned a unique inode. </li></ul><ul><li>A directory is a special kind of file with very specific contents, namely the names of files and directories, each with its associated inode number. </li></ul><ul><li>The passwd file holds the location of a user’s home directory. </li></ul>
  46. 46. Summary <ul><li>The mkdir command is used for creating remote directories. </li></ul><ul><li>The tilde (~) is interpreted as the user’s home directory. </li></ul><ul><li>The name .. (dot-dot) is interpreted as the parent directory. </li></ul><ul><li>The “rm –r dirname” command recursively removes the contents of the directory specified. </li></ul>