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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.