23 C

Input Output Redirection in Linux/Unix with Examples

Introduction to Redirection

Redirection is the process of changing the input or output of commands given for execution in Linux/Unix. By redirection, you are going to change the execution of commands as standard input/output.

The basic workflow of Linux is that it takes input and after processing show the output. As we all know the standard input device (stdin) is keyboard and the standard output device (stdout) is screen. Usually Linux operates on this basic system of input/output. With redirection, we can change the standard input and output.

So, in this tutorial, we will learn.

  • Output Redirection
  • Input Redirection
  • File Descriptors (FD)
  • Error Redirection
  • Why error Redirection?
  • Examples

Output Redirection

Output redirection is represented by a sign “>” The “>” symbol is used for output (STDOUT).

- Advertisement -

First, we will see with examples that how can we take output in Linux rather than having it on our screen we will see it in the Terminal.


ls-al > output

Here’s the output of the given command ls-al is redirected to the file “output” rather than displaying on the screen.

otuput redirection example

Note: Double check the file name and make sure you are using correct file name while redirecting the output to the file. If you have already file with the same name and you again type the same file name all data on the file will be deleted and new output will be written to that file.

If you want to add some more information to previously output file, you can use the “>>” sign to add more output value to it.

You can also redirect standard output to devices. For instance you want to add some audio outputs to the other device you can do this so. It means we can apply standard output to files as well as devices.

$ cat song.mp3 > /dev/audio

The cat command reads the file song.mp3 and sends the output to the device /dev/audio which will play the music of that file.

Note: Make sure your audio systems are properly working and configuration of sound is working.

Input Redirection

Input redirection is represented by a sign “<“ .

The “<” symbol is used for output (STDIN).

We will see with examples that how we can use mail system to send mail as an input from our Terminal.

We can type the contents of the mail, we want to send to our Friends, Family or anyone. With the help of input redirection, we can add files to our mails by just typing a command in Terminal.

Mail -s “Subject” to-address < filename

This will read the file name, attach it to the mail recipient and finally send it to the recipient.

The above example described is sample, now we will move towards the advanced redirection techniques which will make use of advanced redirection techniques like File Descriptors (FD).

File Descriptors (FD)

What are File Descriptors?

In Linux/Unix everything is a file. This includes your regular files, folders, and even your devices are also files. These files are specified against specific numbers. These numbers are called File Descriptor (FD).

Your Screen is also assigned a file descriptor. When you execute a command or give instructions to show some results, the output is sent to the file descriptor of the screen which will show the results on your screen. Similarly, your printer also assigned a specific number which will be called when you require for printing some documents or anything else.

Error Redirection

In Linux/Unix, when we run a program or execute a command 3 files are open always.

  • First, Standard Input
  • Standard Output
  • Standard Error

These files are open whenever you run a program. As we discuss earlier that these files are associated with the specific descriptor number.


Standard Input STDIN

Standard Output STDOUT

And Standard Error STDERR

File Descriptor




Normally the error stream is displayed on the screen but error redirection routs the errors to the files other than your screen.

Why Error Redirection Occurs?

Error-redirection is the most popular and common feature of Linux/Unix .

Frequent Unix users faces that many command return them a large amount of errors. Let’s see some examples.

  • First, suppose you are looking for a file and you do not have specific permissions to read or write or even find that file, you will redirect to error. These errors show permission denied error and do not allow you to find a specific file.
  • Similarly, in executing shell scripts, you do not find the exact output you are looking for due to some errors.

So, the only solution is to redirect the error to a specific file

Example 1

$myfile 2>errorfile

In above example, we are executing a program name “myfile”.

The file descriptor for error redirection is 2. By using “2>” output would not be cluttered with errors.


  • So, Each file in Linux/Unix including any files, folders or devices is assigned with the descriptor numbers.
  • The keyboard is referred to as the basic and standard input device and the screen is standard output device in Linux/Unix.
  • The symbol “>” shows the output redirection and the symbol “<“ shows the input redirection while the symbol “>>” appends the output to the existing file(only in case if you retype the filename that existed before)
  • “>&” symbol redirects the output of one file to the other one.
  • Finally, you can redirect error redirection in accordance with the corresponding descriptor number.

You can also learn about the Tar command in our post.

Please share this post and join our Telegram Channel.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"


Please enter your comment!
Please enter your name here

Latest article