I/O redirect in linux/unix with examples

I/O redirection is a very important function in linux/unix. It can change the standard input/output device when executing commands.

I/O redirection is a process that captures the output of a command, application, or script, and then sends the captured output as input to a file, command, or script.

Before introducing I/O redirection, let’s first understand a few important concepts:

  • File Descriptors (FD)
  • Standard input (stdin)
  • Standard output (stdout)
  • Standard error (stderr)

As long as we execute applications/commands on the terminal, we will always open 3 files(FD), namely standard input, standard output, and standard error.

During our standard interaction with the terminal, standard input is transmitted through our keyboard. Standard output and standard error are displayed on our terminal as text.

File Descriptors (FD)

In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an associated number called File Descriptor (FD).

The standard input, standard output and standard error files are always exist when running the program.

FileFile Descriptor
Standard Input STDIN0
Standard Output STDOUT1
Standard Error STDERR2

By default, standard error is displayed on the terminal. Error redirection routes errors to files outside the terminal.

Standard input

The standard input stream usually transfers data from the user to the program. Programs that expect standard input usually receive input from devices such as keyboards. Terminate standard input by reaching EOF (end of file).

In the following example, we will demonstrate the execution of the cat command in the terminal. When we input content through the keyboard (standard input), standard output will output what we input.

➜  ~ cat
input
input
ouput
ouput

# ctrl-d

We can enter EOF by pressing ctrl-d. After the cat command receives EOF, it will stop.

Standard Output

Standard output can write command results or program-generated data, etc. When standard output is not redirected, it will output text to the terminal.

In the following example, we will use the echo command to write test content to standard output.

➜  ~ echo "write standard output"
write standard output
➜  ~

Standard Error

Standard error will be written to the error generated by the command, and the error indicates that the command failed to execute. As with standard output, the default target for this stream is terminal display.

Redirect

After understanding the concept of redirection, let’s take a look at redirection symbols, which can help us change the standard input or output.

Overwrite

  • > standard output
  • < standard input
  • 2> standard error

Append

  • >> standard output
  • << standard input
  • 2>> standard error

Output redirection

In the following example, we will use echo command, overwrite redirection symbol (>) and append redirection symbol (>>) to show you that echo output is redirected to a file.

append redirect to file

Command output is append – directed to the file.

➜  ~ echo "append redirection >>" >> redirectionFile.log

overwrite redirect to file

Command output is overwrite – directed to the file.

➜  ~ echo "overwrite redirection >" > redirectionFile.log

Input redirection

In the following example, we will use input redirection and pass the contents of the redirectionFile.log to the awk command through the input redirection symbol “<” as an input parameter and execute it.

➜  ~ awk '{print $0}' < redirectionFile.log
overwrite redirection >
➜  ~

Error Redirection

In the following example, we will use the standard error file descriptor to redirect command errors to a file.

➜  ~ awk -F">" '{print $1}' redirectionFile.log 2>error.log

Add a Comment

Your email address will not be published. Required fields are marked *