Exercise: Redirecting Output
Viewing Output
Use ls
(list files) and >
(redirect) to create a file named "mylist" which contains a list of your files.
There are three main methods for viewing a file from the command prompt. Try each of these on your "mylist" file to get a feel for how they work:
-
cat
shows the contents of the entire file at the terminal and scrolls automatically to the end.
$ cat mylist
more
shows the contents of the file, pausing when
it fills the screen.
$ more mylist
- Note that it reads the entire file before displaying, so it could take a long time to load for large files.
- Use the spacebar to advance one page at a time.
less
is similar to more
, but with more
features. It shows the contents of the file, pausing when it fills
the screen.
$ less mylist
-
Note that
less
is faster thanmore
on large files because it does not read the entire input file before displaying. - Use the spacebar to advance one page at a time, or use the arrow keys to scroll one line at a time. Enter q to quit. Entering g or G will take you to the beginning or end of the file, respectively.
-
You can also search within a file (similar to Vim) by
typing / and the word or characters you are searching
for (example: /foo will search for "foo").
less
will jump to the first match for the word. Move between matches by using n and ? keys.
It may also be useful to explore the man pages for head
and tail
and try them out, especially in conjunction with these viewing methods.
Combining Redirection and Viewing
Now let's try an exercise where we enter the famous quote "Four score and
seven years ago" from Lincoln's Gettysburg address into a file called "lincoln.txt".
First, use cat
to direct stdin to "lincoln.txt":
Next, enter the quote above. To end the text input, press Control-D.
Four score and seven years ago
[Control-D]
Finally, you can use cat
to view the file you just created:
$ cat lincoln.txt
Four score and seven years ago
Now try adding another line of the famous quote to the existing file:
If you wish, you could try appending the rest of the speech to the file.
Finally, try viewing the file in both more
and less
to test them out. Feel free to test navigation in both and try searching with
less
. If you have a longer file, try viewing that as well so
you can get used to scrolling.
Creating a Simple Script
We can also redirect input to a script
file that we create and then run the script. First, we will create the script
file called "tryme.sh" that contains the cat
command
without any arguments, forcing it to read from stdin.
The first line of the script #!/bin/sh
indicates which shell
interpreter to use. /bin/sh
is a special sort of file, called
a symlink, which points at the default interpreter. You can see where
it points by:
$ ls -l /bin/sh
The default is often /bin/bash
, but you can also specify to use
bash (or another shell) directly by replacing the line with the location of
bash on your system, which is usually #!/bin/bash
.
Next, we can execute the script using the source
command, and
redirect the "lincoln.txt" file to stdin. This will cause the script
to execute the cat
command with the contents of "lincoln.txt"
as input, consequently printing it to the screen (via stdout):
If you omit the redirection character <
, the script will try
to read from stdin (keyboard input) and then immediately print it back out.