Linux Professional Institute LPIC-1 Exam 101

GNU and Unix Commands

Use Streams Pipes and Redirects Part 1

In this lesson, you’ll master how to redirect input and output in Linux, making your command-line workflows more powerful and flexible.

Table of Contents

  1. Standard Streams Overview
  2. Redirecting Output
  3. Appending Output
  4. Discarding Output
  5. Merging and Redirecting Both Streams
  6. Redirecting Input
  7. Here Documents and Here Strings
  8. Pipes and Pipelines
  9. Quick Reference
  10. Links and References

Standard Streams Overview

Linux programs communicate using three standard streams:

DescriptorStream NamePurpose
0stdin (standard input)Receives data (keyboard, files)
1stdout (standard output)Sends regular output (terminal, files)
2stderr (standard error)Sends error messages (terminal, files)

The image is a diagram illustrating the flow of standard input, output, and error in a command-line environment, showing how data from "file.txt" is processed by the "sort" command, with output directed to a terminal and errors to "errors.txt".

By default, both stdout and stderr appear on your terminal. You can redirect them separately:

$ command 1>output.txt 2>errors.txt

Redirecting Output (>)

To save a command’s output to a file (creating or overwriting it), use the > operator.

  1. Create a file with unsorted numbers:

    $ cat file.txt
    6
    5
    1
    3
    4
    2
    
  2. Sort the file and write the result to sortedfile.txt:

    $ sort file.txt > sortedfile.txt
    $ cat sortedfile.txt
    1
    2
    3
    4
    5
    6
    

Warning

Using > always overwrites the target file. You will lose previous contents!

Appending Output (>>)

To add output to the end of an existing file without erasing its contents, use >>:

$ echo "First line"  >> file.txt
$ echo "Second line" >> file.txt
$ echo "Third line"  >> file.txt
$ cat file.txt
First line
Second line
Third line

Discarding Output (/dev/null)

Send unwanted output or errors to /dev/null, the “black hole”:

$ grep -r '^The' /etc/ 2>/dev/null

This filters matching lines while discarding all error messages.

Merging and Redirecting Both Streams

  • Redirect stdout and stderr to separate files:

    $ grep -r '^The' /etc/ 1>output.txt 2>errors.txt
    
  • Append both streams:

    $ grep -r '^The' /etc/ 1>>output.txt 2>>errors.txt
    
  • Merge stderr into stdout and write to one file:

    $ grep -r '^The' /etc/ > all_output.txt 2>&1
    

Note

Order matters: > all_output.txt 2>&1 merges error output into the same file, while reversing redirects leaves errors on the console.

Redirecting Input (<)

Some commands read from stdin instead of a file argument. Redirect a file into stdin like this:

$ sendemail [email protected] < email_content.txt

The contents of email_content.txt feed directly into sendemail.

Here Documents and Here Strings

Here Documents (<<)

Embed a block of text as input:

$ sort <<EOF
6
3
2
5
1
4
EOF
1
2
3
4
5
6

EOF (or any marker you choose) encloses the input region.

Here Strings (<<<)

For single-line input, here strings are concise:

$ bc <<< "1+2+3+4"
10

Pipes and Pipelines (|)

Pipelines let you chain commands by feeding one’s stdout into the next’s stdin. Example: filter, sort, and align columns from /etc/login.defs:

$ grep -v '^#' /etc/login.defs \
| sort \
| column -t

Steps:

  1. grep -v '^#' removes comments
  2. sort orders lines
  3. column -t aligns columns into a neat table

Example output:

CREATE_HOME         yes
ENCRYPT_METHOD      SHA512
GID_MAX             60000
GID_MIN             1000
HOME_MODE           0700
MAIL_DIR            /var/spool/mail
PASS_MAX_DAYS       99999
PASS_MIN_DAYS       0
PASS_MIN_LEN        5
PASS_WARN_AGE       7
SYS_GID_MAX         999
SYS_GID_MIN         201
SYS_UID_MAX         999
SYS_UID_MIN         201
UID_MAX             60000
UID_MIN             1000
UMASK               022
USERGROUPS_ENAB     yes

Quick Reference

OperatorDescriptionExample
>Redirect stdout, overwrite filesort file.txt > sortedfile.txt
>>Redirect stdout, append to fileecho hi >> greetings.txt
<Redirect stdin from filewc -l < file.txt
2>Redirect stderr, overwrite filegrep foo bar 2>errors.log
/dev/nullDiscard streamcmd 2>/dev/null
&>Redirect both stdout and stderrcmd &> combined.log
``Pipe stdout into next stdin
<<EOFHere document (multiline input)See Here Documents
<<<Here string (single-line input)bc <<< "2+2"

Watch Video

Watch video content

Previous
Perform Basic File Management Part 2 archive files using tar