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.

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 redirectsstdout
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 for1>
, wherestdout
FD is used for redirection.>>
: Redirectsstdout
to a file, appending to any existing contents in the existing file.<0
: Redirectsstdin
to a command, i.e., redirects input from a file to a command. Note that the<
symbol is a shorthand notation for0<
, wherestdin
FD is used for redirection.2>
: Redirectsstderr
to file.
Default input output (I/O) types, associated file descriptors (FDs) and I/O redirection operators are summarized in Figure 1.

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

To append the output of the ls
command to an existing file:
$ ls -la >> file1.txt

To redirect the output of the find
command to an existing file (overwriting an existing file):
$ find . -iname "file*.txt" > file1.txt

Input Redirection
To redirect the output of a file to the sort
command:
$ sort < file2.txt
is equivalent to
$ sort file2.txt

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

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

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

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

To redirect the standard output and error to separate files, for the find
command example:
$ find / -iname file1.txt > outputfile.txt 2> errors.txt

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

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