Management in Linux
The course is part of this learning path
As the title suggests, this course looks at intermediate-level skills for those who already know a bit about Linux but want to enhance that knowledge. In this course, we build upon some of the topics covered in our Linux Fundamentals course, including files and shell scripting, but also introduce new concepts such as wildcards, job control, switching users, and installing software.
This course is part of the Linux Administration Bootcamp learning path, designed to get you up and running with Linux.
- Learn what wildcards are and how and when to use them
- Understand input, output, and redirection
- Work with files and shell scripting
- Implement processes and job control, and switch between users
- Install software using RPM- and Debian-based systems
- Anyone with basic knowledge of Linux who wants to learn more
- Professionals who want to learn more about Linux to enhance their career prospects
This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.
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.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.