We use the >
symbol to redirect the output of a command. For example, to create a file
called list1 containing a list of fruit, type
%
cat > list1
Then type in the names
of some fruit. Press [Return] after each one.
pear
banana
apple
^D (Control D to stop)
What happens is the
cat command reads the standard input (the keyboard) and the >
redirects cat's output, which normally goes to the screen, into a file called
list1
To read the contents
of the file, type
%
cat list1
Exercise 3a
Using the above method,
create another file called list2 containing the following fruit:
orange, plum, mango, grapefruit. Read the contents of list2
The form >>
appends standard output to a file. So to add more items to the file list1,
type
%
cat >> list1
Then type in the names
of more fruit
peach
grape
orange
^D (Control
D to stop)
To read the contents
of the file, type
%
cat list1
You should now have
two files. One contains six fruit, the other contains four fruit. We will
now use the cat command to join (concatenate) list1 and list2
into a new file called biglist. Type
%
cat list1 list2 > biglist
What this is doing is
reading the contents of list1 and list2 in turn, then outputing
the text to the file biglist
To read the contents
of the new file, type
%
cat biglist