Basic Linux Tutorial
Computer Tutorial

Standard Output Redirection



Output Redirection

Redirection of stdout is controlled by ">" the greater-than symbol. The process of redirecting output takes input from the keyboard but sends its output to a file on disk.

Standard output redirection

Standard output redirection

If a command line contains the output redirection symbol (>) followed by a file name, the standard output from the command will go to the specified file instead of to the terminal. If the file didn't exist before the command was invoked, then the file is automatically created. If the file did exist before the command was invoked, then the file will be overwritten; the command's output will completely replace the previous contents of the file.
If you want to append to a file instead of overwriting, you can use the output redirection append symbol (>>). This will also create the file if it didn't already exist. There must be no white space between the two > characters.
We displayed contents of a file as follows :
$ cat newfile
This is first line.
This is the second line.
This is third and last line.
$
To redirect the output of the cat command we use the following step.
$ cat newfile > file1
$
Now the cat command displayed nothing, as the output of the command is redirected to a file. If we check the contents of file file1, it will contain the same text as newfile (the output of the cat command).