Skip to main content
Engineering LibreTexts

05-E.8.3: File Output Manipulation - tee & /dev/null

  • Page ID
    32647
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

    The tee Command

    The tee command reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It basically breaks the output of a program so that it can be both displayed and saved in a file. It does both the tasks simultaneously, copies the result into the specified files or variables and also displays the result.

    A command is run in Linux. The output, by defaults goes to the standard output device. If that output is piped to the tee command (which has a filename as an argument), then the output from the original command is captured in the file by tee as well as displayed on the standard output device
    Figure \(\PageIndex{1}\): The tee Command ("Tee.svg" by SvenWikimedia Commons is licensed under CC BY-SA 4.0)

    SYNTAX:

    tee [ OPTION ] [FILE]...
    

    tee Command Options :

    Options Meaning
    -a, --append append to the given FILEs, do not overwrite
    -i, --ignore-interrupts ignore interrupt signals

    In each of the following examples, the output is shown on the screen and also placed in the specified file.

    # Output of the ls command is sent to the terminal as well as the the file output.txt, as can be seen with the cat of the file.
    pbmac@pbmac-server $ ls | tee output.txt
    one.txt
    states.list
    three.txt
    two.txt
    pbmac@pbmac-server $ cat output.txt 
    one.txt
    states.list
    three.txt
    two.txt
    
    # Output from the ls -l command is appended to the output.txt file
    pbmac@pbmac-server $ ls -l | tee -a output.txt
    total 8
    -rw-r--r-- 1 pbmac pbmac  0 Aug  5 12:31 one.txt
    -rw-r--r-- 1 pbmac pbmac 38 Aug  5 12:32 output.txt
    -rw-r--r-- 1 pbmac pbmac 27 Aug  5 11:11 states.list
    -rw-r--r-- 1 pbmac pbmac  0 Aug  5 12:31 three.txt
    -rw-r--r-- 1 pbmac pbmac  0 Aug  5 12:31 two.txt
    pbmac@pbmac-server $ cat output.txt 
    one.txt
    states.list
    three.txt
    two.txt
    total 8
    -rw-r--r-- 1 pbmac pbmac  0 Aug  5 12:31 one.txt
    -rw-r--r-- 1 pbmac pbmac 38 Aug  5 12:32 output.txt
    -rw-r--r-- 1 pbmac pbmac 27 Aug  5 11:11 states.list
    -rw-r--r-- 1 pbmac pbmac  0 Aug  5 12:31 three.txt
    -rw-r--r-- 1 pbmac pbmac  0 Aug  5 12:31 two.txt
    pbmac@pbmac-server $
    

    Linux /dev/null

    All data written on a /dev/null or /dev/zero special file is discarded by the system. Use /dev/null to send any unwanted output from program/command.

    Syntax:

    command >/dev/null

    It is possible to redirect a command's standard output messages to /dev/null where it is ignored by the shell.

    command 1>/dev/null

    This syntax redirects the command error output messages to /dev/null where it is ignored by the shell. OR

    command 2> /dev/null
    OR
    command &>/dev/null

    This syntax redirects both standard output and error output messages to /dev/null where it is ignored by the shell.

    Adapted from:
    "tee command in Linux with examples" by Anuragrawat1, Geeks for Geeks is licensed under CC BY-SA 4.0
    "/dev/null discards unwanted output" by Variety of contributors is licensed under CC BY-NC-SA 3.0


    05-E.8.3: File Output Manipulation - tee & /dev/null is shared under a CC BY-NC license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?