Difference between revisions of "OPS102 - Redirection"

From CDOT Wiki
Jump to: navigation, search
(Created page with "== Standard File Descriptors == On Linux, other Unix-like systems, and on Windows, programs may open file descriptors (Linux terminology) or handles (Windows terminology). Ea...")
(No difference)

Revision as of 14:13, 26 September 2023

Standard File Descriptors

On Linux, other Unix-like systems, and on Windows, programs may open file descriptors (Linux terminology) or handles (Windows terminology). Each descriptor/handle is a numbered channel connected to a file or device.

By default, three channels are opened automatically by the shell when a process is started. These are:

0 - Standard Input (stdin) - this is the default input channel for the program 1 - Standard Output (stdout) - this is the default output channel for the program, used to output "normal" messages 2 - Standard Error (stderr) - this is the default error channel for the program, used to output error messages

Without redirection, all three of these descriptors/handles are connected to the terminal. Therefore, the command will get input from the terminal, send output messages to the terminal, and send error messages to the terminal.

File Redirection

Any of these descriptors/handles may be connected to a different file or device by adding symbols to the command line.

These are the most commonly-used symbols:

>file      redirects stdout to the specified file
>>file     redirects stdout to the specified file, appending

<file      redirects stdin from the specified file

Both the > and >> symbols will create the file if it does not exist. If the file does exist, the > symbol will overwrite it, while the >> symbol will append to it (add to the end of the file).

Examples on Linux:

$ date >now      # redirect the output of the date command into the file named "now"
$ cat now        # display the file contents
Tue 26 Sep 2028 01:14:02 PM EDT

$ date >>now     # append the output of the date command into the file "now"
$ cat now        # display the file contents - note that there are two dates
Tue 26 Sep 2028 01:14:02 PM EDT
Tue 26 Sep 2028 01:14:10 PM EDT

$ date >>now     # repeat a third time
$ cat now
Tue 26 Sep 2028 01:14:02 PM EDT
Tue 26 Sep 2028 01:14:10 PM EDT
Tue 26 Sep 2028 01:14:22 PM EDT

$ date >now      # redirect with a single > character - will overwrite
$ cat now        # display the file contents - note the old data was overwritten
Tue 26 Sep 2028 01:14:28 PM EDT

The same example on Windows:

> date /t >now.txt 
> type now.txt
2028-09-26 

> date /t >>now.txt 
> type now.txt
2028-09-26 
2028-09-26 

> date /t >>now.txt
> type now.txt
2028-09-26 
2028-09-26 
2028-09-26 
> date /t >now.txt 
> type now.txt
2023-09-26 

To redirect a different file descriptior/handle, place the descriptor/handle number in front of the redirection symbol:

2>file     redirects stderr (2) to the specified file
2>>file    redirects stderr (2) to the specified file, appending

Examples on Linux:

$ touch one two       # create the files "one" and "two"
$ rm three            # make sure that no file named "three" exists
rm: cannot remove 'three': No such file or directory

$ ls -l one two three # this should succeed for 2 files, fail for 1 file
ls: cannot access 'three': No such file or directory
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 one
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 two

$ ls -l one two three >listing.txt # redirect output but not errors
ls: cannot access 'three': No such file or directory
$ cat listing.txt     # view the saved output
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 one
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 two

$ ls -l one two three >listing.txt 2>errors.txt # output and errors redirected separately
$ cat listing.txt     # view the saved output
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 one
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 two
$ cat errors.txt      # view the saved error messages
ls: cannot access 'three': No such file or directory

To redirect one descriptor/handle to another descriptor/handle, use the syntax:

X>&Y

Where X is the descriptor/handle you're redirecting, and Y is the target descriptor/handle.

For example, on Linux:

$ ls -l one two three >all.txt 2>&1 # redirect stdout to all.txt, then redirect stderr to the same place as stdout
$ cat all.txt                       # view the contents of all.txt
ls: cannot access 'three': No such file or directory
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 one
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 two