Home > Articles

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book

Standard Output and Input

Course Objectives Covered

  1. Executing Commands at the Command Line (3036)

  2. Common Command Line Tasks (3036)

  3. Piping and Redirection (3036)

Standard output is where displays usually go—to your screen. When you give the following command

ls -F

a listing of the subdirectories and files beneath the current directory is displayed on your screen. The default location for standard output, therefore, is to your screen. If you do not want the results displayed on your screen, however, you can redirect them to another location, such as a file. Redirection of standard output is possible through the use of the greater-than sign (>). For example, to send the results to a file, the command would be

ls -F > myfile

The first order of business the shell undertakes when given this command is that it creates a file named myfile with a size of zero. It then allows the ls utility to run and places the output within the newly created file rather than on your screen. It is important to note the order of operations. Suppose you give a nonexistent command, such as this one:

ls -z > myfile

The file named myfile is still created with a size of zero that then stays at zero. The error appears on your screen, but this is after the file was created. This is also important because if you attempt to add more information to the file, the file will be overwritten first. To add more information, you must append to the file using two greater-than signs (>>):

ls -l >> myfile

This adds the new information to the end of the existing file and keeps the original contents intact. In some situations, you want a command to run but don't care at all about what the results are. For example, I might want a database to compile and need to run a utility for this to happen. I don't care about the messages generated—I just want the utility to run. When this is the case, you can send the results to nowhere by specifying /dev/null:

ls -F > /dev/null

The results are sent to this device, which is also known as the trashcan, never to be saved or displayed.

Standard input is typically the keyboard, or interpreted to be among the arguments given on the command line. You can, however, specify redirection of standard input using the less-than sign (<). Rarely is there ever a need for this, but it is available. For example

cat myfile

will give the same results as

cat < myfile

In the world of Linux, numerical values exist for these items as well. Standard input (abbreviated stdin) is 0, and standard output (abbreviated stdout) is 1. These numbers can be used with the redirection, but this is rarely done. An example would be

ls -F 1> myfile

NOTE

There can be no space between 1 and the > sign. If there is, the meaning is changed.

This example states that standard output is to be redirected to the file named myfile. The numbers are important for one reason only—because a third possibility exists as well. Standard error (abbreviated stderr) has a value of 2. For an example of standard error, think of the ls -z command you saw earlier. Even though the output was being sent to a file, the command was a nonexistent one and the error message appeared on the screen. The error could be sent to the file with the following command:

ls -z 2> myfile

The problem is that now the error will be sent there, but the output (in the absence of an error) will appear on the screen. To send both output and errors to the same file, the command becomes

ls -z > myfile 2>&1

This states that the output is to go to a file named myfile and further that standard error (2) is to go to the same location as standard output (1). Another alternative is to send errors to one location and output to another, like this:

ls -z > myfile 2>errors

Let's look at the order of operations here: The shell first creates two files (myfile and errors) with zero sizes (whether or not they existed before). If the command is successful, the output goes to myfile. If the command is unsuccessful, the errors go to errors. If these were truly log files, the only other modification might be to append versus zero each time the operation is run:

ls -z >> myfile 2>>errors

tee for Two

There is a miscellaneous utility that really stands alone and does not fit well with any section: tee. This utility, as the name implies, sends output in two directions. The default for most processes is to write their output to the screen. Using redirection (>), you can divert the output to a file, but what if you want to do both?

The tee utility allows output to go to the screen and to a file as well. The utility must always be followed by the name of the file which you want output to write to, for example

$ ps -f | tee example
UID    PID PPID C STIME TTY     TIME CMD
root   19605 19603 0 Aug10 pts/0  00:00:34 bash
root   30089 19605 0 Aug20 pts/0  00:00:00 vi fileone
root   30425 19605 0 Aug20 pts/0  00:00:00 paste -d
  fileone filetwo?
root   32040 19605 0 Aug22 pts/0  00:00:00 cat
root   1183 19605 0 Aug23 pts/0  00:00:00 awk -F:
  questions
root   30778 19605 0 14:25 pts/0  00:00:00 ps -f
$
$ cat example
UID    PID PPID C STIME TTY     TIME CMD
root   19605 19603 0 Aug10 pts/0  00:00:34 bash
root   30089 19605 0 Aug20 pts/0  00:00:00 vi fileone
root   30425 19605 0 Aug20 pts/0  00:00:00 paste -d
  fileone filetwo?
root   32040 19605 0 Aug22 pts/0  00:00:00 cat
root   1183 19605 0 Aug23 pts/0  00:00:00 awk -F:
  questions
root   30778 19605 0 14:25 pts/0  00:00:00 ps -f
$

As illustrated, the output appears on the screen, and is written to the file as well. This can be an extremely useful utility whenever a file is needed and you also want to view the output.

xargs

Another powerful utility to know is xargs. To understand the power of this utility, consider the find utility, which is limited in the results it can return to values within the filesystem structure. The only real text within the structure is the name of the entity and not the data itself (the pointer points to that). To illustrate, suppose the user edulaney put out a memo several months back on acceptable use of the company refrigerator in the break room. Since then, user edulaney has quit the company and several new employees (who would benefit from knowing this policy) have joined. You want to find the file and reprint it.

About the closest you can get to accomplishing this with find (and its results) would be

$ find / -user edulaney -type f -exec grep -i refrigerator {}
  \;
stored in the refrigerator overnight will be thrown out 
$

The type f option must be used or else errors will occur every time grep tries to search a directory. In this case, the line from the file is found, but there is no way of knowing what file it is contained in—rendering the result pretty much useless.

Enter the xargs utility: Analogous to a pipe, it feeds output from one command directly into another. Arguments coming into it are passed through with no changes, and turned into input into the next command. Thus, the command can be modified to

$ find / -user edulaney -type f | xargs grep -i refrigerator
/home/edulaney/fileone:stored in the refrigerator overnight will be thrown out 
$

The desired file is indicated, and can now be found and printed for the new employees.

Food for thought: If xargs works like a pipe, why wouldn't the following command suffice?

$ find / -user edulaney -type f | grep -i refrigerator

Answer: Because the grep operation would take place on the names of the files, not the content of the files. The xargs utility pipes the entire name (thus the contents) into the next command in succession (grep, in this case), not just the resulting filename.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus