How to Input Output (IO) Redirection in Linux

Basic Guide to Input Output (I/O) Redirection in Linux

Linux was developed with the aim of providing powerful and modern command-line tools to optimize user workflows and respond to user needs. For this reason, it offers valuable features that I/O Redirection is one of the most powerful features of the Linux command line to increase users’ productivity in software development and other purposes.

Input/output (I/O) redirection in Linux is a fundamental aspect of the Linux command-line interface (CLI) that allows the Linux user to control and manipulate the input and output streams of commands and programs. Input/Output redirection in Linux lets users change a given command’s default input and output locations.

Understanding input/output (I/O) redirection is essential for Linux terminal use, automation, flexible data management, and scripting. In this article, you will have a better and deeper understanding of I/O Redirection in Linux, and you will learn how to benefit from Input Output Redirection for various purposes, especially automation and scripting.

How to redirect Input Output (I/O) in Linux?

Many users buy Linux VPS to be more productive; for this purpose, it is helpful to learn how to redirect Input Output (I/O) in Linux because it will improve the performance you expect from a Linux VPS and optimize the management of workflows.

To understand and use Input Output (I/O) Redirection to increase productivity, you must understand the basic concepts to benefit from the I/O Redirection features in Linux. In the rest of this article, you will learn how to redirect in Linux after familiarizing yourself with the basic concepts.

1. Definition of Standard Input, Output, and Error concepts

When a command is executed in the terminal environment, you give input to the system and programs through the keyboard, and you receive two types of output: 1. command result and 2. error messages.

As a result, you will receive the data that the program reads along with program execution details and program status in text form.

In the Linux environment, each process has 3 communication channels, also known as file descriptors. Linux users use communication channels for standard interactions with Linux system processes. These communication channels are as follows:

  • Standard input (stdin, file descriptor 0)

Default input stream where programs and systems read data from it. Standard input transfers data by default through the keyboard as a command from the user to a program, and this data comes through the keyboard. But it can be redirected to come from a file or another command’s output.If I want to simplify the understanding of standard input, I should say that standard input is the same command that is used to communicate with the Linux kernel or an existing program in your Linux system through the keyboard in the terminal.

  • Standard output (stdout, file descriptor 1)

default output stream where a program writes its normal output. As a result, the Linux kernel or a program after processing the standard input, display the result as text in the terminal. The output of every process or command running in the Linux operating system has a default location, and you can redirect the standard output to another file or another command’s input, just like the standard input. For example, output an arbitrary text:

echo Opera's high-speed VPS is worth a try

Output:

Opera's high-speed VPS is worth a try

Using theechocommand, you can output any argument you send to it. The output you see is the standard output.

  • Standard error (stderr, file descriptor 2)

Default error stream where a program writes status/error messages in the terminal when it has failed. For example, if you run the ls command along with a directory, the ls command should output the contents of the specified directory, so to generate the standard error, we’ll run this command along with the directory that doesn’t exist in the system:

ls #

Output:

ls: cannot access #: No such file or directory

In this example, instead of specifying the existing directory in the system along with the ls command, we entered the argument #, and because of this, we received an error in the output. A standard error is not always displayed due to a program crash, service malfunction, or technical problems but is caused by the behavior of programs. You won’t get the standard error any time the program runs successfully. Of course, some programs display minor errors as standard errors without causing a crash.

2. Redirection operators in Linux

So far in the article, we have come to the understanding that I/O redirection allows the user to change the input source of a command and the destination of the command output by using the redirection operators “<” and “>“. In Linux, you can use various commands and operators to redirect the input, output, and errors between the command line and files or other processes, which you will learn about these operators in the following:

  • > “: Standard Output Redirection and Overwrites the file’s content
  • >>“: redirecting and Appending Standard Output
  • <“: Standard input redirection of command from a file rather than the keyboard
  • 2>“: Redirects the Standard error of a command to a file and overwrites the file’s contents.
  • 2>>“: Redirects the Standard error of a command to a file and appends the error to the file
  • <<“: Standard input redirection from the block of text rather than command
  • &>” or “2>&1“: Redirecting Standard Output and Error to the same file
  • &>>“: Appending Standard Output and Error to the specified file
  • <&“: redirects input of one file to another.
  • | (Piping): Sends the standard output of one command as the standard input to another command.
  • <<<“: uses a string as the input to a command.
  • <>“: Specifies the files to which standard input and errors are directed

As a result, by learning the redirection operators in Linux, you can control and manage the input and output flows in different Linux distributions such as Debian, Ubuntu, CentOS, Fedora, etc., and also, redirection operators allow you to manipulate and manage data to optimize workflow.

3. input Redirection in Linux

In normal mode, a program reads the input through the command that the user types on the keyboard, but what should we do if we don’t want the input to be a command connected to the keyboard and the program receives the input from a file?

The symbol (<) is used to redirect standard input from a file instead of the keyboard; that is, by using this operator, the program reads input from a file instead of reading from the keyboard. The basic syntax to redirect the standard input from the file instead of the keyboard is as follows:

command < file

For example:

less < /etc/passwd

In the previous example, using the (<) operator, the command’s standard input is taken from the /etc/passwd file instead of the keyboard.

Note: The “0<” symbol can also be used instead of (<) because they have the same function.

In addition to the (<), you can use the (<<) operator to redirect the standard input without overwriting the content of the target file if it existed before. The (<<) operator allows the user to specify a text block as input to a command and appends it to the file’s contents. For example:

