Linux Administration Bootcamp
Installing and Connecting to a Linux System
Intermediate Linux Skills
Linux Boot Process and System Logging
Disk Management in Linux
LVM - The Logical Volume Manager
User Management in Linux
Networking With Linux
Shell Scripting with Linux
Command Line Kung Fu
In this course, you will learn how to install a Linux system and connect to it, whether that be on Mac or Windows.
In this lesson, we'll be talking about the different types of I/O; input and output and how to redirect that I/O. There are three default types of input and output. They are standard input, standard output and standard error. By default, standard input comes from the keyboard and standard output and standard error are displayed to the screen.
Each one of these I/O types is given a file descriptor. File descriptors are just numbers that represent open files. For us humans it's easier to reference files by name, but it's easier for computers to reference them by number. You might be thinking, my keyboard is in the file now where's my screen? On one level, that's true, but on another level it's not. Linux represents practically everything as a file. This abstraction allows you to do some powerful things like take the standard output of one command that would normally be displayed to your screen and use it as the input to another command or even redirect that output to a file.
Use the greater than sign to redirect output and the less than sign to redirect input. The greater than sign redirects output from a command to a file, while the less than sign redirects input from a file to a command. The explicit way of using redirection is to provide a file descriptor number. However, if one is omitted then the file descriptor zero is assumed for input, and one for output redirection.
When you use two greater than signs the redirected output will append to the file. Normally with redirection a file follows the redirection operator. If you want to use a file descriptor instead of a file name, you use the ampersand symbol. So instead of redirecting standard error to a file with two greater than out.err, for instance you could redirect it to standard output with two greater than ampersand one. This effectively combines standard error and standard output.
If you want to ignore output, you can send it to the null device, which is dev/null. The null device is a special file that throws away whatever is fed to it. You may hear people refer to this as a Bitbucket. If you don't wanna see errors on your screen and you don't wanna save them to a file, you can redirect them to dev/null, for instance.
Let's look at the output of the ls-l command. Let's redirect that standard output to a file. Use the greater than symbol and follow it with a file name. So use files.text. So if we look at the contents of files.text we'll see that it has the output of ls-l. You can append to an existing file with two greater than symbols.
So let's take the standard output of ls and append it to files.text. You can see that this is the ls-l output from earlier, and then at the bottom is just the ls output. Input redirection works like this. Let's take files.text and redirect the contents into sort. And you see that sort was run on the command. This is really equivalent to sort files.text, but this is another way to do it.
File descriptor one is assumed for output redirection, while file descriptor two is assumed for error redirection. So this is the same ls-l to files.text is the same as ls-l one, the file descriptor number to files.text. However, make sure you don't leave a space if you're going to use this.
Input and output redirection can be combined. So let's use files.text as the input for sort command, and let's redirect that output to sorted files.text. And you can see that, that worked. When a program encounters an error it reports its findings on standard error. File descriptor one is for standard output, while two is for standard error. File descriptor one is the default descriptor for output redirection. So this can mean that not all of the output generated by a program is captured by default.
Let's look at this example. You can see that this ls command found files.texts but it also had an error. The file not here was not there. Let's run the same command again. So in this situation, file descriptor one standard out was redirected to the file called out. Standard error was not redirected, so it was displayed to our screen. You could see that the standard out did go to out.
Let's redirect standard error into out.err. So standard output came to our screen, standard error went to this file. This can actually be handy because you can send standard output one place and standard error to another. So let's do that. That's standard out and this is standard error. And you could use that for an error log, for instance. You can also combine standard output and standard error. Let's do that.
Redirect output to out.both, take standard error and append it to standard input. So both the error message, no such file or directory and the standard output of files.text ended up in out.both. Let's use the null device. If you wanna ignore errors, you can send the errors to dev/null. Or send standard output to dev/null. And then now you just get the error message or you can do both. Redirect output to dev/null, append standard error to standard output.
This lesson covered standard input, standard output and standard error. The default file descriptor for standard input is zero, while standard out is one and standard error is two. You can redirect input and output with greater than and less than symbols. If you want to use redirection in conjunction with file descriptors, use an ampersand. You can redirect output to dev/null to prevent output from being displayed to your screen or written to a file.