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.
Author
Released
8/31/2017Instructor 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
Skill Level Intermediate
Duration
Views
Related Courses
-
Setting Up a Red Hat Enterprise Linux Server
with Sandra Toner2h 46m Intermediate -
Linux: Desktops and Remote Access
with Grant McWilliams1h 44m Intermediate
-
Introduction
-
Welcome57s
-
What you should know3m 45s
-
About Red Hat Certifications2m 52s
-
-
1. Deploy Systems
-
Managing virtual machines6m 42s
-
2. System Config and Services
-
Get systemd service status3m 56s
-
Manage systemd services2m 33s
-
Configure networking3m 52s
-
Manage one-time jobs with at4m 16s
-
3. Essential Tools
-
Archive files using tar4m 40s
-
Compress files and archives4m 14s
-
Create files and directories5m 18s
-
Copy files and directories5m 48s
-
Move files and directories4m 59s
-
Create hard and soft links4m 55s
-
Introduction to vim3m 33s
-
Editing text with vim2m 45s
-
Reading the system journal3m 26s
-
4. Manage System Software
-
Query with RPM7m 14s
-
RPM query formatting6m 5s
-
Overview of yum2m 20s
-
Get info on package groups5m 15s
-
Search for packages1m 47s
-
Install and remove packages6m 59s
-
Manage OS updates6m 48s
-
Update the kernel4m 29s
-
Manage kernel modules5m 18s
-
5. Users and Accounts
-
Modify local user accounts4m 57s
-
6. File Access Control
-
File and directory modes1m 19s
-
Read access control lists3m 36s
-
Set access control lists5m 49s
-
Delete access control Lists4m 15s
-
-
7. Manage Security
-
Manage firewalls3m 15s
-
8. Accessing Linux Remotely
-
Configure Secure Shell2m 36s
-
-
9. Configure Local Storage
-
Conclusion
-
Next steps1m 20s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Use input-output redirection (>, >>, |, 2>, and more)