USF Home
Administrators
Hardware Status





Search this site
 

Linux Tutorial: Chapter 3

4.0 Working with Input/Output

Up until now we have talked about using commands with parameters. The commands have not really needed much more than a few switches added to the end of them to have the program perform different tasks. There are however more ways of using commands together either through pipes or redirection of input and output that can help us get things done quicker and have output formatted the way that we want it to be. The options that you use in your programs will not always be the same and a lot of the time you will not know what you need to send a program as arguments to have it do what you want. In this chapter we will discuss a little about the different types of input and output, yes there are different kinds, and how to use them.

4.0.1 Input/Output Redirection

Let's say that you would like to have a list of all the files in a certain directory stored in a text file. How would you do that without opening a text editor and manually typing the name of each file into the editor? There is a much quicker way to accomplish this task.

Before we go on, I'd like to talk a little about input and output. All the output that you have seen so far has been the results of commands that get printed to the screen. There are other ways of using output, such as redirecting the output to a file or program. The type of input that we have used in our commands/programs has been from the keyboard, we can also get input from other places. Sure just using standard input and output is great and all, but it gets a little annoying if the size of the input is to big to type in to the keyboard all at once. This is where someone got the great idea of allowing us to redirect output and input to and from files. Using this method allows us to get information from a file and have the program use it and also allows us to save output for later use.

Linux commands use three different types of I/O streams to do everything. These three types of I/O are called stdin, stdout, and stderr.

stdin: This is the basic input stream, information received by the command. Usually it is from the keyboard.

stdout: This is the basic output stream, information sent by the command. stdout usually is to the monitor.

stderr: This is the error stream, error messages are usually sent to the monitor.
So let's continue with our problem from above. To store the directory listing in a file all we have to do is redirect the output of the ls command to a file. The file name can be what ever we want it to be and will be created after the ls is finished and ready to output its results. See this example:
[bob@host ~] ls -al
total 14
drwxr-xr-x 4 bob other 512 May 2 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
[bob@host ~] ls -al > list.txt
[bob@host ~] ls -al
total 16
drwxr-xr-x 4 bob other 512 May 2 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 1 bob other 485 May 2 11:45 list.txt
[bob@host ~]
You won't see any output from the command, that is because we redirected the output to a file instead of the screen. The > in the command means to redirect the output. If you want to see the listing that we created you can type 'cat list.txt', this will print the contents of list.txt to the screen.
[bob@host ~] cat list.txt
total 14
drwxr-xr-x 4 bob other 512 May 2 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 1 bob other 0 May 2 11:45 list.txt
[bob@host ~]
Let's do a little example on input redirection. We have a list of names stored in a file that we would like to have sorted in alphabetical order. We can sort the list using the sort command and input redirection. The sort command already has an option to accept a file to sort, but if the programmer had not included this we could still get the same effect using the input redirect. Let's see an example...
[bob@host ~] cat names.txt
Dustin
Brian
Carly
Shannon
Anna
Carly
Courtney
John
[bob@host ~]
Here we have the unsorted file, now lets use sort on it.
[bob@host ~] sort < names.txt
Anna
Brian
Carly
Carly
Courtney
Dustin
John
Shannon
[bob@host ~]
And there we go, it prints the list of names from the file in order. Let's try combining the input and output redirection by using the unsorted names file and output the sorted list of names to a file.
[bob@host ~] ls -al
total 16
drwxr-xr-x 4 bob other 512 May 2 13:02 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 1 bob other 52 May 2 12:44 names.txt
[bob@host ~] sort < names.txt > sorted.txt
[bob@host ~] ls -al
total 18
drwxr-xr-x 4 bob other 512 May 2 13:02 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 1 bob other 52 May 2 12:44 names.txt
-rw-r--r-- 1 bob other 52 May 2 13:02 sorted.txt
[bob@host ~] cat sorted.txt
Anna
Brian
Carly
Carly
Courtney
Dustin
John
Shannon
[bob@host ~]
There are more redirectors that you can use here is a list of them.
> fileRedirects standard output stream to file.
2> fileRedirects standard error stream to file.
>> fileRedirects standard output stream to file, appending output to the file if the file already exists.
2>> fileRedirects standard error stream to file, appending output to the file if the file already exists.
&> fileRedirects standard output and error streams to the file.
< fileRedirects standard input stream to the file.
<< textReads standard input until a line matching text is found, at which point end of file is posted.

4.0.2 What are pipes?

Linux gives us the nice functionality of pipes. Pipes let us join two or more commands together by taking the output from one command and using it as the input for another one. One way of thinking about pipes is that you put something into the end of the pipe and you get something else from the other end.

For example, have you ever tried to view the files in a directory and it didn't all fit on the screen at one time? Wouldn't it be nice if you could have the listing stop so you could scroll through it? Well you can if you use pipes. Instead of just typing ls -al /etc:

ls -al /etc | less
What this does is the ls command is executed and then the output is sent to a program called less instead of just being sent to standard output (the screen). Each command is separated using the '|' key. The less program takes input and then outputs to the screen one page at a time. Now we can hit the spacebar to view the output page by page, we can even scroll back and forth using the arrow keys. Just enter 'Q' when you want to quit.

Lets say that we have a directory filled with various types of files and we just want to get a list of all the files that end in '.txt' and then have them sorted. We can use pipes to do this, here is the command that we would type:
ls -la | grep '.txt' | sort
In this example what happened is the ls command passed its output to the grep command and then grep passed its output to the sort command. grep is a program that searches through given input for a specific pattern, in this case the pattern was .txt, giving us a list of all the files that end in .txt. We then pass that list to the sort command to put everything in order, and then we get the final output to the screen.

There are many other ways of using pipes and you will probably find them as you continue to use Linux.

Take Chapter Quiz


 



Copyright © 2008, Research Computing, USF, 4202 E. Fowler Avenue, Tampa, FL 33620