Navigating the Linux File System
◾
79
ls file1.txt file2.txt file1.dat
Thus, enumerating the list in braces does not seem worthwhile. However, the power of
brace expansion comes when the items in braces are directories and subdirectories. If you
use this correctly, it saves you from having to type full paths to each directory.
Consider the directory structure shown in Figure 3.3. If
you wanted to perform some
operation, say ls, on all of the subdirectories of foxr, you could do so with the following
listing:
ls /home/foxr/temp /home/foxr/temp2 /home/foxr/temp2/a
/home/foxr/temp2/b
But this listing can be greatly simplified (reduced in size) using braces as shown here:
ls /home/foxr/{temp,temp2/{a,b}}
The above instruction is unfolded into the full list /home/foxr/temp, /home/foxr/temp2,
/home/foxr/temp2/a, /home/foxr/temp2/b, without the user having to do so. Through brace
expansion, you do not have to repeat all of the directory paths.
Through the use of .., ., ~, *, ?, [], and {}, the user is provided a number of very useful
shortcuts to simplify paths. Recall from Section 2.6.3 that the
Bash interpreter performs
several steps before executing an instruction. With respect to these symbols, Bash first
expands items found in braces. This is followed by replacing ~ with the appropriate home
directory name. Next, globbing,
or filename expansion, takes place replacing wildcards
with lists of matching files and directories. Finally, the instruction can be executed on the
resulting list(s) generated.
3.3 FILE SYSTEM COMMANDS
In this section, we examine many common Linux file system commands.
This section does
not include commands reserved for system administrators. Keep in mind that if you are
attempting to apply an instruction on an item owned by another user, the command may
bin
foxr
dukeg
temp
temp2
a
b
marst
zappaf
etc
home
usr
FIGURE 3.3
Example directory structure.
80
◾
Linux with Operating System Concepts
not work depending on that item’s permissions. We explore permissions in Section 3.5.
We also visit some more special purpose file system commands at the end of this chapter.
One aspect of Linux which is most convenient is that many
file system commands will
operate on different types of file system constructs. These include files, directories, sym-
bolic links, pipes, I/O devices, and storage devices. We discuss the difference between these
types in Chapter 10 although we also look at symbolic links in this chapter and pipes in
other chapters.
The
file
command will describe the type of entity passed to the command. For
instance,
file /etc/passwd
will tell us that this is ASCII text (a text file) while
file
/
will tell us that /
is a directory and
file /dev/sda1
will tell us that sda1 is a block
special device (a special file for a block device). Other types that might be output from the
file command include empty (an empty text file), a particular type of text file (e.g., C pro-
gram, bash shell script, Java program),
symbolic link, and character special (character-type
device). Using the -i option causes file to output the MIME type of the file as in
text/
plain
,
application/x-directory
,
text/x-shellscript
, and others.
Table 3.3 displays the commands we will cover in Sections 3.3.1 through 3.3.5, along
with a description of each command and the more common options. To gain a full under-
standing of each command, it is best to study the command’s main page.
3.3.1
Directory Commands
You will always want to know your current working directory. Without this knowledge,
you will not be able to use relative paths. The command
pwd
is used to print the current
working directory. It is one of the simplest commands in Linux as it has no options. The
command responds with the current working directory as output. For instance:
TABLE 3.3
Common Linux File Commands
Do'stlaringiz bilan baham: