![]() |
Examine Unix files and Redirect Output | |
---|---|---|
© September 2, 1993 | B-7 |
How-to B-4, Create, Copy, Rename and Remove Unix Files and Directories, described how to create a simple file using the command cat (short for concatenate). The same command can be used to look at the contents of an existing file. To see what the file firstfile contains, type
cat firstfile
(Note that when using cat to examine a file, we do not include > as we did when creating a file. See the section below Redirecting output for information on >.)
The cat command displays the contents of the file on the screen. When you use cat to examine a very long file, the file contents will fly by until finished. To prevent this, type Control-s to stop the file's scrolling. When you are ready to see more, type Control-q to resume.
There is an easier way to browse through long files -- the command more will display one screenful of a file's contents, then wait.
more longfile
Line 1 of a long text file
line 2
. . .
line 11
line 12
etc
--More--(40%)
At the bottom of each screenful, more shows how much of the file has been displayed, calculated as a percentage of the whole file.
At this point, you can do one of the following:
You may need to examine only part of a file. The command
head longfile
will display the first 10 lines of the file longfile. Similarly,
tail longfile
will display the last 10 lines of the file.
Arguments may be added to the head and tail commands to change which lines are displayed.
head -40 longfile
will display the first 40 lines of the file called longfile.
tail -15 longfile
will display the last 15 lines of the file.
In order to skip to line 30 of the file, and display from that line to the end of the file, type
tail +30 longfile
Sometimes you are familiar with the contents of a file, and wish to display only a particular section. For example, assume that you have a file containing text and a section titled BIBLIOGRAPHY. In order to display only the portion of the file beginning with the word BIBLIOGRAPHY, use the more command as follows:
more +/BIBLIOGRAPHY longfile
more searches through the file until it finds BIBLIOGRAPHY, and displays the two lines before BIBLIOGRAPHY and the remainder of the file. Note that the case of the string being searched for is important. If more does not find the string, it will inform you "Pattern not found", then display the whole file.
more allows you to control quite precisely how you browse through a file. At the --More-- prompt type ? to see a list of controls for more. These controls are used at the --More-- prompt. Some of the most useful are:
f | skip forward 1 screenful of text
(2f skips forward 2 screenfuls, etc.) |
b or ctrl-B | skip backwards 1 screenful of text
(3b skips backward 3 screenfuls, etc.) |
= | display current line number |
/string | search for next occurrence of string |
You may want to see what sort of data a file contains without having to look at its contents. In particular, if a file is a compiled program, trying to display its contents can produce disconcerting results on your screen. Use the command file to determine the type of a file
file anyfile
Some possible file types are ASCII text, data, c-shell commands, commands text, compressed data, and directory.
Unix normally accepts input from the keyboard (the characters you type) and sends its output (the characters it produces) to the screen. The keyboard and the screen are termed the standard input and standard output devices.
A command can also have standard input and standard output. For example, the first section of this how-to describes using cat to look at the contents of a file. cat takes its input from the filename following it, and normally sends its output to the standard output device, the screen. Some commands do not require input. The date command, for example, requires no input, but sends its output to the screen.
In Unix it is possible to "redirect" output -- to take what the system would have displayed on the screen and put it in a file instead. For example, when we issue the ls command (see how-to B-5, List Contents and Navigate Unix Directories, for more information on ls) a list of our files normally appears on the screen. If we use a redirection symbol (>), we can have this list redirected into a file:
ls > myfilelist
When you redirect output to a file that already exists, any previous contents are deleted before the command is completed. To prevent the accidental overwriting of files, first issue the command
set noclobber
If you then type
ls > myfilelist
where myfilelist already exists, you will see the response
myfilelist: File exists.
However, if you are certain you want to replace the contents of an existing file with redirected output, use the emphatic form of the redirection command:
ls >! myfilelist
It is possible to append the redirected output onto the end of an existing file, instead of replacing the contents, by using the append symbol (>>). The following command adds the date to the end of the file myfilelist, without removing its original contents:
date >> myfilelist
If you have issued the set noclobber command to prevent accidental overwriting, you must use the emphatic form:
date >>! myfilelist
How-to B-14, Customize Your Unix Environment, explains how to have the set noclobber command issued automatically whenever you login to Unix.
Just as you can redirect the output of a command, you can also redirect a command's input. Using the symbol < allows you to specify a file as input for a command which usually takes its input from the keyboard. For example, the command
cat < firstfile
is a valid way to display the contents of the file firstfile on the screen, although
cat firstfile
produces the same result. (In this second case, the filename is used as an argument for the cat command.)
Two or more Unix commands can be strung together so that the output from one command becomes the input for the next. For example, suppose you want to print an alphabetically sorted list of currently logged-in users. The who command (described in how-to B-9, Find out about others using Unix and "talk" to them) lists who's logged in, and the lpr command prints a file.
Using input and output redirection you could accomplish this by typing
who > wholist
sort < wholist > whosorted
lpr whosorted
but it's much more efficient to pipe the commands together, as follows:
who | sort | lpr
Piping works by redirecting the standard output of one Unix command into the standard input of the next, as if the data were flowing down a pipeline. The pipe symbol is the vertical bar, "|" -- you'll see this technique used often in other how-tos.
Piping commands also avoids having unnecessary files created. However, if you had wanted to save the sorted list of users as well as printing it, you could still pipe the commands. In this case, put a "T" intersection in your pipe so that the data goes in two different directions:
who | sort | tee whosorted | lpr
The tee command saves a copy of the sorted list of users in the file whosorted and, at the same time, pipes it to the next command in the chain, lpr.