Input/output redirection in the Bash shell
Two Birds with One Stone
Cleverly combining the operators also makes it possible to redirect two channels at once. If you want to write the standard output of the find
command to a file, without also logging all error messages, simply write:
$ find /home -name "*.tex" > \ findoutput 2> /dev/null
The double operator >>
, which creates non-existent files or appends to existing files, can also be used in this scenario. You have already seen how this works for the standard output. You can use the double arrow for the standard error output in the same way:
$ find /home -name "*.tex" > \ findoutput 2>> error
This command ensures that the actual command output ends up in the findoutput
file, but the error messages land in a file called error
.
Pipe System
Pipes help save further steps by redirecting a command's output directly to another program without having to detour via file redirection. In doing so, the pipe character (|
) sits between the individual commands, as illustrated in the following example:
$ ls /etc | less
The ls
output no longer goes directly to the terminal but is instead sent to the less
pager first. The output is then shown in the terminal page by page, thus allowing the user to scroll conveniently up and down. The pipe is also frequently used to search output for character strings in combination with grep
; for example:
$ find debian -name "*.png" | \ grep --color apt
This command searches in the debian
folder for all files that end with .png
and then forwards the output directly to grep
. Grep then searches for the character string apt
and highlights the results in red using the --color
option (Figure 1).
You can also use multiple pipes. The following command lists the content of your home directory in a single line, forwards the results to the grep
program again, searches for the .jpg
character string, and then counts the number of matches:
$ ls -1 ~ | grep .jpg | wc -l 12
Without spending ages searching, you can discover that there are 12 JPG files residing directly below your home directory.
Time for Tee?
You can install a branch between the individual pipe pieces using the tee
program. The command expects data from standard input and writes it to both a file and the screen. The placement of tee
can occur between individual commands:
$ Command1 | tee output.txt | Command2
To search for PNG files starting in the current directory, you can log the output in images.txt
, display it, and search with grep
for the character string book
by typing:
$ find . -name "*.png" | \ tee images.txt | grep book
By default, tee
overwrites the specified target file, if it exists.
The call parameter -a
is available for appending output to an existing protocol:
$ find . -name "*.png" | \ tee -a images.txt | grep book
The different operators and the pipe and tee
command thus allow very flexible combinations of shell commands. As you can see, it is not worth writing a script for a quick search with these tools at your disposal. When used regularly, these combinations will flow from your fingertips as a matter of course.
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.