Tải bản đầy đủ (.docx) (19 trang)

Accessing Files and Directories

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (180.62 KB, 19 trang )

Accessing Files and Directories 5
Objectives
Upon completion of this module you should be able to:
• Contrast absolute and relative pathnames
• Access files and directories within the file structure using absolute and relative pathnames
• Identify and describe the parts of a command line
• Access files and directories within the file structure using pathname abbreviations
• List the contents of directories and their file types
• Identify various metacharacters
• Enter more than one command on a single command line
• Demonstrate the use of wildcard characters
Discussion – How does the Linux system know how to find files and directories?
File System Structure
Figure 5-1 shows a typical Linux system file structure; you will refer to this again later in this
course.
Figure 5-1 Linux File System Structure
Pathnames
A pathname uniquely identifies a particular file or directory by specifying its location. Pathnames
are similar to a road map or a set of directions that tells you how to get from one place in the
directory hierarchy to another.
Note – Depending on the setup of your system, home directories can be found in either /home
or /export/home. For the purposes of this discussion, /home is used.
The slashes within the pathnames are delimiters between object names. A slash in the first
position represents the / (root) directory. For example:
/home/user2/dir1/coffees
root directory Delimiters
Figure 5-2 Pathname With Delimiters
Notes
 The concept of pathnames is critical to the student’s understanding of how to access files in
the file structure. Pathnames identify every file in the file structure.
 Briefly describe the Linux file structure in terms of a hierarchy of directories,


subdirectories, and files. The file cabinet analogy works well with the Linux computing
environment file structure. If the file cabinet analogy does not work for everyone, you might
draw a “tree” structure. Use root (/) and some directories to illustrate the root, branches,
limbs, and so on.
 At some point it should be noted that this is not what the students’ file structure
will look like back at the office. In other words, the file names, and possibly the location of
the home directory, will be different in their work environment. The structure shown is an
example only.
 If the classroom lab is set up in such a way that students’ home directories are
under /export/home ,explain that when home directories reside on the local system they
are put, by convention, under /export/home .When home directories are provided by a
home directory server, by convention, they are found under /home.
There are two types of pathnames: absolute and relative.
Absolute Pathname
An absolute pathname specifies a file or directory in relation to the entire Linux file
hierarchy. The hierarchy begins at the / (root) directory.
Absolute pathnames always:
• Start at the root (/) directory and list each directory along the path to the
destination file (or directory)
• Use a slash (/) between each directory name in the path to indicate different
directories
Refer to Figure 5-1 for a visual representation of the following pathnames:
• Absolute pathname to user2
/home/user2
• Absolute pathname to dir1
/home/user2/dir1
• Absolute pathname to coffees
/home/user2/dir1/coffees
Relative Pathname
A relative pathname describes the location of a file or directory as it relates to the current

directory. If you are in a directory and you want to move down the directory tree, it is not
necessary to type the absolute pathname. You can just type the path starting with the name
of the next directory in the directory structure. If a pathname does not begin with a slash,
it is a relative pathname.
Relative pathnames are useful because they are shorter than absolute pathnames.
Use Figure 5-1 to support these examples:
• The current directory
/home
• Relative pathname to user2
user2
• Relative pathname to dir1
user2/dir1
• Relative pathname to coffees
user2/dir1/coffees
Command-Line Syntax
When using a terminal window, a shell prompt appears on the screen. For the Bourne and Korn
shells, the shell prompt for a user is a dollar sign ($). For a C shell user, the prompt is a percent
sign (%).
At the shell prompt, you can type commands. Commands are instructions which tell the system to
perform an action.
The general format for Linux commands is:
$ command [option (s)] [argument[s]]
command Executable (specifies what you want the system to do)
options Modify the executable (specifies how you want the command run)
argument File or directory, including the path name, or text. If a pathname is not specified
for a file or directory argument, then the operating system will use the current
directory.
• A space must be used as a delimiter between each part of the command line.
• Up to 256 characters can be entered on a single command line.
• Many commands do not require all three parts.

Some examples are:
$ cal (Command only)
$ cal 1987 (Command and argument)
$ date (Command only)
$ date -u (Command and option)
$ banner "hi there" (Command and argument)
Changing Directories Using the cd Command
Files you need to access are often stored in subdirectories below your home directory. Because of
the way in which a UNIX file system is set up, it is often desirable to change directory locations.
Use the cd command to change to a new current directory. This command, like all UNIX
commands, accepts both absolute and relative pathnames.
Command Format
cd [directory_name]
Moving Around the Directory Hierarchy
You can use cd with:
• An absolute pathname
$ cd /home
• A relative pathname
$ cd user2/dir1
Use the cd command without a directory name to return to your home directory.
$ cd
Displaying the Current Directory Using the pwd Command
Remembering which directory you are currently working in is often difficult. Use the pwd (print
working directory) command to display the absolute pathname of your current directory.
Command Format
pwd
Determining Your Current Directory
$ pwd
/home/user2
$

$ cd /home
$ pwd
/home
$
$ cd
$ cd practice
$ pwd
/home/user2/practice
$
 Directories always contain a link to their parent directory (..) and a link to themselves (.).
Pathname abbreviations are a form of shorthand when moving between or referring to
directories.
Table 5-1 Pathname Abbreviations
Symbol Meaning
. Current (working) directory
.. Parent directory; the directory directly above the current directory
~ User’s home directory (Korn and C shells)
~- The full path to the previous working directory (Korn shell only)
~ logname The home directory of the user specified by logname
 The symbol ~+ refers to the current working diretory ($PWD ). It is used mostly when
writing scripts.
Use pathname abbreviations with the cd command to move around the file structure. For
example:
$ pwd
/home/user2/dir1
$ cd ..
$ pwd
/home/user2
$ cd ../..
$ pwd

/
$ cd ~/practice
$ pwd
/home/user2/practice
$ cd ~-
$ pwd
/
$ cd ~guest
$ pwd
/home/guest
Using the ls Command
To determine the contents of a directory, use the ls command. This command will display a
listing of all files and directories within specified directories. If no pathname is given as an
argument, ls will display the contents of the current directory.
Command Format
ls [-option(s)] [pathname[s]]
Listing the Contents of a Directory
$ cd
$ pwd
/home/user2
$
$ ls
dante dir1 dir3 file1 file3 practice
dante_1 dir2 dir4 file2 file4
$
$ ls dir1
coffees fruit trees
$
$ ls /var/mail
user1 user2 user3 user4 user5

$
 Have the students try using absolute and relative pathnames.
Displaying Hidden Files
File names that begin with a dot (.) are called hidden files. Hidden files are frequently used
to customize a user’s work environment. They are not shown by default because they are
infrequently edited.
Use ls -a to list all files in a directory, including hidden (.) files.
Using ls to Display Hidden Files

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×