Input / Output types

There are three default types of input and output.

I/O Name Abbreviation File Descriptor
Standard Input stdin 0
Standard Output stdout 1
Standard Error stderr 2

By default, standard input comes from the keyboard, and standard output and standard error are displayed to the screen. Each one of these I/O types is given a file descriptor . File descriptors are just numbers that represent open files,

For use human, it is easier to reference files by name, but it is easier for computers to reference them by number. You might be thinking, keyboard and screen are not files. On one level, that is true, but on another level, it is not. Linux represents practically everything as a file. This abstraction allows you to do some powerful things like take the standard output of one command that would normally be displayed to your screen and use it as the input to another command, or even redirect that output to a file.

Redirection

Use the greater than sign (>) to redirect output and less than sign (<) to redirect input. The greater than sign (>) redirects output from a command to a file, while the less than sign (<) redirects input from a file to a command.

Redirection Operator Meaning
> Redirects standard output to a file. Overwrites existing contents.
>> Redirects standard output to a file. Appends to any existing contents.
< Redirects input from a file to a command.

The explicit way of using redirection is to provide a file descriptor number. However, if 1 is ommited, then the file descriptor 0 is assumed for input, and 1 for output redirection.

Normally, in redirection, a filename follows the redirection operator. If you want to use a file descriptor instead of a filename, you can use the ampersand sign (&). So, instead of redirecting standard error to a file called err.txt e.g. 2>err.txt , you could redirect it to standard output with 2>&1, this effectively combines standard input and standard output.

Sign Meaning
& Used with redirection to signal that a file descriptor is being used.
2>&1 Combine standard error and standard output.
2>filename Redirect standard error to a file.

The Null Device

If you want to ignore output, you can send it to the null device which is dev/null. The null device is a special file that throws away whatever is fed to it. You may hear people refer this as a bit bucket.

If you don't want to see errors on your screen and you don't want to save them to a file, you can redirect them to dev/null.

Examples

Example 1:

Take the output of the ls -l command and redirect it to a file called file.txt:

$ ls -l > file.txt
Example 2:

Take the output and append to an existing file:

$ ls -l >> file.txt
Example 3:

Take file.txt and redirect the contents into sort:

$ sort < file.txt
Example 4:

The file descriptor 1 is assumed for input redirection. Therefore, the following 2 commands are the same. However, make sure you don't leave a space if you want to use this.

$ ls -l > file.txt
$ ls -l 1> file.txt
 
$ ls -l 1 > file.txt
ls: cannot access 1: No such file or directory
Example 5:

Input and output redirection can be combined, so we can use file.txt as the input for sort command, and redirect that output to sorted_file.txt.

$ sort < file.txt > sorted_file.txt
Example 6:

You can see that the ls command found file.txt , but it also has an error. The file not-here cannot be found.

$ ls file.txt not-here
ls: cannot access not-here: No such file or directory
file.txt

If we redirect above output to a file called out.txt, then the stand output will be redirected to the file out.txt. Standard error will not be redirected, but display to the screen.

$ ls file.txt not-here > out.txt
ls: cannot access not-here: No such file or directory
 
$ cat out.txt
file.txt

To redirect standard error into a file called error.txt. The following command will redirect standard error to a file, but display standard output to the screen.

$ ls file.txt not-here 2> error.txt
file.txt. 

A more handy way is to send standard output to one place, and send standard error to another.

$ ls file.txt not-here 1>out.txt 2> error.txt 

You can also combine standard output and standard error. Redirect output to out.txt , and take standard error and append it to standard input.

$ ls file.txt not-here > out.txt 2>&1
Example 6:

Send standard error to dev/null

$ ls file.txt not-here 2>/dev/null
file.txt

Send standard output to dev/null

$ ls file.txt not-here >/dev/null
ls: cannot access not-here: No such file or directory

Or you can combine standard output and standard error to dev/null

$ ls file.txt not-here >/dev/null 2>&1