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
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.