Skip to main content

Command Palette

Search for a command to run...

Input/Output Redirection in Linux

Redirect your data anywhere

Published
3 min read
Input/Output Redirection in Linux
A

I'm a tech aficionado, and cloud & DevOps engineer. I love solving problems and collaborating with teams. #TechEnthusiast #CloudOps #AWS #DevOps #TeamPlayer Connect with me to stay updated with my latest articles and insights on AWS, Linux, and DevOps!

Sometimes we want our Linux terminal output to be stored in a file instead of printing on the terminal. So how to achieve it? Don’t worry, Here output redirection comes into the picture. In Linux, we can redirect our terminal output to a file with a concept called Output Redirection.

Types of Output Redirection :

There are mainly three types of output redirections in Linux :

  • Standard Output (stdout) Redirection

  • Standard Input (stdin) Redirection

  • Standard Error (stderr) Redirection

Standard Output (stdout) Redirection

When we run any command or program and it produces/prints some output on the terminal screen, then it is called standard output. Redirecting this output to a file is called standard output (stdout) redirection. We redirect stdout using a greater than symbol ‘>’.

The below screenshot shows an example of stdout redirection.

The first command is echo “Hi, this is an example”. It prints output to the terminal as the echo command prints everything on the terminal which is given to it. In the second command, we redirected this output to a file named file.txt. Greater than symbol ‘>’ tells the shell to send the output of the left side command/program to the file given to it on the right side. So in our case, the output of the echo command will be stored in file.txt. We can verify it by reading the file.txt using the cat file.txt command.

We can also redirect stdout using double greater than symbols “>>”. The main difference is that “>” writes the terminal output to the given file whereas “>>” appends the terminal output to the given file. Below is an example of “>>” :

Standard Input (stdin) Redirection

When we run any command or program and it reads input from the terminal command line, then it is called standard input (stdin). Redirecting this input from a file is called standard input (stdout) redirection. We can redirect standard input using smaller than symbol “<”.

The below screenshot shows an example of stdin redirection:

cat command reads input from stdin and prints its output to the terminal screen. In our example, we took the input from a file named file2.txt and it prints output on the terminal screen.

Standard Error (stderr) Redirection

When we run any command or program and it gives some error on the terminal screen then this output is called standard error (stderr). Redirecting this output to a file is called standard error (stderr) redirection.stderr” is different than “stdout”. The descriptor for stdout is 1 which is the default descriptor. While the descriptor for stderr is 2. So we can redirect standard error using “2>”.

The below screenshot shows an example of stderr redirection :

In the above example, we executed an unknown command that gives an error. We tried redirecting this output to a file using “>” but it did not work. It proves that it is not and standard output (stdout). We then redirected this output using “2>” into file3.txt and it was stored successfully because it is a standard error (stderr).

Redirect Output using pipe '|'

We can use the pipe symbol “|” to redirect the output of a command to the input of another command. The syntax for this is: command1 | command2

An example is given below :

In the above example, we used the pipe symbol to redirect the output of the cat command as an input of the grep command.

Note: grep command checks for a pattern in a file and prints the line which contains the pattern.

Conclusion

  • We learned to redirect stdout, stdin, and stderr into a file.

  • We learned overwriting and appending into a file.

  • We learned the difference between stdout and stderr.

  • We learned to redirect the output of a command as an input of another command.

The Ultimate Linux Starter Series

Part 3 of 5

In this series, I'll be sharing practical insights and tutorials on how to effectively utilize Linux concepts and commands to boost your productivity by 5x to 10x.

Up next

Demystifying Linux Permissions:

A Comprehensive Guide for Beginners