Join Scott Simpson for an in-depth discussion in this video System basics: Bash output redirection, part of Linux Tips Weekly.
- [Instructor] When you're working at the Bash prompt, usually you type information in the command line; and you see a response when a command runs or returns an error message. But there's a little more to it than that. The command prompt represents the standard input, one of three special streams of data that a process has access to. Standard input is generally input from the keyboard. Another stream, the standard output, is what we see as the result of running programs in a text environment like a terminal; and there's another stream we see sometimes called the standard error. That stream carries exception messages from programs as they run.
By using special operators, you can change how the shell treats them. One of the most common uses for redirection is to take the standard output and send it somewhere else. We have two primary ways of doing this: with a pipe, which sends the output to another command; or with various other redirection operators, which send the output to a file. And in a little bit, we'll see how to redirect the standard input. Let's generate some text here with echo. I'll use a few brace expansions, and there's the output.
With a pipe character, we can send that instead to a program like wc, or WordCount; and that'll show us that we have one long line, 6,760 words, and 27,040 characters. To send the output to a file instead, I can use the greater than character and a file name. I don't get any response here at the command line because the standard output was redirected to that file, and there it is. Using one greater than sign replaces anything that's in the destination file with the output of a command.
Using two, like this, appends information to the end of a file rather than erasing the contents of or truncating the file. This is helpful to add things to a running log file, for example, or just to make sure that you're not accidentally eliminating data. In fact, we can use another redirection operator to send this output file to the WC command. I'll use WC and a less than symbol and the file name. Here I see I have two lines instead of just the one because we appended the output to the file rather than replacing it.
Of course, WC works without that redirection operator just the same too. I can also send text to a command with a double less than operator, which is called a here document. This one's a little bit different. I'll use WC again to demonstrate it. I'll write WC, two less than characters, and then EOF. The EOF here is a token, and it can be any text you want; but EOF is customary for end of file. Once I type that, I am prompted for input. I'll add a few lines, and then I'll write EOF; and Bash knows I'm finished with my input.
Then it sends the text that I've given it to the command: three lines, twelve words, 49 characters. So far we've seen redirection to and from the standard input and output, but we can also redirect the standard error. In order to work with that, we need to start being a little bit more specific about what we're doing with the redirection operators. Each of the streams is represented by a number: zero for standard input, one for standard output, and two for standard error. When we use redirection operators, it's kind of assumed that if we're taking output from something, it's redirecting the standard output; and if we're sending something to a command, we're redirecting input.
But bringing these numbers into the mix lets us be more precise, and it's required to work with error. Let's look at a command that I know will generate output to both standard output and standard error. I'll use the find command to look for something generic, let's say, Linux on the root file system. As a regular user, I don't have access to a lot of the places that branch off of the root; so I'll see errors for those. And where I do have access and where I find something that matches my search term, I'll get a path.
Lots of stuff, some errors, some matches. Now, I'll rewrite that command; but I'll redirect the standard output to a file called out and the errors to a file called errors. I'll use the greater than symbol for the standard output and send that to out, and then I'll use two and a greater than symbol to get the standard error and redirect that to error. Where I'm writing to the file called out, I could use one and then greater than to indicate the standard output should go there; but it's assumed.
I need to put two and the greater than sign to catch the errors though from stream two, and now I have two files. Let's take a look at the top of the output file, and let's take a look at the output of the error file. I can see that the resulting messages are filed correctly. I've used a similar command to make two files that record successful and unsuccessful attempts at copying many files off of a failing hard disk. Redirection pops up all over the place, and it's good to know how to get output where you need it.
Note: Because this is an ongoing series, there is no certificate of completion available for this course.