Partial example of a hierarchical file space.
Navigating the Linux File System
◾
75
A path is a description of how to reach a particular location in the file system. A path
is required if the Linux command operates on a file or directory that is not in the current
working directory. Paths can be expressed in one of two ways: absolute and relative.
An absolute path starts at the root level of the file system and works its way down the file
system’s hierarchy, directory by directory. The length of an absolute path is determined by the
number of directories/subdirectories that exist between the root level and the target file or
directory. Each new directory listed in a path is denoted by a slash (/) followed by the directory
name. Each directory/subdirectory listed in the path is separated by a /. As an example, the
passwd file is stored in the top-level directory etc. From root, this is denoted as /
etc/passwd
.
Consider a user, foxr, who has a directory in the top-level directory home, and has a sub-
directory, temp, which has a subdirectory, temp2, which contains the file foo.txt (see Figure
3.2). The absolute path to this file is /
home/foxr/temp/temp2/foo.txt
indicating a
greater depth than /
etc/passwd
.
Some commands operate on directories instead of files. In such a case, the / that follows
the directory name is not needed. For instance, /
home/foxr/temp
is acceptable although
/
home/foxr/temp/
is also acceptable. If the command is to operate on the items stored
in the directory, then the specification may look like this: /
home/foxr/temp/*
. The use
of * is a wildcard, as introduced in Chapter 2.
A relative path starts at the current working directory. The working directory is the
directory that the user last changed into (using the cd command). The initial working
directory upon logging in and opening a terminal window is either the user’s home direc-
tory (e.g., /home/foxr for user foxr) or the user’s Desktop directory (depending on how the
user opened the terminal window). Recalling foxr’s directory structure from Figure 3.2,
if foxr’s current directory is temp, then the file foo.txt is found through the relative path
temp2/foo.txt
. Notice that the subdirectory, temp2, is specified without a leading slash
(/). If you were to state /
temp2/foo.txt
, this would lead to an error because /temp2 is
an absolute reference meaning that temp2 is expected to be found among the top-level
directories. If you were in the directory /
etc
, then the file passwd is indicated by relative
path as just
passwd
.
foo.txt
foxr
temp
temp2
zappaf
waka_jawaka
passwd
etc
home
FIGURE 3.2
Example file space.
76
◾
Linux with Operating System Concepts
You can identify the current working directory in three ways: through the command
pwd
, through the variable PWD (for instance, by entering the command
echo $PWD
), or
by looking at the command line prompt (in most cases, the current working directory is
the last item in the prompt before the $).
3.2.2 Specifying Paths above and below the Current Directory
To specify a directory or file beneath the current directory, you specify a downward motion.
This is done by listing subdirectories with slashes between the directories. If you are using
a relative path, you do not include a leading /. For instance, if at /
home/foxr
, to access
foo.txt
, the path is
temp/temp2/foo.txt
.
To specify a directory above the current directory, you need to indicate an upward
motion. To indicate a directory above the current directory, use .
.
as in .
./foo.txt
which
means the file foo.txt is found one directory up. You can combine the upward and down-
ward specifications in a single path. Let us assume you are currently in /
home/foxr/
temp/temp2
and wish to access the file waka_jawaka in zappaf’s home directory. The
relative path is .
./../../zappaf/waka_jawaka
. The first .. moves you up from temp2
to temp. The second .. moves you from temp to foxr. The third .. moves you from foxr to
home. Now from /home, the rest of the path moves you downward into zappaf and finally
the reference to the file. In a relative path, you can use as many .. as there are between your
current directory and the root level of the file system. The absolute reference for the file
waka_jawaka is far easier to state: /
home/zappaf/waka_jawaka
.
As another example, if you are currently at /
home/foxr/temp/temp2
, to reach the
file /
etc/passwd
, you could specify absolute or relative paths. The absolute path is simply
/
etc/passwd
. The relative path is
../../../etc/passwd
.
To denote the current directory, use a single period (
.
). This is sometimes required when
a command expects a source or destination directory and you want to specify the current
working directory. We will see this when we use the move (mv) and copy (cp) commands.
If you wish to execute a script in the current working directory, you precede the script’s
name with a period as well.
As explained in Chapter 2, ~ is used to denote the current user’s home directory.
Therefore, if you are foxr,
~
means /
home/foxr
. You can also precede any user name
with ~ to indicate that user’s home directory so that
~zappaf
denotes /
home/zappaf
.
3.2.3 Filename Arguments with Paths
Many Linux file commands can operate on multiple files at a time. Such commands will
accept a list of files as its arguments. For instance, the file concatenation command, cat,
will display the contents of all of the files specified to the terminal window. The command
cat file1.txt file2.txt file3.txt
will concatenate the three files, displaying all of their content.
Navigating the Linux File System
◾
77
Each file listed in a command must be specified with a path to that file. If the file is in
the current working directory, the path is just the filename itself. Otherwise, the path may
be specified using the absolute pathname or the relative pathname.
Assume that file1.txt is under /home/foxr, file2.txt is under /home/foxr/temp, and file3.
txt is under /home/zappaf, and the current working directory is /home/foxr. To display
all three files using cat, you could use any of the following (along with other possibilities):
•
cat file1.txt temp/file2.txt
../zappaf/file3.txt
•
cat file1.txt /home/foxr/temp/file2.txt
/home/zappaf/file3.txt
•
cat ./file1.txt temp/file2.txt ../zappaf/file3.txt
Given a filename or a full path to a file, the instructions
basename
and
dirname
return the file’s name and the path, respectively. That is, basename will return just the
name of the file and dirname will return just the path to the file. If, for instance, we issue
the following two commands:
basename /etc/sysconfig/network-scripts/ifcfg-eth0
dirname /etc/sysconfig/network-scripts/ifcfg-eth0
we receive the values
ifcfg-eth0
and /
etc/sysconfig/network-scripts
, respec-
tively. For the commands
basename ~
and
dirname ~
, we receive
username
and /
home
, respectively where
Do'stlaringiz bilan baham: |