Skip to main content
Engineering LibreTexts

05-E.8: File Output Manipulation

  • Page ID
    32547
  • \( \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}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    EXAM OBJECTIVES COVERED
    2.3 Given a scenario, create, modify, and redirect files.

    Text Streams—a Universal Interface

    Everything in Linux revolves around streams of data—particularly text streams. Data streams are the raw materials upon which the Linux core utilities, and many other command-line tools, perform their work. The use of Standard Input/Output (STDIO) for program input and output is a key foundation of the Linux way of doing things. STDIO was first developed for Unix and has found its way into most other operating systems since then, including DOS, Windows, and Linux.

    STDIO

    STDIO, standard input/output, was developed by Ken Thompson, one of the original authors of Unix, in the early versions of Unix. Programs that implement STDIO use standardized file handles for input and output rather than files that are stored on a disk or other recording media. STDIO is best described as a buffered data stream, and its primary function is to stream data from the output of one program, file, or device to the input of another program, file, or device.

    There are three STDIO data streams, each of which is automatically opened as a file at the startup of a program—well, those programs that use STDIO. Each STDIO data stream is associated with a file handle, which is just a set of metadata that describes the attributes of the file. File handles 0, 1, and 2 are explicitly defined by convention and long practice as STDIN, STDOUT, and STDERR, respectively.

    STDIN, File handle 0, is standard input which is usually input from the keyboard. STDIN can be redirected from any file, including device files, instead of the keyboard. It is not common to need to redirect STDIN, but it can be done.

    STDOUT, File handle 1, is standard output which sends the data stream to the display by default. It is common to redirect STDOUT to a file or to pipe it to another program for further processing.

    STDERR, File handle 2. The data stream for STDERR is also usually sent to the display.

    Redirection

    The work of any command is either taking input or giving an output or both. So, Linux has some commands or special characters to redirect these input and output functionalities. For example: suppose we want to run a command called “date” - if we run it will print the output to the current terminal screen. But our requirement is different - we don’t want the output to be displayed on the terminal, we want the output to be saved in a file. This could be done very easily with output redirection. Redirection here simply means diverting the output or input.

    Output Redirection

    Output redirection takes the output from command, system utility or error messages from Linux and redirects that output to a different location, usually a file. Below are most of the redirection operators and comments about how they work.

    pbmac@pbmac-server $ command > somefile
          # Redirect stdout to a file.
          # Creates the file if not present, otherwise overwrites it.
    
    pbmac@pbmac-server $ ls -lR > dir-tree.list
          # Creates a file containing a listing of the directory tree.
    
    pbmac@pbmac-server $ : > filename
          # There no command given, just the : character
          # The > truncates file "filename" to zero length.
          # If file not present, creates zero-length file (same effect as 'touch').
          # The : serves as a dummy placeholder, producing no output.
    
    pbmac@pbmac-server $ > filename    
          # There no command given, just the > character
          # The > truncates file "filename" to zero length.
          # If file not present, creates zero-length file (same effect as 'touch').
          # (Same result as ": >", above, but this does not work with some shells.)
    
    pbmac@pbmac-server $ command >> someFile.txt
          # Redirect stdout to a file.
          # Creates the file if not present, otherwise appends to it.
    
    
    # Single-line redirection commands (affect only the line they are on):
    # --------------------------------------------------------------------
    # Redirect stdout to file "filename."
       1>filename
    pbmac@pbmac-server $ some_command 1>outFile.txt    
      
       
    # Redirect and append stdout to file "filename."
       1>filename
    pbmac@pbmac-server $ some_command 1>>outFile.txt 
    
    # Redirect stderr to file "filename."
       2>>filename
    pbmac@pbmac-server $ some_command 2>errorFile.txt 
    
    # Redirect and append stderr to file "filename."
       2>&1
    pbmac@pbmac-server $ some_command1 output.txt 2>&
    
    Error messages get sent to same place as standard output.
        >>filename 2>&1
    pbmac@pbmac-server $ some_command >>filename 2>&1
          # Appends both stdout and stderr to the file "filename" ...
    

    Adapted from:
    "Chapter 20. I/O Redirection" by Mendel Cooper, The Linux Documentation Guide is in the Public Domain, CC0
    "Read and write data from anywhere with redirection in the Linux terminal " by Seth Kenlon, opensource.com is licensed under CC BY-SA 4.0
    "Input Output Redirection in Linux" by Hemusharma196, Geeks for Geeks is licensed under CC BY-SA 4.0


    05-E.8: File Output Manipulation is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?