command << DELIMITER
Text input
Goes here
DELIMITER

To pass multiple input lines to one command, you can use the (<<) as follows:

command << END
This is line 1
This is line 2
END

Between << END and END, you can send multiple input lines to the command.

Also, the (<<<) operator allows using string as input for a command:

command <<< "Input String"

4. output Redirection in Linux

Operators (> and >>) are used to redirect the standard output of a command to a file so that the output of this command is used as the standard input of another command. You may ask that If we want to redirect the standard output to a different file, should we use” >” or “>>“? In the following, we describe when you should use”>” and “>>.”

(>) operator: If you use the (>) to redirect the standard output to a file, the content of the file will be overwritten if it exists:

command > output_file

For example:

ls ~ > root_dir_contents.txt

By running the previous command, you will not see the output in the terminal because the output of the above command is sent to the root_dir_contents.txt file, and if this file already has contents, its content will be deleted, and the output of the previous command will be written in it because the (>)operator overwrites the content of the file.

Note: If you want to delete the command output, you can redirect the standard output of a command to the /dev/null file; Because /dev/null is a special file, any data sent to the /dev/null file will be deleted. Therefore, to discard standard output that is unused, you can also use the following command:

command > /dev/null

It is worth noting that /dev/null, also known as “the black hole,” you should be careful in using the /dev/null file in your commands because it can have dangerous consequences. For example, the “mv folder /dev/null” command, one of the most dangerous Linux commands, is sometimes used to transfer files to files, which causes the deletion of your important data.

(>>) operator: This symbol is also used to redirect standard output to a file, but instead of overwriting the content of the specified file (if it already exists), it appends the standard output of the command at the end of the target file:

command >> output_file

For example:

sudo apt-cache pkgnames >> pkgnames

In this example, the output of the command is sent to the pkgnames file, and if the pkgnames file already exists, the output of the command is appended to the end of the pkgnames file, but the contents of the pkgnames file will not be overwritten.

Note: Instead of the (>) operator, you can use (1>) to redirect the standard output.

5. Standard Error Redirection in Linux

The (2> and 2>>) operators allow you to redirect the standard error of a command to a file.

“2>” operator: redirects the standard error of a command to the target file; if the target file already exists, it overwrites the file’s contents. Use the following basic syntax to redirect a command’s standard error to a file:

command 2> file

For example:

ls -l /root/ 2>ls-error.log

If you run thelscommand without root permission, you will encounter an error; with the previous command, you sent the error of the ls command to the ls-error.log file, and if the ls-error.log file already exists, by running the “2> ” operator the content of the file is overwritten. Note that the error message is displayed as text in the terminal but is also sent to the target file.

“2>>” operator: “2>>” redirects the Standard error of a command to the file, but if the file exists, it will not overwrite its contents but appends the error to the end of the specified file. As a result, if you do not want the content of the error file to be deleted and overwritten, and you want to have an error report file for a service whose content will not be deleted by writing to it, we recommend using the “2>>” operator to redirect the standard error.

6. Standard output/error redirection to a file

&>” or “2>&1” are efficient operators that are used to redirect the standard output and error of a command to a single file simultaneously. The main syntax for simultaneously redirecting the output and error message of a command to a single file is as follow:

command &> file

Or

command 2>&1 file

If you want output and standard error to be appended to a single file, you can use “&>>” instead of “&>” or “2>&1”:

command &>> file

7. Combining Input and Output Redirection

Fortunately, Linux allows you to combine input and output redirection in one command. To make the command take input from input_file and direct output to output_file, run the following syntax:

command < input_file > output_file

For example:

sort <domains.list >sort.output

In this example, the sort command takes the input from the domains.list file and passes it to the sort.output file.

8. input /output redirection in linux using pipes

pipes (|) is the best way to combine multiple commands. Because pipes allow you to use the output of one command as the input of another command:

command1 | command2

This pattern is executed with the purpose that the standard output of command 1 is redirected to the standard input of command 2.

For example:

ls | less

By running the previous command, the output of the ls command, which presents the contents of your current directory, is used as the input of the less command, and the less command displays the contents of your current directory one line at a time. As a result, the contents of your current directory will be displayed as each entry in a new line by running the previous command.

9. Redirecting output to multiple files

Using the tee command, you can redirect the standard output to several files and simultaneously receive the command output in the terminal. To redirect the standard output of a command to a file at the same time you see it in the Linux terminal, use the following syntax:

command | tee output_file1 output_file2

This causes the output of the command to be redirected to another file in addition to being displayed in the Linux terminal, and if the file already exists, the file’s content will be overwritten; if the file does not already exist, a new file will be created. For example:

wc /etc/magic | tee magic_count.txt

In this example, the output of the “wc” command is sent in two places: 1. Terminal 2. The magic_count.txt file.

Conclusion

Input Output (I/O) Redirection in Linux is helpful for different purposes, one of its advantages is saving your time and energy in data management. Input Output (I/O) Redirection in Linux allows you to perform your tasks more optimally by creating error-reporting files, using the output of commands as input for other commands, and combining commands.

We are happy that we have expanded your skills in using the Linux terminal, and now, by learning how to I/O Redirection in Linux, you can experience better performance in the Linux system and control and manage your workflows in the best way. Input Output Redirection is a basic and helpful technique for scripting. We hope you will benefit from this valuable feature for various purposes and increase efficiency.

Thank you for taking the time to read this article.

Leave a Reply

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