Input/Output Redirection in Linux

Input/Output Redirection in Linux

Adding content to files in Linux can be done in a variety of ways. Linux has a few text editors that can be used to add content to a file. However, this method requires some familiarity with Linux text editor commands.

There is a way in Linux to quickly add content to a file using a command line feature called input/output (I/O) redirection. I/O redirection allows for information in the command line to be sent to files, devices, and other commands. The input or output of a command is redirected from its default destination to a different location. I/O redirection is like a series of train tracks, where a switch can be enabled to direct the output of a command on a different track so it goes somewhere else in the shell. In this section, we are writing to files by redirecting the output of a command to a file.

When it comes to command input and output there are three paths, or “tracks”. These paths are called file descriptors. The first file descriptor is standard input, abbreviated as STDIN. Standard input is the information the command receives and processes when it is executed, essentially what a user types on the keyboard. The second file descriptor is standard output, abbreviated as STDOUT. Standard output is the information that the command displays, the output of the command. The last file descriptor is standard error, abbreviated as STDERR. STDERR, are the error messages generated by commands that are not correctly executed. The following are examples of how file descriptors will appear in the terminal:

Standard Input (STDIN)

Standard Output (STDOUT)

Standard Error (STDERR)

We will see one of the three file descriptors, STDOUT, and how to redirect STDOUT from where you normally see it, in the terminal, to a file in the filesystem. To use redirection, simply use a greater-than symbol > along with a filename:


To demonstrate redirection, we will use the output of the cat command. Without redirection, the output of the cat command will be displayed in the terminal:

Now use the > character to redirect the STDOUT of the cat food.txt command above to a new file called newfile.txt:

As you can see, there is no output displayed since the STDOUT has been redirected to the newfile.txt file. Verify that the STDOUT of the cat food.txt command is in newfile.txt:

This is useful if you need to copy content from an important file to another file in order to edit the contents without modifying the original file. However, what if you want to add a comment or note to a file? To do this, you can use the echo command. The echo command is used to print output in the terminal:

Printing comments to the screen is a fun feature but the echo command can be made more useful by using redirection. Using the echo command, content can be added to the newfile.txt file:

Notice that the STDOUT of the echo command has replaced the original content of the file. This is because the single > character will overwrite any contents in an existing file. To append rather than overwrite content to a file, use a double greater-than symbol >>:

Leave a Reply
Your email address will not be published. *