3 .\" Copyright (C) Caldera International Inc. 2001-2002.
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code and documentation must retain the above
10 .\" copyright notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed or owned by Caldera
17 .\" International, Inc.
18 .\" 4. Neither the name of Caldera International, Inc. nor the names of other
19 .\" contributors may be used to endorse or promote products derived from
20 .\" this software without specific prior written permission.
22 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 .\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
27 .\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 .\" POSSIBILITY OF SUCH DAMAGE.
35 .\" @(#)u2 8.1 (Berkeley) 6/8/93
40 Creating Files \(em The Editor
42 If you have to type a paper or a letter or a program,
43 how do you get the information stored in the machine?
44 Most of these tasks are done with
51 is thoroughly documented in
55 A Tutorial Introduction to the UNIX Text Editor,
56 we won't spend any time here describing how to use it.
57 All we want it for right now is to make some
60 (A file is just a collection of information stored in the machine,
61 a simplistic but adequate definition.)
66 with some text in it, do the following:
69 ed junk \fR(invokes the text editor)\f3
70 a \fR(command to ``ed'', to add text)\f3
73 whatever text you want ...
75 \&. \fR(signals the end of adding text)\f3
77 The ``\f3.\fR'' that signals the end of adding text must be
78 at the beginning of a line by itself.
80 for until it is typed,
83 commands will be recognized \(em
84 everything you type will be treated as text to be added.
86 At this point you can do various editing operations
87 on the text you typed in, such as correcting spelling mistakes,
88 rearranging paragraphs and the like.
89 Finally, you must write the information you have typed
90 into a file with the editor command
96 will respond with the number of characters it wrote
103 nothing is stored permanently,
104 so if you hang up and go home
105 the information is lost.\(dg
107 \(dg This is not strictly true \(em
108 if you hang up while editing, the data you were
109 working on is saved in a file called
111 which you can continue with at your next session.
115 the information is there permanently;
116 you can re-access it any time by typing
124 (If you try to quit without writing,
131 gets you out regardless.)
133 Now create a second file called
136 You should now have two files,
141 What files are out there?
145 (for ``list'') command lists the names
147 of any of the files that
159 which are indeed the two files just created.
160 The names are sorted into alphabetical order automatically,
161 but other variations are possible.
167 causes the files to be listed in the order in which they were last changed,
171 option gives a ``long'' listing:
175 will produce something like
177 -rw-rw-rw- 1 bwk users 41 Jul 22 2:56 junk
178 -rw-rw-rw- 1 bwk users 78 Jul 22 2:57 temp
180 The date and time are of the last change to the file.
181 The 41 and 78 are the number of characters
182 (which should agree with the numbers you got from
185 is the owner of the file, that is, the person
188 is the name of the file's group.
191 tells who has permission to read and write the file,
192 in this case everyone.
194 Options can be combined:
196 gives the same thing as
198 but sorted into time order.
199 You can also name the files you're interested in,
202 will list the information about them only.
203 More details can be found in
206 The use of optional arguments that begin with a minus sign,
211 is a common convention for
214 In general, if a program accepts such optional arguments,
215 they precede any filename arguments.
216 It is also vital that you separate the various arguments with spaces:
223 Now that you've got a file of text,
224 how do you print it so people can look at it?
225 There are a host of programs that do that,
226 probably more than are needed.
228 One simple thing is to use the editor,
229 since printing is often done just before making changes anyway.
236 will reply with the count of the characters in
238 and then print all the lines in the file.
239 After you learn how to use the editor,
240 you can be selective about the parts you print.
242 There are times when it's not feasible to use the editor for printing.
243 For example, there is a limit on how big a file
246 (several thousand lines).
249 will only print one file at a time,
250 and sometimes you want to print several, one after another.
251 So here are a couple of alternatives.
255 the simplest of all the printing programs.
257 simply prints on the terminal the contents of all the files
268 The files are simply concatenated (hence the name
273 produces formatted printouts of files.
277 prints all the files named in a list.
278 The difference is that it produces
279 headings with date, time, page number and file name
280 at the top of each page,
282 extra lines to skip over the fold in the paper.
290 then skip to the top of a new page and print
295 can also produce multi-column output:
302 You can use any reasonable number in place of ``3''
307 has other capabilities as well;
311 It should be noted that
316 a formatting program in the sense of shuffling lines around
317 and justifying margins.
318 The true formatters are
322 which we will get to in the section on document preparation.
324 There are also programs that print files
325 on a high-speed printer.
326 Look in your manual under
329 Shuffling Files About
331 Now that you have some files in the file system
332 and some experience in printing them,
333 you can try bigger things.
335 you can move a file from one place to another
336 (which amounts to giving it a new name),
341 This means that what used to be ``junk'' is now ``precious''.
350 Beware that if you move a file to another one
352 the already existing contents are lost forever.
358 of a file (that is, to have two versions of something),
365 makes a duplicate copy of
370 Finally, when you get tired of creating and moving
372 there is a command to remove files from the file system,
378 will remove both of the files named.
380 You will get a warning message if one of the named files wasn't there,
386 does its work silently.
387 There is no prompting or chatter,
388 and error messages are occasionally curt.
389 This terseness is sometimes disconcerting
391 but experienced users find it desirable.
395 So far we have used filenames without ever saying what's
397 so it's time for a couple of rules.
398 First, filenames are limited to 14 characters,
399 which is enough to be descriptive.\(dg
401 \(dg In 4.2 BSD the limit was extended to 255 characters.
403 Second, although you can use almost any character
405 common sense says you should stick to ones that are visible,
406 and that you should probably avoid characters that might be used
408 We have already seen, for example,
413 means to list in time order.
414 So if you had a file whose name
417 you would have a tough time listing it by name.
418 Besides the minus sign, there are other characters which
419 have special meaning.
422 use only letters, numbers and the period
423 until you're familiar with the situation.
425 On to some more positive suggestions.
426 Suppose you're typing a large document
428 Logically this divides into many small pieces,
429 like chapters and perhaps sections.
430 Physically it must be divided too,
433 will not handle really big files.
434 Thus you should type the document as a number of files.
435 You might have a separate file for each chapter,
443 Or, if each chapter were broken into several files, you might have
453 You can now tell at a glance where a particular file fits into the whole.
455 There are advantages to a systematic naming convention which are not obvious
459 What if you wanted to print the whole book?
462 pr chap1.1 chap1.2 chap1.3 ......
464 but you would get tired pretty fast, and would probably even make mistakes.
465 Fortunately, there is a shortcut.
472 means ``anything at all,''
473 so this translates into ``print all files
474 whose names begin with
476 listed in alphabetical order.
478 This shorthand notation
479 is not a property of the
482 It is system-wide, a service of the program
483 that interprets commands
486 Using that fact, you can see how to list the names of the files in the book:
499 is not limited to the last position in a filename \(em
501 and can occur several times.
506 removes all files that contain
510 as any part of their name.
513 by itself matches every filename,
518 prints all your files
519 (alphabetical order),
529 sure that's what you wanted to say!)
534 the only pattern-matching feature available.
535 Suppose you want to print only chapters 1 through 4 and 9.
542 means to match any of the characters inside the brackets.
543 A range of consecutive letters or digits can be abbreviated,
544 so you can also do this
549 Letters can also be used within brackets:
551 matches any character in the range
558 pattern matches any single character,
563 lists all files which have single-character names,
568 lists information about the first file of each chapter
575 is certainly the most useful,
576 and you should get used to it.
577 The others are frills, but worth knowing.
579 If you should ever have to turn off the special meaning
584 enclose the entire argument in single quotes,
589 We'll see some more examples of this shortly.
591 What's in a Filename, Continued
593 When you first made that file called
597 know that there wasn't another
600 especially since the person in the next office is also
601 reading this tutorial?
602 The answer is that generally each user
605 which contains only the files that belong to him.
606 When you log in, you are ``in'' your directory.
607 Unless you take special action,
608 when you create a new file,
609 it is made in the directory that you are currently in;
610 this is most often your own directory,
611 and thus the file is unrelated to any other file of the same name
612 that might exist in someone else's directory.
615 is organized into a (usually big) tree,
616 with your files located several branches into the tree.
617 It is possible for you to ``walk'' around this tree,
618 and to find any file in the system, by starting at the root
619 of the tree and walking along the proper set of branches.
620 Conversely, you can start where you are and walk toward the root.
622 Let's try the latter first.
623 The basic tools is the command
625 (``print working directory''),
626 which prints the name of the directory you are currently in.
628 Although the details will vary according to the system you are on,
632 it will print something like
636 This says that you are currently in the directory
638 which is in turn in the directory
640 which is in turn in the root directory
641 called by convention just
643 (Even if it's not called
646 you will get something analogous.
647 Make the corresponding mental adjustment and read on.)
653 you should get exactly the same list of file names
654 as you get from a plain
658 lists the contents of the current directory;
659 given the name of a directory,
660 it lists the contents of that directory.
666 This should print a long series of names,
667 among which is your own login name
671 is a directory that contains the directories
672 of all the normal users of the system,
675 The next step is to try
679 You should get a response something like this
680 (although again the details may be different):
689 This is a collection of the basic directories of files
693 we are at the root of the tree.
697 cat /usr/your\(hyname/junk
701 is still around in your directory).
704 /usr/your\(hyname/junk
709 you normally think of as ``junk''.
710 ``Pathname'' has an obvious meaning:
711 it represents the full name of the path you have to follow from the root
712 through the tree of directories to get to a particular file.
713 It is a universal rule in
717 that anywhere you can use an ordinary filename,
718 you can use a pathname.
720 Here is a picture which may make this clearer:
732 / | \e / | \e / | \e / | \e / | \e
746 is unrelated to Eve's.
748 This isn't too exciting if all the files of interest are in your own
749 directory, but if you work with someone else
750 or on several projects concurrently,
751 it becomes handy indeed.
752 For example, your friends can print your book by saying
754 pr /usr/your\(hyname/chap*
756 Similarly, you can find out what files your neighbor has
759 ls /usr/neighbor\(hyname
761 or make your own copy of one of his files by
763 cp /usr/your\(hyneighbor/his\(hyfile yourfile
766 If your neighbor doesn't want you poking around in his files,
768 privacy can be arranged.
769 Each file and directory has read-write-execute permissions for the owner,
770 a group, and everyone else,
778 As a matter of observed fact,
779 most users most of the time find openness of more
780 benefit than privacy.
782 As a final experiment with pathnames, try
786 Do some of the names look familiar?
787 When you run a program, by typing its name after the prompt character,
788 the system simply looks for a file of that name.
789 It normally looks first in your directory
790 (where it typically doesn't find it),
795 There is nothing magic about commands like
799 except that they have been collected into a couple of places to be easy to find and administer.
801 What if you work regularly with someone else on common information
803 You could just log in as your friend each time you want to,
805 ``I want to work on his files instead of my own''.
806 This is done by changing the directory that you are
809 cd /usr/your\(hyfriend
815 Now when you use a filename in something like
819 it refers to the file in your friend's directory.
820 Changing directories doesn't affect any permissions associated
822 if you couldn't access a file from your own directory,
823 changing to another directory won't alter that fact.
825 if you forget what directory you're in, type
831 It is usually convenient to arrange your own files
832 so that all the files related to one thing are in a directory separate
834 For example, when you write your book, you might want to keep all the text
835 in a directory called
845 then start typing chapters.
846 The book is now found in (presumably)
848 /usr/your\(hyname/book
850 To remove the directory
857 The first command removes all files from the directory;
859 removes the empty directory.
861 You can go up one level in the tree of files
867 is the name of the parent of whatever directory you are currently in.
871 for the directory you are in.
873 Using Files instead of the Terminal
875 Most of the commands we have seen so far produce output
877 some, like the editor, also take their input from the terminal.
881 that the terminal can be replaced by a file
882 for either or both of input and output.
887 makes a list of files on your terminal.
892 a list of your files will be placed in the file
895 will be created if it doesn't already exist,
896 or overwritten if it does).
899 means ``put the output on the following file,
900 rather than on the terminal.''
901 Nothing is produced on the terminal.
902 As another example, you could combine
903 several files into one by capturing the output of
912 operates very much like
916 ``add to the end of.''
926 to the end of whatever is already in
928 instead of overwriting the existing contents.
933 doesn't exist, it will be created for you.
935 In a similar way, the symbol
937 means to take the input
938 for a program from the following file,
939 instead of from the terminal.
940 Thus, you could make up a script of commonly used editing commands
941 and put them into a file called
943 Then you can run the script on a file by saying
947 As another example, you can use
949 to prepare a letter in file
951 then send it to several people with
953 mail adam eve mary joe <let
958 One of the novel contributions of
965 A pipe is simply a way to connect the output of one program
966 to the input of another program,
967 so the two run as a sequence of processes \(em
979 beginning each on a new page.
981 them run together instead.
988 but this is more work than necessary.
989 Clearly what we want is to take the output of
992 connect it to the input of
994 So let us use a pipe:
1001 take the output from
1003 which would normally have gone to the terminal,
1006 to be neatly formatted.
1008 There are many other examples of pipes.
1013 prints a list of your files in three columns.
1016 counts the number of lines, words and characters in
1017 its input, and as we saw earlier,
1019 prints a list of currently-logged on people,
1025 tells how many people are logged on.
1033 that reads from the terminal
1034 can read from a pipe instead;
1035 any program that writes on the terminal can drive
1037 You can have as many elements in a pipeline as you wish.
1041 programs are written so that they will take their input from one or more files
1042 if file arguments are given;
1043 if no arguments are given they will read from the terminal,
1044 and thus can be used in pipelines.
1055 in order in three columns.
1061 prints the information coming down the pipeline,
1067 We have already mentioned once or twice the mysterious
1071 The shell is the program that interprets what you type as
1072 commands and arguments.
1073 It also looks after translating
1076 into lists of filenames,
1082 into changes of input and output streams.
1084 The shell has other capabilities too.
1085 For example, you can run two programs with one command line
1086 by separating the commands with a semicolon;
1087 the shell recognizes the semicolon and
1088 breaks the line into two commands.
1093 does both commands before returning with a prompt character.
1095 You can also have more than one program running
1099 For example, if you are doing something time-consuming,
1100 like the editor script
1101 of an earlier section,
1102 and you don't want to wait around for the results before starting something else,
1107 The ampersand at the end of a command line
1108 says ``start this command running,
1109 then take further commands from the terminal immediately,''
1111 don't wait for it to complete.
1112 Thus the script will begin,
1113 but you can do something else at the same time.
1114 Of course, to keep the output from interfering
1115 with what you're doing on the terminal,
1116 it would be better to say
1118 ed file <script >script.out &
1120 which saves the output lines in a file
1124 When you initiate a command with
1127 replies with a number
1128 called the process number,
1129 which identifies the command in case you later want
1131 If you do, you can say
1133 kill process\(hynumber
1135 If you forget the process number,
1138 will tell you about everything you have running.
1139 (If you are desperate,
1141 will kill all your processes.)
1142 And if you're curious about other people,
1147 programs that are currently running.
1151 (command\(hy1; command\(hy2; command\(hy3) &
1153 to start three commands in the background,
1154 or you can start a background pipeline with
1156 command\(hy1 | command\(hy2 &
1159 Just as you can tell the editor
1160 or some similar program to take its input
1161 from a file instead of from the terminal,
1162 you can tell the shell to read a file
1164 (Why not? The shell, after all, is just a program,
1165 albeit a clever one.)
1166 For instance, suppose you want to set tabs on
1167 your terminal, and find out the date
1168 and who's on the system every time you log in.
1169 Then you can put the three necessary commands
1173 into a file, let's call it
1175 and then run it with
1179 This says to run the shell with the file
1182 The effect is as if you had typed
1187 If this is to be a regular thing,
1188 you can eliminate the
1191 simply type, once only, the command
1195 and thereafter you need only say
1199 to run the sequence of commands.
1202 command marks the file executable;
1203 the shell recognizes this and runs it as a sequence of commands.
1207 to run automatically every time you log in,
1208 create a file in your login directory called
1210 and place in it the line
1212 When the shell first gains control when you log in,
1215 file and does whatever commands it finds in it.\(dg
1217 \(dg The c shell instead reads a file called
1221 We'll get back to the shell in the section