Creating Files \(em The Editor
If you have to type a paper or a letter or a program,
how do you get the information stored in the machine?
Most of these tasks are done with
is thoroughly documented in
A Tutorial Introduction to the UNIX Text Editor,
we won't spend any time here describing how to use it.
All we want it for right now is to make some
(A file is just a collection of information stored in the machine,
a simplistic but adequate definition.)
with some text in it, do the following:
ed junk \fR(invokes the text editor)\f3
a \fR(command to ``ed'', to add text)\f3
whatever text you want ...
\&. \fR(signals the end of adding text)\f3
The ``\f3.\fR'' that signals the end of adding text must be
at the beginning of a line by itself.
commands will be recognized \(em
everything you type will be treated as text to be added.
At this point you can do various editing operations
on the text you typed in, such as correcting spelling mistakes,
rearranging paragraphs and the like.
Finally, you must write the information you have typed
into a file with the editor command
will respond with the number of characters it wrote
nothing is stored permanently,
so if you hang up and go home
the information is lost.\(dg
\(dg This is not strictly true \(em
if you hang up while editing, the data you were
working on is saved in a file called
which you can continue with at your next session.
the information is there permanently;
you can re-access it any time by typing
(If you try to quit without writing,
gets you out regardless.)
Now create a second file called
You should now have two files,
What files are out there?
(for ``list'') command lists the names
which are indeed the two files just created.
The names are sorted into alphabetical order automatically,
but other variations are possible.
causes the files to be listed in the order in which they were last changed,
option gives a ``long'' listing:
will produce something like
-rw-rw-rw- 1 bwk 41 Jul 22 2:56 junk
-rw-rw-rw- 1 bwk 78 Jul 22 2:57 temp
The date and time are of the last change to the file.
The 41 and 78 are the number of characters
(which should agree with the numbers you got from
is the owner of the file, that is, the person
tells who has permission to read and write the file,
but sorted into time order.
You can also name the files you're interested in,
will list the information about them only.
More details can be found in
The use of optional arguments that begin with a minus sign,
is a common convention for
In general, if a program accepts such optional arguments,
they precede any filename arguments.
It is also vital that you separate the various arguments with spaces:
Now that you've got a file of text,
how do you print it so people can look at it?
There are a host of programs that do that,
probably more than are needed.
One simple thing is to use the editor,
since printing is often done just before making changes anyway.
will reply with the count of the characters in
and then print all the lines in the file.
After you learn how to use the editor,
you can be selective about the parts you print.
There are times when it's not feasible to use the editor for printing.
For example, there is a limit on how big a file
(several thousand lines).
will only print one file at a time,
and sometimes you want to print several, one after another.
So here are a couple of alternatives.
the simplest of all the printing programs.
simply prints on the terminal the contents of all the files
The files are simply concatenated (hence the name
produces formatted printouts of files.
prints all the files named in a list.
The difference is that it produces
headings with date, time, page number and file name
extra lines to skip over the fold in the paper.
then skip to the top of a new page and print
can also produce multi-column output:
You can use any reasonable number in place of ``3''
has other capabilities as well;
a formatting program in the sense of shuffling lines around
which we will get to in the section on document preparation.
There are also programs that print files
Look in your manual under
what equipment is attached to your machine.
Now that you have some files in the file system
and some experience in printing them,
you can try bigger things.
you can move a file from one place to another
(which amounts to giving it a new name),
This means that what used to be ``junk'' is now ``precious''.
Beware that if you move a file to another one
the already existing contents are lost forever.
of a file (that is, to have two versions of something),
makes a duplicate copy of
Finally, when you get tired of creating and moving
there is a command to remove files from the file system,
will remove both of the files named.
You will get a warning message if one of the named files wasn't there,
There is no prompting or chatter,
and error messages are occasionally curt.
This terseness is sometimes disconcerting
but experienced users find it desirable.
So far we have used filenames without ever saying what's
so it's time for a couple of rules.
First, filenames are limited to 14 characters,
which is enough to be descriptive.
Second, although you can use almost any character
common sense says you should stick to ones that are visible,
and that you should probably avoid characters that might be used
We have already seen, for example,
means to list in time order.
So if you had a file whose name
you would have a tough time listing it by name.
Besides the minus sign, there are other characters which
use only letters, numbers and the period
until you're familiar with the situation.
On to some more positive suggestions.
Suppose you're typing a large document
Logically this divides into many small pieces,
like chapters and perhaps sections.
Physically it must be divided too,
will not handle really big files.
Thus you should type the document as a number of files.
You might have a separate file for each chapter,
Or, if each chapter were broken into several files, you might have
You can now tell at a glance where a particular file fits into the whole.
There are advantages to a systematic naming convention which are not obvious
What if you wanted to print the whole book?
pr chap1.1 chap1.2 chap1.3 ......
but you would get tired pretty fast, and would probably even make mistakes.
Fortunately, there is a shortcut.
means ``anything at all,''
so this translates into ``print all files
listed in alphabetical order.
It is system-wide, a service of the program
Using that fact, you can see how to list the names of the files in the book:
is not limited to the last position in a filename \(em
and can occur several times.
removes all files that contain
as any part of their name.
by itself matches every filename,
sure that's what you wanted to say!)
the only pattern-matching feature available.
Suppose you want to print only chapters 1 through 4 and 9.
means to match any of the characters inside the brackets.
A range of consecutive letters or digits can be abbreviated,
Letters can also be used within brackets:
matches any character in the range
pattern matches any single character,
lists all files which have single-character names,
lists information about the first file of each chapter
is certainly the most useful,
and you should get used to it.
The others are frills, but worth knowing.
If you should ever have to turn off the special meaning
enclose the entire argument in single quotes,
We'll see some more examples of this shortly.
What's in a Filename, Continued
When you first made that file called
know that there wasn't another
especially since the person in the next office is also
The answer is that generally each user
which contains only the files that belong to him.
When you log in, you are ``in'' your directory.
Unless you take special action,
when you create a new file,
it is made in the directory that you are currently in;
this is most often your own directory,
and thus the file is unrelated to any other file of the same name
that might exist in someone else's directory.
is organized into a (usually big) tree,
with your files located several branches into the tree.
It is possible for you to ``walk'' around this tree,
and to find any file in the system, by starting at the root
of the tree and walking along the proper set of branches.
Conversely, you can start where you are and walk toward the root.
Let's try the latter first.
The basic tools is the command
(``print working directory''),
which prints the name of the directory you are currently in.
Although the details will vary according to the system you are on,
it will print something like
This says that you are currently in the directory
which is in turn in the directory
which is in turn in the root directory
called by convention just
you will get something analogous.
Make the corresponding changes and read on.)
you should get exactly the same list of file names
lists the contents of the current directory;
given the name of a directory,
it lists the contents of that directory.
This should print a long series of names,
among which is your own login name
is a directory that contains the directories
of all the normal users of the system,
You should get a response something like this
(although again the details may be different):
This is a collection of the basic directories of files
we are at the root of the tree.
cat /usr/your\(hyname/junk
is still around in your directory).
you normally think of as ``junk''.
``Pathname'' has an obvious meaning:
it represents the full name of the path you have to follow from the root
through the tree of directories to get to a particular file.
It is a universal rule in
that anywhere you can use an ordinary filename,
Here is a picture which may make this clearer:
/ | \e / | \e / | \e / | \e / | \e
This isn't too exciting if all the files of interest are in your own
directory, but if you work with someone else
or on several projects concurrently,
For example, your friends can print your book by saying
pr /usr/your\(hyname/chap*
Similarly, you can find out what files your neighbor has
or make your own copy of one of his files by
cp /usr/your\(hyneighbor/his\(hyfile yourfile
If your neighbor doesn't want you poking around in his files,
Each file and directory has read-write-execute permissions for the owner,
a group, and everyone else,
As a matter of observed fact,
most users most of the time find openness of more
As a final experiment with pathnames, try
Do some of the names look familiar?
When you run a program, by typing its name after the prompt character,
the system simply looks for a file of that name.
It normally looks first in your directory
(where it typically doesn't find it),
There is nothing magic about commands like
except that they have been collected into a couple of places to be easy to find and administer.
What if you work regularly with someone else on common information
You could just log in as your friend each time you want to,
``I want to work on his files instead of my own''.
This is done by changing the directory that you are
Now when you use a filename in something like
it refers to the file in your friend's directory.
Changing directories doesn't affect any permissions associated
if you couldn't access a file from your own directory,
changing to another directory won't alter that fact.
if you forget what directory you're in, type
It is usually convenient to arrange your own files
so that all the files related to one thing are in a directory separate
For example, when you write your book, you might want to keep all the text
then start typing chapters.
The book is now found in (presumably)
The first command removes all files from the directory;
removes the empty directory.
You can go up one level in the tree of files
is the name of the parent of whatever directory you are currently in.
for the directory you are in.
Using Files instead of the Terminal
Most of the commands we have seen so far produce output
some, like the editor, also take their input from the terminal.
that the terminal can be replaced by a file
for either or both of input and output.
makes a list of files on your terminal.
a list of your files will be placed in the file
will be created if it doesn't already exist,
or overwritten if it does).
means ``put the output on the following file,
rather than on the terminal.''
Nothing is produced on the terminal.
As another example, you could combine
several files into one by capturing the output of
to the end of whatever is already in
instead of overwriting the existing contents.
doesn't exist, it will be created for you.
In a similar way, the symbol
for a program from the following file,
instead of from the terminal.
Thus, you could make up a script of commonly used editing commands
and put them into a file called
Then you can run the script on a file by saying
As another example, you can use
to prepare a letter in file
then send it to several people with
mail adam eve mary joe <let
One of the novel contributions of
A pipe is simply a way to connect the output of one program
to the input of another program,
so the two run as a sequence of processes \(em
beginning each on a new page.
them run together instead.
but this is more work than necessary.
Clearly what we want is to take the output of
connect it to the input of
which would normally have gone to the terminal,
There are many other examples of pipes.
prints a list of your files in three columns.
counts the number of lines, words and characters in
its input, and as we saw earlier,
prints a list of currently-logged on people,
tells how many people are logged on.
that reads from the terminal
can read from a pipe instead;
any program that writes on the terminal can drive
You can have as many elements in a pipeline as you wish.
programs are written so that they will take their input from one or more files
if file arguments are given;
if no arguments are given they will read from the terminal,
and thus can be used in pipelines.
in order in three columns.
prints the information coming down the pipeline,
We have already mentioned once or twice the mysterious
The shell is the program that interprets what you type as
It also looks after translating
into changes of input and output streams.
The shell has other capabilities too.
For example, you can run two programs with one command line
by separating the commands with a semicolon;
the shell recognizes the semicolon and
breaks the line into two commands.
does both commands before returning with a prompt character.
You can also have more than one program running
For example, if you are doing something time-consuming,
and you don't want to wait around for the results before starting something else,
The ampersand at the end of a command line
says ``start this command running,
then take further commands from the terminal immediately,''
don't wait for it to complete.
Thus the script will begin,
but you can do something else at the same time.
Of course, to keep the output from interfering
with what you're doing on the terminal,
it would be better to say
ed file <script >script.out &
which saves the output lines in a file
When you initiate a command with
called the process number,
which identifies the command in case you later want
If you forget the process number,
will tell you about everything you have running.
will kill all your processes.)
And if you're curious about other people,
programs that are currently running.
(command\(hy1; command\(hy2; command\(hy3) &
to start three commands in the background,
or you can start a background pipeline with
command\(hy1 | command\(hy2 &
Just as you can tell the editor
or some similar program to take its input
from a file instead of from the terminal,
you can tell the shell to read a file
(Why not? The shell, after all, is just a program,
For instance, suppose you want to set tabs on
your terminal, and find out the date
and who's on the system every time you log in.
Then you can put the three necessary commands
into a file, let's call it
This says to run the shell with the file
The effect is as if you had typed
If this is to be a regular thing,
simply type, once only, the command
and thereafter you need only say
to run the sequence of commands.
command marks the file executable;
the shell recognizes this and runs it as a sequence of commands.
to run automatically every time you log in,
create a file in your login directory called
When the shell first gains control when you log in,
file and does whatever commands it finds in it.
We'll get back to the shell in the section