Input Output Redirections in Linux

Input Output Redirections in Linux

Learn default input out (I/O) types and I/O redirections in Linux.

In this article, we explain input output redirections in Linux by first describing default input/output (I/O) types and their associated file descriptors, and then giving practical I/O redirection examples.

What is Linux?

Linux is a family of open-source operating systems based on the Linux kernel. The first Linux system kernel was released on September 17, 1991, by Linus Torvalds.

Read more

Popular Linux distributions include Debian, Fedora, and Ubuntu, and the commercial distributions include Red Hat Enterprise Linux and SUSE Linux Enterprise Server.

There are also quite a number of customized Linux distributions, such as Kali Linux, REMnux etc. Kali Linux is a Debian-based distribution developed, funded and maintained by Offensive Security for ethical hackers for the purposes of Penetration Testing, Security Research & Assessment, and Computer Computer Forensics & Reverse Engineering. REMnux, on the other hand, is a Linux distro curated for reverse-engineering and malware analysis purposes.

Quote by Dennis Ritchie
Quote by Dennis Ritchie

UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.

Dennis Ritchie

Read more educational and inspirational cyber quotes at our page 100+ Best Cyber Security & Hacker Quotes.

Default Input and Output (I/O) Types in Linux

In Linux, there are three default input and output (I/O) types. They are Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr). By default, stdin device is the keyboard, while the stdout and stderr devices are the screen.

Input and Output (I/O) File Descriptors

In Linux, everything is represented as a file. In other words, files, directories, devices etc. are all files, albeit with different functionalities. In this respect, devices such as keyboards and screens are represented as files internally in Linux.

Linux assigns a File Descriptor (FD) to each of these files. File Descriptors are represented by numbers and used for referencing open files. By default stdin has a FD value of 0, stdout is represented by 1 and stderr has a FD value of 2.

Input Output (I/O) Redirections

Redirection is a feature in Linux that allows users to change the above described default input and output devices. The following are a list of frequently used symbols in Linux to redirect stdin, stdout, and stderr.

  • >0: Creates a file and redirects stdout to that file. If a file with the same name exists, it overwrites any existing contents of that file. Note that the > symbol is a shorthand notation for 1>, where stdout FD is used for redirection.
  • >>: Redirects stdout to a file, appending to any existing contents in the existing file.
  • <0: Redirects stdin to a command, i.e., redirects input from a file to a command. Note that the < symbol is a shorthand notation for 0<, where stdin FD is used for redirection.
  • 2>: Redirects stderr to file.

Default input output (I/O) types, associated file descriptors (FDs) and I/O redirection operators are summarized in Figure 1.

Figure 1: Input Output Redirections in Linux
Figure 1: Input Output Redirections in Linux

Using Input Output (I/O) Redirections

Output Redirection

To redirect the output of the echo command to a new file:

$ echo 'Learning Linux is fun!' > file1.txt
Figure 2: Standard Output Redirection Example - Creating a New File
Figure 2: Standard Output Redirection Example – Creating a New File

To append the output of the ls command to an existing file:

$ ls -la >> file1.txt
Figure 3: Standard Output Redirection Example - Appending to an Existing File
Figure 3: Standard Output Redirection Example – Appending to an Existing File

To redirect the output of the find command to an existing file (overwriting an existing file):

$ find . -iname "file*.txt" > file1.txt
Figure 4: Standard Output Redirection Example - Overwriting the Contents of an Existing File
Figure 4: Standard Output Redirection Example – Overwriting the Contents of an Existing File

Input Redirection

To redirect the output of a file to the sort command:

$ sort < file2.txt

is equivalent to

$ sort file2.txt
Figure 5: Standard Input Redirection Example
Figure 5: Standard Input Redirection Example

Standard input and output redirections can be combined. To redirect the output of a file to the sort command and then redirect this output to a new file:

$ sort < file3.txt > file4.txt
Figure 6: Example of Combining Standard Input and Output Redirection
Figure 6: Example of Combining Standard Input and Output Redirection

Error Redirection

The find command returns quite a number of error messages in addition to its standard output when access to some files and directories are not permitted by the system. In this case, both the standard output and the error messages are printed on the screen. This is shown in Figure 7.

$ find / -iname file1.txt
Figure 7: Example of Printing Both Standard Output and Error on the Screen
Figure 7: Example of Printing Both Standard Output and Error on the Screen

To leave the default standard output device as is (screen) and redirect the standard error to a file, for the find command example:

$ find / -iname file1.txt 2> errors.txt
Figure 8: Example of Redirecting Standard Error to a File
Figure 8: Example of Redirecting Standard Error to a File

To ignore the standard error messages by redirecting them to the null device (/dev/null), for the find command example:

$ find / -iname file1.txt 2> /dev/null
Figure 9: Example of Redirecting Standard Error to the Null Device
Figure 9: Example of Redirecting Standard Error to the Null Device

To redirect the standard output and error to separate files, for the find command example:

$ find / -iname file1.txt > outputfile.txt 2> errors.txt
Figure 10: Example of Redirecting Standard Output and Error to Separate Files
Figure 10: Example of Redirecting Standard Output and Error to Separate Files

Finally, to combine standard output and standard error and redirect the result to a file, for the find command example:

$ find / -iname file1.txt > combinedoutputfile.txt 2>&1
Figure 11: Example of Combining Standard Output and Error and Then Redirecting to a File
Figure 11: Example of Combining Standard Output and Error and Then Redirecting to a File

To learn more on Linux, you could also visit our Linux Resources Page.