LL4 – Redirection


As before, you are responsible for all previous Linux coursework and for researching the commands below in more depth using Wikipedia and the built in Linux man pages.  You will also be taking screen shots, pasting them into your LastnameFirstnameLL4.odt file and submitting (uploading) them in BB.

To begin, please navigate to your ciss100/FirstnameLastname directory you created in previous labs.

4.1 Redirection

Most processes initiated by Linux commands write to the standard output (that is, they write to the terminal screen), and many take their input from the standard input (that is, they read it from the keyboard). There is also the standard error, where processes write their error messages, by default, to the terminal screen.  So what do we have – input, processing and output – :).

We have already seen the use of the cat command to write the contents of a file to the screen.

Now type cat without specifing a file to read

$ cat

Then type a few words on the keyboard and press the [Enter] key.

Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to end the input.

What has happened?

If you run the cat command without specifing a file to read, it reads the standard input (the keyboard), and on receiving the’end of file’ (^D), copies it to the standard output (the screen).

In Linux, we can redirect both the input and the output of commands and this can be very useful.

4.2 Redirecting the Output

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 [Enter] after each one.

^D (Control D to stop)

What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1.  Now recall that Linux treats files and directories the same,  could the same be said for devices?  We will explore this later but if you are ready see here: http://www.cyberciti.biz/faq/understanding-unix-linux-bsd-device-files/

To read the contents of the file, type

$ cat list1 #of course you could have used less, more, head or tail to view the file

Create another file called list2 containing the following fruit: orange, plum, mango, grapefruit. Read the contents of list2 to verify your input and the file’s existence

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

^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 outputting the text to the file biglist

Exercise 4a

Display biglist on the terminal screen using a command other than “cat”, take a screen shot and paste this into your .odt file.

4.3 Redirecting the Input

We use the < symbol to redirect the input of a command.

The command sort alphabetically or numerically sorts a list. Type

$ sort

Then type in the names of some vegetables. Press [Return] after each one.

^D (control d to stop)

The output will be


Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type

$ sort < biglist

and the sorted list will be output to the screen.

To output the sorted list to a file, type,

$ sort < biglist > slist

Exercise 4b

Use a command other than “cat” and other than the command you used above to read the contents of the file slist. Take a screenshot and paste this into your .odt document.

4.4 Pipes

To see who is on the system with you, type

$ who

Now we only have 1 user on the system (us) in our VB Ubuntu instance but this would not be the case for production systems.  Over time we may want to see who is logged on a system at a particular point in time.  One method to get a sorted list of these names is to type,

$ who > names.txt
$ sort < names.txt

This is a bit slow and you should remember to remove the temporary file called names when you have finished (please do this). What you really want to do is connect the output of the who command directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe is the vertical bar |

For example, typing

$ who | sort

will give the same result as above, but will execute quicker and be cleaner.

To find out how many users are logged on, we could type

$ who | wc -l

Exercise 4c

 Using a pipe, sort a directory listing (i.e. list contents of directory and sort).  Take a screenshot and past this into your .odt file

Additional commands

Please research a2ps and lpr however we will not cover these here as we are not in a production network environment.



command > file redirect standard output to a file
command >> file append standard output to a file
command < file redirect standard input from a file
command1 | command2 pipe the output of command1 to the input of command2
cat file1 file2 > file0 concatenate file1 and file2 to file0
sort sort data
who list users currently logged in
a2ps -Pprinter textfile print text file to named printer
lpr -Pprinter psfile print postscript file to named printer

Leave a Reply