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, 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.
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 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" ...
"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