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).

Figure 1: The find command output is sent directly to grep without detouring via the shell.

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.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Command Line: Data Flow

    Working in the shell has many benefits. Pipelines, redirectors, and chains of commands give users almost infinite options.

  • Pipes in the Shell

    Pipes in the shell offer a surprising amount of versatility, including the ability to transfer data between computers.

  • Optimizing Shell Scripts

    Shell scripts are often written for simplicity rather than efficiency. A closer look at the processes can lead to easy optimization.

  • Bash 4.0 Introduces Associative Arrays

    The GNU Project's Bourne Again Shell (bash) is now in its fourth major version, which provides numerous enhancements.

  • Hotwire Interactive Shell

    The Hotwire shell removes the complexities of interactive consoles. We’ll show you how to Hotwire your Linux with this handy text and graphical shell for developers and administrators.

comments powered by Disqus
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.

Learn More

News