This chapter provides an introduction to files, file systems, and text editors. A file is a collection of data stored together in the computer. Typical files contain memos, reports, correspondence, programs, or other data. A file system is the useful arrangement of files into directories.
A text editor is a program that allows you to create new files and modify existing ones.
After completing this chapter, you will be able to:
This knowledge can help you design a file system that is appropriate for the type of information you use and the way you work.
An editor is a program that allows you to create and change files containing text, programs, or other data. An editor does not provide the formatting and printing features of a word processor.
With a text editor, you can:
Your editing takes place in an edit buffer that you can save or discard.
The vi and ed text editing programs are available on the operating system. Each editor has its own methods of displaying text as well as its own set of subcommands and rules.
For information about vi, read the next section and Appendix A. For information about ed, see Appendix B.
Your system may contain additional editors; see your system administrator for details.
This section shows how to create three files with the vi text editor.
The goal of this section is to have you create, using a minimal set of commands, files that can be used for working through the examples later in this book. For more information about vi, see Appendix A and the vi(1) reference page.
Note
If you are familiar with a different editing program, you can use that program to create the three sample files described in this section. If you have already created three files with an editing program, you can use those files by substituting their names for the filenames used in the examples.
When following the steps that are used to create the sample
files, only enter the text that is shown in
boldface
characters.
System prompts and output are shown in a different typeface,
like this.
To create three sample files, follow these steps:
$
vi file1
[Return]:
This is a new file, so the system responds by putting your cursor
at the top of a screen:
Note the blank lines on your screen that begin with a
tilde (~).
These tildes indicate the lines that contain no text.
Because you have not entered any text, all lines begin with a tilde.
You start the vi program by entering
[Return]
Note
Depending upon how your terminal or workstation is set up,
the Escape key may be programmed to perform a different function.
It is possible that one of the function keys on your keyboard may
have been set up to perform the "escape" function.
See your system administrator if your Escape key does not operate
properly.
The colon is displayed as a prompt at the bottom of the
screen as follows:
Your screen will look like this:
The system displays the name of the new file as well as the
number of lines and characters it contains.
The system is still in the
vi
text editor so you can create two more sample files.
The process is the same as the
one you used to create
file1,
but the text you enter will be different.
The system responds with a screen that looks
like this:
The message
file2 No such file or directory
indicates that
file2
is a new file.
If you have created a new file, you will find
You will find that vi is a useful
The
wq
command writes the file, quits (or exits) the editor, and returns
you to the shell prompt.
A
file
is a collection of data stored in a computer. A
file stored in a computer is like a document stored in a filing cabinet
because you can retrieve it, open it, process it, close it, and
store it as a unit. Every computer file has a
filename
that both users and the system use to refer to the file.
A
file system
is the arrangement of files into a useful pattern.
Any time you organize information, you
create something like a computer file system. For example, the
structure of a
manual file system (file cabinets, file drawers, file folders,
and documents)
resembles the structure of a computer file system. (The software that
manages the file storage is also known as the file system, but that
usage of the term does not occur in this chapter.)
Once you have organized your file system (manual or computer), you
can find a
particular piece of information quickly because you understand
the structure of
the system. To understand the file system, you should
first become
familiar with the following three concepts:
A file can contain the text of a document, a computer program,
records for a
general ledger, the numerical or statistical output of a computer
program, or other data.
A file name can contain any character except the following because
these characters have special meaning to the shell:
You may use a period or dot (.)
in the middle of a filename, but never at the beginning of the
filename unless you want the file to be
"hidden" when doing a simple listing of files.
For
information about characters with special meanings to your shell,
refer to the sections about metacharacters in
Chapter 8.
For information about listing hidden files, see
Section 3.1.3.
Note
Unlike some operating systems,
this operating system distinguishes
between uppercase and
lowercase letters in filenames (that is, it is case sensitive). For
example, the following three filenames represent three distinct files:
filea,
Filea,
and
FILEA.
Use filenames that reflect the actual contents of your
files. For example, a filename such as
memo.advt
might indicate that the
file contains a memo about advertising. On the other
hand, filenames such as
filea,
fileb,
or
filec
tell you nothing about the contents of that file.
It is also a good idea to use a consistent pattern to name related
files. For
example, suppose you have an advertisting report that is divided
into chapters, with each
chapter contained in a separate file. You might name these files in the
following way:
chap1.advt
The maximum length of a filename depends upon the file system used on
your operating system. For example, your file system may allow a
maximum filename
length of 255 characters (the default), or it may allow
a maximum filename length of only 14 characters. Because knowing the
maximum filename length is
important to name files with meaningful file names, see your system
administrator for details.
You can organize your files into groups and
subgroups that resemble the cabinets, drawers, and folders in a
manual file system.
These groups are called
directories,
and the subgroups are
called
subdirectories.
A well-organized system of directories and
subdirectories lets you retrieve and manipulate the data in your files
quickly.
Directories differ from files in two significant ways:
When you first log in, the system automatically places you in your
login
directory.
This directory was created for you when your computer account
was established. However, a file system in which all files are
arranged under
your login directory is not necessarily the most efficient method to
organize your files.
As you work with the system, you may want to set up
additional directories and
subdirectories so you can organize your files into useful groups.
For example,
assume that you work for the Sales department and are responsible for
four lines of automobiles.
You may want to create a subdirectory under your login directory
for each automobile line. Each subdirectory can contain all memos,
reports, and sales figures applicable for the automobile model.
Once your files are arranged into a directory structure that you
find useful, you can move easily between directories.
See
Chapter 4
for information about creating directories and moving between them.
The directory in which you are working at any given time is
your
current,
or working directory.
Whenever you are uncertain about the directory in which you are working
or where that
directory exists in the file system, enter the
pwd
(print working directory)
command as follows:
$
pwd
The system displays the name of your current directory in the
format:
This information
indicates that you are currently working in a directory named
msg
that is located under the
usr
directory.
The
/usr/msg
notation is known as the
pathname
of your working directory. See the following section for information
about pathnames.
The files and directories in the
file system are arranged hierarchically
in a structure that resembles an upside-down tree with the roots
at the top
and the branches at the bottom. This arrangement is called a
tree
structure. You can find more detailed information about
the directory structure in the
hier(5)
reference page.
Figure 2-1
shows a typical file system arranged in a tree
structure. The names of directories are printed in bold, and the
names of files are printed in italics.
A higher level directory is frequently called a
parent
directory.
For example, in
Figure 2-1,
the directories
plans,
report,
and
payroll
all have
chang
as their parent directory.
A
pathname
specifies the location of a directory or a file within
the file system. For example, when you want to change from
working on File A in
Directory X to File B in Directory Y, you enter the pathname to
File B. The
operating system then uses this pathname to search
through the file system until it locates File B.
A pathname consists of a sequence of directory names separated
by slashes
(/) that ends with a directory name or a filename. The first
element in a pathname specifies where the system is to begin searching,
and the
final element specifies the target of the search. The following
pathname is based on
Figure 2-1:
The first slash (
/)
represents the root directory and indicates the starting
place for the search. The remainder of the pathname indicates
that the search is to go to the
user
directory, then to the
chang
directory,
next
to the
report
directory, and finally to the
part3
file.
Whether you are changing your current directory, sending data to a
file, or
copying or moving a file from one place in your file system to
another, you use
pathnames to indicate the objects you want to manipulate.
A pathname that starts with a slash (
/)
(the symbol representing the root directory) is called a
full pathname
or an
absolute pathname.
You can also think of a
full pathname as the complete name of a file or a directory.
Regardless of
where you are working in the file system, you can always find a file
or a directory by specifying its full pathname.
The file system also lets you use
relative pathnames.
Relative pathnames do not begin with the
//
that represents the root directory because they are relative
to the current directory.
You can specify a relative pathname in one of several ways:
Every directory contains at least two entries:
..
(dot dot), and
.
(dot, which refers to the current directory).
In
Figure 2-2,
for example, if your current directory is
chang,
the relative pathname for the file
1Q
in the
contract
directory is
payroll/contract/1Q.
By comparing this relative pathname with the full pathname for the same file,
/user/chang/payroll/contract/1Q,
you can see that using relative
pathnames means less typing and more convenience.
In the C shell and the Korn shell, you may also use a tilde (~)
at the beginning of relative pathnames. The tilde character
specifies a user's login (home) directory.
For example, to specify your own login directory, use the tilde alone.
To specify the login directory of user
chang,
specify
~chang.
For more information on using relative pathnames, see
Chapter 4.
Note
If there are other users on your system, you may or
may not be able to get to their files and directories, depending
upon the permissions set for them. For more information about
file and directory permissions, see
Chapter 5.
In addition, your system may contain enhanced security features
that may affect access to files and directories.
If so, see your system administrator for details.
Commands often take filenames as arguments. To use several different
filenames as arguments to a command, you can type out the full name of
each file, as the following example shows:
$
ls file1 file2 file3
However, if the filenames have a common pattern (in this example, the
file
prefix), the shell can match that pattern, generate a
list of those names, and automatically pass them to the command
as arguments.
The asterisk (*), sometimes referred to as a wildcard,
matches any string of characters.
In the following example, the
ls
command finds the name of every text file in the current directory
that includes the
file
prefix:
$
ls file*
The
file*
matches any filename that begins with
file
and ends with any other character string.
The shell passes every filename that matches
this pattern as an argument for the
ls
command.
Thus, you do not have to enter (or even remember) the full name
of each file in
order to use it as an argument. Both commands
(ls
with all filenames typed out and
ls file*)
do the
same thing - they pass all files with the
file
prefix in the directory as arguments to the
ls
command.
There is one exception to the general rules for pattern matching.
When the
first character of a filename is a period, you must match the period
explicitly. For example,
ls *
displays the names of all files
in the current directory that do not begin with a period. The command
ls -a
displays all filenames that
begin with a period.
This restriction prevents the shell from automatically matching
the relative directory names. These are
.
(called dot, standing for the current
directory) and
..
(called dot dot, standing for the
parent directory). For more information on relative directory names, see
Chapter 4.
If a pattern does not match any filenames, the shell displays a
message informing you that no match has been found.
In addition to the asterisk (*), operating system
shells provide other ways
to match character
patterns. The following list summarizes all pattern-matching
characters and provides examples.
Because this operating system is an internationalized
operating system, it provides
the following additional pattern-matching features:
For more information on internationalized pattern-matching
characters, see
the
grep(1)
reference page. For more information on the operating system's
internationalization features, see
Appendix C.
~
~
~
~
~
~
"file1" [New file]
the vi command optionally followed by the name
[Return]
of a new or existing file.
[Escape]
~
~
~
~
~
~
"file1" [New file]
You start the vi program by entering
the vi command optionally followed by the name
of a new or existing file.
~
~
~
~
~
~
:
You start the vi program by entering
the vi command optionally followed by the name
of a new or existing file.
~
~
~
~
~
~
"file1" [New file] 3 lines, 111 characters
~
~
~
~
~
~
~
"file2" No such file or directory
that it is easy to add text.
If you have created a new file, you will find
that it is easy to add text.
~
~
~
~
~
~
~
"file2" [New file] 2 lines, 75 characters
editor that has many features.
2.3 Understanding Files, Directories, and Pathnames
2.3.1 Files and Filenames
chap2.advt
chap3.advt
2.3.2 Directories and Subdirectories
2.3.3 Displaying the Name of Your Current (Working) Directory (pwd)
/usr/msg
2.3.4 The Tree-Structure File System and Pathnames
Figure 2-1: A Typical Digital UNIX File System
/user/chang/report/part3
Figure 2-2: Relative and Full Pathnames
2.4 Specifying Files with Pattern Matching