Top Banner
Lesson 7-Creating and Changing Directories
46

Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Dec 26, 2015

Download

Documents

Edward Mathews
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Lesson 7-Creating and Changing Directories

Page 2: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Overview

Using directories to create order.

Managing files in directories.

Using pathnames to manage files in directories.

Accessing files in remote directories.

Managing files from more than one directory.

Moving and removing directories and their contents.

Page 3: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Using Directories to Create Order

A file is a collection of information stored electronically on

the hard drive of a system.

An inode contains all the information about each file,

including the location of the actual file on the disk.

Page 4: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Using Directories to Create Order

The home directory.

Listing files in a directory.

Working with directories.

Distinguishing between files and directories.

Obtaining information about a directory or its contents.

Page 5: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

The Home Directory

The “pwd” command displays the path from root to the

user’s home directory.

The /etc/passwd file holds the user’s home directory path.

A file is created and listed in the current home directory by

default.

Page 6: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Listing Files in a Directory

The “ls” command reads the names of the files listed in the

current directory, and then outputs those names.

Each file in the directory contains only the name of that file

and a number that leads to the inode for that file.

The “–i” option instructs ls to include the inodes in its output.

Inodes are small pieces of memory created on the hard disk

when it is formatted.

Page 7: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Listing Files in a Directory

The inode has a list of addresses of the block on the disk

where the data that comprise the file is actually located.

It addresses up to 13 data blocks on the hard drive.

The first ten blocks addressed from the inode contains the

actual file data.

If a file is too large, additional blocks are allocated to fit the

data.

Page 8: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Working with Directories

The “mkdir” command is used for creating a new directory.

Directories created in uppercase are listed first by the ls

command.

Each new directory created is called a subdirectory.

The “cd” or “change directory” command instructs the shell

to locate the directory listed as an argument.

Page 9: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Working with Directories

The “pwd” command outputs the absolute path from the

top of the file system (root or /) to the current directory.

A path is a list of directory names separated by the / (slash)

character.

The topmost directory is called the root and is symbolized

by the first forward slash (/) in the pathname.

Page 10: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Working with Directories

The vi editor can be used for creating a file within a

directory or subdirectory.

The cd command, without any directory name as an

argument, returns to the home directory.

The “–R” (recursive) option used with ls descends through

each subdirectory in the directory tree.

Page 11: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Distinguishing between Files and Directories

The “ls –F” command displays directory names with a slash

appended at the end.

Filenames displayed with an asterisk (*) at the end are

executable files.

The “–C” option to ls instructs ls to make several columns in

the output instead of one.

Page 12: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Obtaining Information About a Directory or its Contents

The “ls –l” command displays the permissions and owners

of files listed in a directory.

The “–d” option with ls command returns a listing of

information about the contents of the target directory.

The “ls –ld” displays the permissions of the directory, and

not the information about the files.

Page 13: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Managing Files in Directories

Moving or copying files into a subdirectory.

Accessing a file in a subdirectory.

Avoiding mistakes when moving files into directories.

Renaming files.

Removing files from subdirectories.

Page 14: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Moving or Copying Files into a Subdirectory

Moving a file or directory does not move the file

electronically.

The name and the inode number are erased from the

current directory and are written in the subdirectory.

Page 15: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Moving or Copying Files into a Subdirectory

When a file is copied, a second electronic version is

created.

The copied file does not have the same inode number as

the original.

The copied file has its own inode, permission, and data

blocks containing the new files actual contents.

Page 16: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Accessing a File in a Subdirectory

To access a file in a subdirectory:

Locate the inode number of the file.

Check the permissions on the directory.

Get the directory’s address from the inode.

Identify the inode associated with the file.

Read the files from the data blocks.

Page 17: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Avoiding Mistakes When Moving Files into Directories

The mv utility takes two arguments.

The first argument is the name of an existing file.

The second argument is a new name assigned to existing

file, unless the second argument is a directory.

A slash at the end of the second argument interprets the

argument as a directory name.

Page 18: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Renaming Files

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.

Moving a file just moves its listing from one directory to

another.

Page 19: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Removing Files from Subdirectories

The “rm” command can be used for removing files form a

directory or subdirectory.

After the rm command, the entry of the file is removed, and

the data blocks listed in the inode for that file are released.

Page 20: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Using Pathnames to Manage Files in Directories

Accessing or creating a subdirectory.

Using pathnames with utilities.

Copying files into other directories using paths.

Using parent directory names.

Copying and moving multiple files to subdirectories.

Examining the full path from root to directories and files.

Page 21: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Using Pathnames to Manage Files in Directories

Explicitly accessing a user’s home directory.

Including other users’ logins in directory paths.

Returning to the previous directory.

Page 22: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Accessing or Creating a Subdirectory

Providing a full path to the cd command without any spaces

can access a subdirectory.

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.

Remote directories can also be created by specifying the

relative or absolute pathname.

Page 23: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Using Pathnames with Utilities

Pathnames can also be used as arguments to utilities.

A relative path can be specified for accessing a file without

changing the current directory.

Page 24: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Copying Files into Other Directories Using Paths

Pathnames are particularly useful with the “cp” and “mv”

commands.

Any command that takes a filename or directory name as

an argument can be given an explicit pathname argument.

Page 25: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Using Parent Directory Names

A single dot is used for referring to the current directory.

In a directory, the .. (dot-dot) is the listing for its parent

directory.

The parent directory is the directory located one level

above the current directory.

Page 26: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Using Parent Directory Names

The parent directory lists the current directory’s name and

inode.

The . and .. are the two listings that get created when a

new directory is created.

Page 27: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Copying and Moving Multiple Files to Subdirectories

The move and copy commands are usually used with only two

arguments.

The cp utility displays a usage error message when more than

two arguments are specified and the last argument is not a

directory.

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.

Page 28: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Copying and Moving Multiple Files to Subdirectories

The mv and cp commands can affect multiple files when

the last argument is a directory.

The .. (dot-dot) listing can be used in the mv command for

moving files into the parent directory.

Page 29: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Examining the Full Path from Root to Directories and Files

A user can specify a directory’s location with the full path

from the top of the file system.

The full path starting at root identifies a file explicitly.

The pathname for every file describes a particular file

uniquely and absolutely.

The full pathname to a file is called its absolute pathname.

Page 30: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Explicitly Accessing a User’s Home Directory

The “ls ~” command lists all filenames listed in the home

directory.

The C shell and all shells define the tilde (~) as the path to the

user’s home directory.

The shell replaces the tilde (~) with the value of the path to the

user’s home directory.

The shell replaces the tilde (~) in any command line with the

absolute path from root to the user’s home directory.

Page 31: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Including Other Users’ Logins in Directory Paths

The “echo ~root” option displays the path to the home

directory of the user root.

The contents of the home directory of a user are displayed

if the user has appropriate permissions.

Page 32: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Returning to the Previous Directory

The bash and ksh shells maintain the previous directory

path as the value of a variable.

The tcsh shell maintains a list of previous directories in a

list that can be accessed.

The value of the variable PWD is the path to the current

directory.

Page 33: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Returning to the Previous Directory

An alternative way of specifying the previous directory on

many bash and ksh shells is to do it with a dash argument.

The exit command can be used for exiting from a child bash

or ksh to the login shell.

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.

Page 34: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Accessing Files in Remote Directories

A filename that includes no relative path defaults to the

current directory.

The ./ is the default relative path that is used.

Every file on the system has a unique pathname from /

(root).

Page 35: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Accessing Files in Remote Directories

The “pwd” command displays the absolute path of the

present directory.

A path always starts at root (/) and includes the appropriate

subdirectories.

Page 36: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Managing Files from More Than One Directory

A listing for a file in a directory is a link to the file.

The ls –l command at the start lists the number of data

blocks used by files and directories in the current directory.

In a long listing entry, or record, the information for each

file or directory is divided into seven fields.

The second field in each entry of a file indicates the number

of directories where the object is listed.

Page 37: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Managing Files from More Than One Directory

Linking files:

The “ln” command can be used for linking a file to the current

directory.

The index card of each file keeps track of the number of

directories that list it.

Each instance of the file listed in a directory is one link.

Removing a file removes it from the directory listing.

Page 38: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Managing Files from More Than One Directory

Linking files (continued):

The ln utility can be used to link multiple files at a time.

The “ls –il” command lists the current files with link count and

inodes.

The “–s” option to ln creates a file in the current directory that

contains information required to locate the linked file.

Page 39: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Managing Files from More Than One Directory

Using symbolic links:

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.

In the long listing of the symbolic link, the initial character is an

“l” for directories and files.

An arrow and path at the end of the listing displays the actual

directory where the link points.

Page 40: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Managing Files from More Than One Directory

Using symbolic links (continued):

The ln utility provides a “–d” option for the root user to create

hard links to directories.

Ordinary users cannot create directory hard links.

Ordinary users can create symbolic links to directories.

Page 41: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Managing Files from More Than One Directory

The administrator can divide the hard drive into partitions

for installing the system.

The partition labeled / holds the core of the hierarchal

directory tree.

A partition is further divided into small pieces called data

blocks, which hold the actual data for the file.

Page 42: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Managing Files from More Than One Directory

Each partition holds a single file system and has a set of

unique inode numbers for that partition.

The inode contains the whole of a file, including its

descriptive characteristics and the addresses of the blocks

where the file data resides.

Page 43: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Moving and Removing Directories and their Contents

Renaming a directory only changes the name of the

directory, the inode number and its location remains

unchanged.

A directory can also be moved from a different location in

the hierarchal file structure without affecting the child

contents of the directory.

Page 44: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

The “rmdir” command is used for removing an empty

directory.

The “–r” option instructs the rm command to recursively

remove file.

Moving and Removing Directories and their Contents

Page 45: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Summary

Files reside in one or more data blocks on the hard drive.

Each file in a directory is assigned a unique inode.

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.

The passwd file holds the location of a user’s home

directory.

Page 46: Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.

Summary

The mkdir command is used for creating remote directories.

The tilde (~) is interpreted as the user’s home directory.

The name .. (dot-dot) is interpreted as the parent directory.

The “rm –r dirname” command recursively removes the

contents of the directory specified.