next up previous contents
Next: Appending to a file Up: Files and the File Previous: Files and the File   Contents

Creating files: Using the command cat

To create a file you have two options; you may either use the command cat or an editor (usually ``visual'', i.e. screen-oriented) such as vi, emacs, or edt. Before you try the editors you should read up on some of the basic commands, so for the time being we will just use the cat command to create files. cat stands for ``concatenate and type'' (display).

Before we begin talking about cat, it is appropriate to describe a UNIX convention involving input to and output from commands. Everything you run under UNIX, whether it is a proper UNIX command or one of your own programs, has the concept of three special I/O units: ``standard input'', ``standard output'', and ``standard error''. By default, for interactive processes, these are your terminal. However, if you wish you can ``redirect'' this I/O to a file using special symbols. Redirecting standard input and standard output is done using < and > symbols respectively, so that they look like arrows pointing in the direction you want them to go. Details and examples will be given in many of the sample commands in this manual, particularly where it is very useful. Redirecting standard error is a little more complicated and will be discussed in the section about shells, below.

cat reads each file in a sequence and displays them on a specified output. If no files are given then cat reads from the standard input. Similarly, if no output file is given cat directs the data to the standard output. If the input is from the terminal then it is terminated by a ^D (control-D) on a line by itself. In UNIX, control-D is a generic end-of-file character; you can almost always use it to terminate input you are typing to any program.

The format for cat is:

cat input_files > output_file

The greater-than sign, >, redirects the output to the specified file or device instead of your terminal. This redirection works for the majority of UNIX commands, not just cat. Any time you want output from a command to go to a file instead of your screen, you can use this feature.

To create a file one could use the command:

malama% cat > filename

This command tells the computer to read data from the terminal (standard input, because no input file was named) and place the data in the file you have chosen. E.g.

malama% cat > sharky
Sun's going down, like a big bald head,
Disappearing behind the boulevard.
It's Sharky's night.
^D
malama%

The file ``sharky'' has now been created and contains the lines shown above. To type a file on the screen type cat followed by the file name. This command outputs the data to the screen (standard output) since no redirection is given.

malama% cat sharky
Sun's going down, like a big bald head,
Disappearing behind the boulevard.
It's Sharky's night.
malama%

In conclusion, using the command cat, you can take input from any file and direct it to any other file (or the screen). E.g.:

malama% cat night > day

The contents of the file ``night'' can now also be found in the file ``day''. Use the > sign with caution; if the output file is not empty, its prior contents will be erased and replaced with the contents of the input file. Your system may be set up such that an error message will be given in case you try to > into an existing file. Adding a ``!'' to a redirection (e.g. >!) will cause any existing file to be overwritten for sure.


next up previous contents
Next: Appending to a file Up: Files and the File Previous: Files and the File   Contents
h.stilmack@jach.hawaii.edu