1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
|
.\" $OpenBSD: u2,v 1.2 2003/08/09 09:00:14 jmc Exp $
.\"
.\" Copyright (C) Caldera International Inc. 2001-2002.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code and documentation must retain the above
.\" copyright notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed or owned by Caldera
.\" International, Inc.
.\" 4. Neither the name of Caldera International, Inc. nor the names of other
.\" contributors may be used to endorse or promote products derived from
.\" this software without specific prior written permission.
.\"
.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.\" @(#)u2 8.1 (Berkeley) 6/8/93
.\"
.SH
II. DAY-TO-DAY USE
.SH
Creating Files \(em The Editor
.PP
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
the
.UC UNIX
``text editor''
.UL ed .
Since
.UL ed
is thoroughly documented in
.UL ed (1)
and explained in
.ul
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
.ul
files.
(A file is just a collection of information stored in the machine,
a simplistic but adequate definition.)
.PP
To create a file
called
.UL junk
with some text in it, do the following:
.P1
.ta .65i
ed junk \fR(invokes the text editor)\f3
a \fR(command to ``ed'', to add text)\f3
.ft I
now type in
whatever text you want ...
.ft 3
\&. \fR(signals the end of adding text)\f3
.P2
The ``\f3.\fR'' that signals the end of adding text must be
at the beginning of a line by itself.
Don't forget it,
for until it is typed,
no other
.UL ed
commands will be recognized \(em
everything you type will be treated as text to be added.
.PP
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
.UL w :
.P1
w
.P2
.UL ed
will respond with the number of characters it wrote
into the file
.UL junk .
.PP
Until the
.UL w
command,
nothing is stored permanently,
so if you hang up and go home
the information is lost.\(dg
.FS
\(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
.UL ed.hup ,
which you can continue with at your next session.
.FE
But after
.UL w
the information is there permanently;
you can re-access it any time by typing
.P1
ed junk
.P2
Type a
.UL q
command
to quit the editor.
(If you try to quit without writing,
.UL ed
will print a
.UL ?
to remind you.
A second
.UL q
gets you out regardless.)
.PP
Now create a second file called
.UL temp
in the same manner.
You should now have two files,
.UL junk
and
.UL temp .
.SH
What files are out there?
.PP
The
.UL ls
(for ``list'') command lists the names
(not contents)
of any of the files that
.UC UNIX
knows about.
If you type
.P1
ls
.P2
the response will be
.P1
junk
temp
.P2
which are indeed the two files just created.
The names are sorted into alphabetical order automatically,
but other variations are possible.
For example,
the command
.P1
ls -t
.P2
causes the files to be listed in the order in which they were last changed,
most recent first.
The
.UL \-l
option gives a ``long'' listing:
.P1
ls -l
.P2
will produce something like
.P1
-rw-rw-rw- 1 bwk users 41 Jul 22 2:56 junk
-rw-rw-rw- 1 bwk users 78 Jul 22 2:57 temp
.P2
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
.UL ed ).
.UL bwk
is the owner of the file, that is, the person
who created it.
.UL users
is the name of the file's group.
The
.UL \-rw\-rw\-rw\-
tells who has permission to read and write the file,
in this case everyone.
.PP
Options can be combined:
.UL ls\ \-lt
gives the same thing as
.UL ls\ \-l ,
but sorted into time order.
You can also name the files you're interested in,
and
.UL ls
will list the information about them only.
More details can be found in
.UL ls (1).
.PP
The use of optional arguments that begin with a minus sign,
like
.UL \-t
and
.UL \-lt ,
is a common convention for
.UC UNIX
programs.
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:
.UL ls\-l
is not the same as
.UL ls\ \ \-l .
.SH
Printing Files
.PP
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.
.PP
One simple thing is to use the editor,
since printing is often done just before making changes anyway.
You can say
.P1
ed junk
1,$p
.P2
.UL ed
will reply with the count of the characters in
.UL junk
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.
.PP
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
.UL ed
can handle
(several thousand lines).
Secondly,
it
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.
.PP
First is
.UL cat ,
the simplest of all the printing programs.
.UL cat
simply prints on the terminal the contents of all the files
named in a list.
Thus
.P1
cat junk
.P2
prints one file, and
.P1
cat junk temp
.P2
prints two.
The files are simply concatenated (hence the name
.UL cat '') ``
onto the terminal.
.PP
.UL pr
produces formatted printouts of files.
As with
.UL cat ,
.UL pr
prints all the files named in a list.
The difference is that it produces
headings with date, time, page number and file name
at the top of each page,
and
extra lines to skip over the fold in the paper.
Thus,
.P1
pr junk temp
.P2
will print
.UL junk
neatly,
then skip to the top of a new page and print
.UL temp
neatly.
.PP
.UL pr
can also produce multi-column output:
.P1
pr -3 junk
.P2
prints
.UL junk
in 3-column format.
You can use any reasonable number in place of ``3''
and
.UL pr
will do its best.
.UL pr
has other capabilities as well;
see
.UL pr (1).
.PP
It should be noted that
.UL pr
is
.ul
not
a formatting program in the sense of shuffling lines around
and justifying margins.
The true formatters are
.UL nroff
and
.UL troff ,
which we will get to in the section on document preparation.
.PP
There are also programs that print files
on a high-speed printer.
Look in your manual under
.UL lpr .
.SH
Shuffling Files About
.PP
Now that you have some files in the file system
and some experience in printing them,
you can try bigger things.
For example,
you can move a file from one place to another
(which amounts to giving it a new name),
like this:
.P1
mv junk precious
.P2
This means that what used to be ``junk'' is now ``precious''.
If you do an
.UL ls
command now,
you will get
.P1
precious
temp
.P2
Beware that if you move a file to another one
that already exists,
the already existing contents are lost forever.
.PP
If you want
to make a
.ul
copy
of a file (that is, to have two versions of something),
you can use the
.UL cp
command:
.P1
cp precious temp1
.P2
makes a duplicate copy of
.UL precious
in
.UL temp1 .
.PP
Finally, when you get tired of creating and moving
files,
there is a command to remove files from the file system,
called
.UL rm .
.P1
rm temp temp1
.P2
will remove both of the files named.
.PP
You will get a warning message if one of the named files wasn't there,
but otherwise
.UL rm ,
like most
.UC UNIX
commands,
does its work silently.
There is no prompting or chatter,
and error messages are occasionally curt.
This terseness is sometimes disconcerting
to new\%comers,
but experienced users find it desirable.
.SH
What's in a Filename
.PP
So far we have used filenames without ever saying what's
a legal name,
so it's time for a couple of rules.
First, filenames are limited to 14 characters,
which is enough to be descriptive.\(dg
.FS
\(dg In 4.2 BSD the limit was extended to 255 characters.
.FE
Second, although you can use almost any character
in a filename,
common sense says you should stick to ones that are visible,
and that you should probably avoid characters that might be used
with other meanings.
We have already seen, for example,
that in the
.UL ls
command,
.UL ls\ \-t
means to list in time order.
So if you had a file whose name
was
.UL \-t ,
you would have a tough time listing it by name.
Besides the minus sign, there are other characters which
have special meaning.
To avoid pitfalls,
you would do well to
use only letters, numbers and the period
until you're familiar with the situation.
.PP
On to some more positive suggestions.
Suppose you're typing a large document
like a book.
Logically this divides into many small pieces,
like chapters and perhaps sections.
Physically it must be divided too,
for
.UL ed
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,
called
.P1
chap1
chap2
.ft R
etc...
.P2
Or, if each chapter were broken into several files, you might have
.P1
chap1.1
chap1.2
chap1.3
\&...
chap2.1
chap2.2
\&...
.P2
You can now tell at a glance where a particular file fits into the whole.
.PP
There are advantages to a systematic naming convention which are not obvious
to the novice
.UC UNIX
user.
What if you wanted to print the whole book?
You could say
.P1
pr chap1.1 chap1.2 chap1.3 ......
.P2
but you would get tired pretty fast, and would probably even make mistakes.
Fortunately, there is a shortcut.
You can say
.P1
pr chap*
.P2
The
.UL *
means ``anything at all,''
so this translates into ``print all files
whose names begin with
.UL chap '',
listed in alphabetical order.
.PP
This shorthand notation
is not a property of the
.UL pr
command, by the way.
It is system-wide, a service of the program
that interprets commands
(the ``shell,''
.UL sh (1)).
Using that fact, you can see how to list the names of the files in the book:
.P1
ls chap*
.P2
produces
.P1
chap1.1
chap1.2
chap1.3
\&...
.P2
The
.UL *
is not limited to the last position in a filename \(em
it can be anywhere
and can occur several times.
Thus
.P1
rm *junk* *temp*
.P2
removes all files that contain
.UL junk
or
.UL temp
as any part of their name.
As a special case,
.UL *
by itself matches every filename,
so
.P1
pr *
.P2
prints all your files
(alphabetical order),
and
.P1
rm *
.P2
removes
.ul
all files.
(You had better be
.IT very
sure that's what you wanted to say!)
.PP
The
.UL *
is not
the only pattern-matching feature available.
Suppose you want to print only chapters 1 through 4 and 9.
Then you can say
.P1
pr chap[12349]*
.P2
The
.UL [...]
means to match any of the characters inside the brackets.
A range of consecutive letters or digits can be abbreviated,
so you can also do this
with
.P1
pr chap[1-49]*
.P2
Letters can also be used within brackets:
.UL [a\-z]
matches any character in the range
.UL a
through
.UL z .
.PP
The
.UL ?
pattern matches any single character,
so
.P1
ls ?
.P2
lists all files which have single-character names,
and
.P1
ls -l chap?.1
.P2
lists information about the first file of each chapter
.UL chap1.1 \&, (
.UL chap2.1 ,
etc.).
.PP
Of these niceties,
.UL *
is certainly the most useful,
and you should get used to it.
The others are frills, but worth knowing.
.PP
If you should ever have to turn off the special meaning
of
.UL * ,
.UL ? ,
etc.,
enclose the entire argument in single quotes,
as in
.P1
ls \(fm?\(fm
.P2
We'll see some more examples of this shortly.
.SH
What's in a Filename, Continued
.PP
When you first made that file called
.UL junk ,
how did
the system
know that there wasn't another
.UL junk
somewhere else,
especially since the person in the next office is also
reading this tutorial?
The answer is that generally each user
has a private
.IT directory ,
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.
.PP
The set of all files
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.
.PP
Let's try the latter first.
The basic tools is the command
.UL pwd
(``print working directory''),
which prints the name of the directory you are currently in.
.PP
Although the details will vary according to the system you are on,
if you give the
command
.UL pwd ,
it will print something like
.P1
/usr/your\(hyname
.P2
This says that you are currently in the directory
.UL your-name ,
which is in turn in the directory
.UL /usr ,
which is in turn in the root directory
called by convention just
.UL / .
(Even if it's not called
.UL /usr
on your system,
you will get something analogous.
Make the corresponding mental adjustment and read on.)
.PP
If you now type
.P1
ls /usr/your\(hyname
.P2
you should get exactly the same list of file names
as you get from a plain
.UL ls :
with no arguments,
.UL ls
lists the contents of the current directory;
given the name of a directory,
it lists the contents of that directory.
.PP
Next, try
.P1
ls /usr
.P2
This should print a long series of names,
among which is your own login name
.UL your-name .
On many systems,
.UL usr
is a directory that contains the directories
of all the normal users of the system,
like you.
.PP
The next step is to try
.P1
ls /
.P2
You should get a response something like this
(although again the details may be different):
.P1
bin
dev
etc
lib
tmp
usr
.P2
This is a collection of the basic directories of files
that
the system
knows about;
we are at the root of the tree.
.PP
Now try
.P1
cat /usr/your\(hyname/junk
.P2
(if
.UL junk
is still around in your directory).
The name
.P1
/usr/your\(hyname/junk
.P2
is called the
.UL pathname
of the file that
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
the
.UC UNIX
system
that anywhere you can use an ordinary filename,
you can use a pathname.
.PP
Here is a picture which may make this clearer:
.P1 1
.ft R
.if t .vs 9p
.if t .tr /\(sl
.if t .tr ||
.ce 100
(root)
/ | \e
/ | \e
/ | \e
bin etc usr dev tmp
/ | \e / | \e / | \e / | \e / | \e
/ | \e
/ | \e
adam eve mary
/ / \e \e
/ \e junk
junk temp
.ce 0
.br
.tr //
.P2
.LP
Notice that Mary's
.UL junk
is unrelated to Eve's.
.PP
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,
it becomes handy indeed.
For example, your friends can print your book by saying
.P1
pr /usr/your\(hyname/chap*
.P2
Similarly, you can find out what files your neighbor has
by saying
.P1
ls /usr/neighbor\(hyname
.P2
or make your own copy of one of his files by
.P1
cp /usr/your\(hyneighbor/his\(hyfile yourfile
.P2
.PP
If your neighbor doesn't want you poking around in his files,
or vice versa,
privacy can be arranged.
Each file and directory has read-write-execute permissions for the owner,
a group, and everyone else,
which can be set
to control access.
See
.UL ls (1)
and
.UL chmod (1)
for details.
As a matter of observed fact,
most users most of the time find openness of more
benefit than privacy.
.PP
As a final experiment with pathnames, try
.P1
ls /bin /usr/bin
.P2
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),
then in
.UL /bin
and finally in
.UL /usr/bin .
There is nothing magic about commands like
.UL cat
or
.UL ls ,
except that they have been collected into a couple of places to be easy to find and administer.
.PP
What if you work regularly with someone else on common information
in his directory?
You could just log in as your friend each time you want to,
but you can also say
``I want to work on his files instead of my own''.
This is done by changing the directory that you are
currently in:
.P1
cd /usr/your\(hyfriend
.P2
(On some systems,
.UL cd
is spelled
.UL chdir .)
Now when you use a filename in something like
.UL cat
or
.UL pr ,
it refers to the file in your friend's directory.
Changing directories doesn't affect any permissions associated
with a file \(em
if you couldn't access a file from your own directory,
changing to another directory won't alter that fact.
Of course,
if you forget what directory you're in, type
.P1
pwd
.P2
to find out.
.PP
It is usually convenient to arrange your own files
so that all the files related to one thing are in a directory separate
from other projects.
For example, when you write your book, you might want to keep all the text
in a directory called
.UL book .
So make one with
.P1
mkdir book
.P2
then go to it with
.P1
cd book
.P2
then start typing chapters.
The book is now found in (presumably)
.P1
/usr/your\(hyname/book
.P2
To remove the directory
.UL book ,
type
.P1
rm book/*
rmdir book
.P2
The first command removes all files from the directory;
the second
removes the empty directory.
.PP
You can go up one level in the tree of files
by saying
.P1
cd ..
.P2
.UL .. '' ``
is the name of the parent of whatever directory you are currently in.
For completeness,
.UL . '' ``
is an alternate name
for the directory you are in.
.SH
Using Files instead of the Terminal
.PP
Most of the commands we have seen so far produce output
on the terminal;
some, like the editor, also take their input from the terminal.
It is universal in
.UC UNIX
systems
that the terminal can be replaced by a file
for either or both of input and output.
As one example,
.P1
ls
.P2
makes a list of files on your terminal.
But if you say
.P1
ls >filelist
.P2
a list of your files will be placed in the file
.UL filelist
(which
will be created if it doesn't already exist,
or overwritten if it does).
The symbol
.UL >
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
.UL cat
in a file:
.P1
cat f1 f2 f3 >temp
.P2
.PP
The symbol
.UL >>
operates very much like
.UL >
does,
except that it means
``add to the end of.''
That is,
.P1
cat f1 f2 f3 >>temp
.P2
means to concatenate
.UL f1 ,
.UL f2
and
.UL f3
to the end of whatever is already in
.UL temp ,
instead of overwriting the existing contents.
As with
.UL > ,
if
.UL temp
doesn't exist, it will be created for you.
.PP
In a similar way, the symbol
.UL <
means to take the input
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
.UL script .
Then you can run the script on a file by saying
.P1
ed file <script
.P2
As another example, you can use
.UL ed
to prepare a letter in file
.UL let ,
then send it to several people with
.P1
mail adam eve mary joe <let
.P2
.SH
Pipes
.PP
One of the novel contributions of
the
.UC UNIX
system
is the idea of a
.ul
pipe.
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
a pipeline.
.PP
For example,
.P1
pr f g h
.P2
will print the files
.UL f ,
.UL g ,
and
.UL h ,
beginning each on a new page.
Suppose you want
them run together instead.
You could say
.P1
cat f g h >temp
pr <temp
rm temp
.P2
but this is more work than necessary.
Clearly what we want is to take the output of
.UL cat
and
connect it to the input of
.UL pr .
So let us use a pipe:
.P1
cat f g h | pr
.P2
The vertical bar
.UL |
means to
take the output from
.UL cat ,
which would normally have gone to the terminal,
and put it into
.UL pr
to be neatly formatted.
.PP
There are many other examples of pipes.
For example,
.P1
ls | pr -3
.P2
prints a list of your files in three columns.
The program
.UL wc
counts the number of lines, words and characters in
its input, and as we saw earlier,
.UL who
prints a list of currently-logged on people,
one per line.
Thus
.P1
who | wc
.P2
tells how many people are logged on.
And of course
.P1
ls | wc
.P2
counts your files.
.PP
Any program
that reads from the terminal
can read from a pipe instead;
any program that writes on the terminal can drive
a pipe.
You can have as many elements in a pipeline as you wish.
.PP
Many
.UC UNIX
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.
.UL pr
is one example:
.P1
pr -3 a b c
.P2
prints files
.UL a ,
.UL b
and
.UL c
in order in three columns.
But in
.P1
cat a b c | pr -3
.P2
.UL pr
prints the information coming down the pipeline,
still in
three columns.
.SH
The Shell
.PP
We have already mentioned once or twice the mysterious
``shell,''
which is in fact
.UL sh (1).
The shell is the program that interprets what you type as
commands and arguments.
It also looks after translating
.UL * ,
etc.,
into lists of filenames,
and
.UL < ,
.UL > ,
and
.UL |
into changes of input and output streams.
.PP
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.
Thus
.P1
date; who
.P2
does both commands before returning with a prompt character.
.PP
You can also have more than one program running
.ul
simultaneously
if you wish.
For example, if you are doing something time-consuming,
like the editor script
of an earlier section,
and you don't want to wait around for the results before starting something else,
you can say
.P1
ed file <script &
.P2
The ampersand at the end of a command line
says ``start this command running,
then take further commands from the terminal immediately,''
that is,
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
.P1
ed file <script >script.out &
.P2
which saves the output lines in a file
called
.UL script.out .
.PP
When you initiate a command with
.UL & ,
the system
replies with a number
called the process number,
which identifies the command in case you later want
to stop it.
If you do, you can say
.P1
kill process\(hynumber
.P2
If you forget the process number,
the command
.UL ps
will tell you about everything you have running.
(If you are desperate,
.UL kill\ 0
will kill all your processes.)
And if you're curious about other people,
.UL ps\ a
will tell you about
.ul
all
programs that are currently running.
.PP
You can say
.P1 1
(command\(hy1; command\(hy2; command\(hy3) &
.P2
to start three commands in the background,
or you can start a background pipeline with
.P1
command\(hy1 | command\(hy2 &
.P2
.PP
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
to get commands.
(Why not? The shell, after all, is just a program,
albeit a clever one.)
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
.UL tabs , (
.UL date ,
.UL who )
into a file, let's call it
.UL startup ,
and then run it with
.P1
sh startup
.P2
This says to run the shell with the file
.UL startup
as input.
The effect is as if you had typed
the contents of
.UL startup
on the terminal.
.PP
If this is to be a regular thing,
you can eliminate the
need to type
.UL sh :
simply type, once only, the command
.P1
chmod +x startup
.P2
and thereafter you need only say
.P1
startup
.P2
to run the sequence of commands.
The
.UL chmod (1)
command marks the file executable;
the shell recognizes this and runs it as a sequence of commands.
.PP
If you want
.UL startup
to run automatically every time you log in,
create a file in your login directory called
.UL .profile ,
and place in it the line
.UL startup .
When the shell first gains control when you log in,
it looks for the
.UL .profile
file and does whatever commands it finds in it.\(dg
.FS
\(dg The c shell instead reads a file called
.UL .login
.
.FE
We'll get back to the shell in the section
on programming.
|