Whenever you want to take the output of a command and save it, you can. This is a functionality built into bash. All commands have two outputs: STDOUT and STDERR. You can redirect these both individually. To redirect STDERR by redirection, use 2>. If you only want to redirect the stdout, you could without touching the STDERR.
- [Voiceover] Every command has one input STDIN, and two outputs STDOUT and STDERR. A pipe sends STDOUT from one command to the STDIN of another. A redirect works in a similar manner, but the source or destination is the file system. By default, the output of a command goes to the screen. This includes STDOUT and STDERR. STDOUT is the successful output from a command. STDERR is used for error messages. Redirection allows us to split these two outputs individually.
We can redirect either output or both to a disk. Commands also have a STDIN. If we wanted to redirect a file to a command, we can with a redirect toward the command. Once the command has processed the output, we can then redirect that output back to the disk. Let's see what this looks like using commands. To redirect STDOUT from a command to a file use a > symbol. One > symbol overwrites a file or creates it if it doesn't exist. To append to a file, supply two > symbols.
This will also create the file if it doesn't exist. If STDOUT is the first output, then STDERR must be the second output. To redirect STDERR supply 2>. Again, this overwrites the file if it exists or creates it if it doesn't. To append to an existing file, redirect the STDERR with 2>>. There are times when we might want to redirect all output from a command to a file. To do this we'll use &>.
Note that if you redirect all output from a command, nothing will show up on the screen. Once again, to append add an extra > symbol. You can redirect both into the STDIN of a command and then back out to a file again. For instance, we can redirect a file into the sort command to sort it, and then redirect the sorted output back out to a different file. We won't be able to redirect back over the original file though, as it will conflict. There are times when we might want to send the output of a command to a file and to the screen.
Neither pipes or redirects handle this natively, but Linux provides this special command for doing this called tee. It splits or tees the output and sends it both to a file and to the screen. This example takes the output of ls and saves it to a file named lsout.txt as well as display it on the screen. If we want to append to the lsout.txt file, we can add the -a or append option to tee. Let's see how this works by using redirects in a terminal.
In a terminal type in find space /etc space | space sort space | space tee space etcsort.txt space | space wc -l. In this example we get a list of all files in etc using the find command. That list will send to the sort command using a pipe. We'll split the output by using the tee command to save a copy of the sorted list to a file called etcsort.txt, but also send it into the next pipe.
Lastly, we'll use wc -l to count the lines. Go ahead and hit enter. What we should see when done is the sorted lines in etcsort.txt file, and the number of lines on the screen. Notice the last line is the output from wc -l and is the number of lines in the file. Also notice that we have more than just the number of lines on the screen. We have what looks like permission denied error messages from the find command. We can redirect standard error using the 2> symbols. Let's add this.
Press the up arrow key and move your cursor over to etc and insert 2> space etcerr.txt. You should have find space /etc space 2> space etcerr.txt space | space sort space | space tee space etcsort.txt space | space wc space -l and hit enter.
Now the error messages from find will be saved in etcerr.txt. The known error message text will be sorted and saved as etcsort.txt. And the total lines will be displayed on screen. When last de-veh-lah-du-shure is totally getting rid of output from a command by redirecting it to a bottomless pit. This bottomless pit is provided by slash dev slash null. If we redirect output from commands to slash dev slash null, nothing will show on the screen. For instance, type in find space /etc space &> space /dev/null and hit enter.
All output disappeared. Whenever we want to hide any output from a command, we can just redirect it to /dev/null.
Instructor Grant McWilliams covers network and internet services administration, kernel management, and intrusion prevention. He shows how to make your systems more efficient with virtualization, manage users and groups, and lock everything down with SELinux mandatory access control. Plus, get access to 25 PDF "cheat sheets" and 100 practice questions so you can solidify and test your knowledge.
- Installing Linux on a physical machine
- Managing systemd services
- Managing reoccurring jobs with cron
- Limiting system access
- Configuring networking
- Creating, editing, and moving files and directories
- Analyzing text with grep and regular expressions
- Installing software and packages
- Managing the kernel
- Managing users, accounts, and groups
- Setting permissions
- Using access control lists
- Securing Linux with SELinux
- Accessing Linux remotely
- Configuring local storage