Standard Input / Output Redirection & Pipes in Linux

Post completing this article, you will be able to: 

  • Use redirection in the command line 
  • Use standard input 
  • Use standard output 
  • Use pipes 

At first glance, many people think the Unix Shell is a more cryptic version of the MS-Windows / DOS command prompt. However, the Unix shell can do much more than launch programs and copy files. Sequences of commands can be strung together in “Shell Scripts” to automate tedious or repetitive tasks. 

Unix systems provide a series of tools that are helpful when writing shell scripts. Typically, each tool performs a specific task, accepting input from the “standard input” source and sending its output to the “standard output source”. 


The standard input, output and error streams can be redirected to files. This is useful for saving the output of commands. Redirecting the output of a command can be done by using the ‘greater than‘ symbol ( > ) followed by the name of the file the output is to be saved in. 

For example, to save a list of the files in the current directory, use the command: 

ls >/tmp/filelist 

Running this command will create a file named filelist in the /tmp directory containing the output of ls. If a file named filelist already exists, it will be overwritten by this command. 

Standard Input

Many commands can accept input from a facility called standard input. By default, standard input gets its contents from the keyboard, but can be redirected. To redirect standard input from a file instead of the keyboard, the “<” character is used as 


[me@linuxbox me]$ sort < file_list.txt 

[me@linuxbox me]$ sort < file_list.txt > sorted_file_list.txt 

Standard Output

Most command line programs that display their results do so by sending their results to a facility called standard output. By default, standard output directs its contents to the display. To redirect standard output to a file, the “>” character is used as follows: 

[me@linuxbox me]$ ls > file_list.txt 

In this example, the ls command is executed and the results are written in a file named file_list.txt. Since the output of ls was redirected to the file, no results appear on the display. 

Each time the command above is repeated, file_list.txt is overwritten (from the beginning) with the output of the command ls. If you want the new results to be appended to the file instead, use “>>”

[me@linuxbox me]$ ls >> file_list.txt 

When the results are appended, the new results are added to the end of the file, thus making the file longer each time the command is repeated. If the file does not exist when you attempt to append the redirected output, the file will be created. 

As you can see, a command can have both its input and output redirected. Also, be aware that the order of the redirection does not matter. The only requirement is that the redirection operators (the “<” and “>”) must appear after the other options and arguments in the command. 


By far, the most useful and powerful thing you can do with I/O redirection is to connect multiple commands together with what are called pipes. With pipes, the standard output of one command is fed into the standard input of another. Here is an example: 

me@linuxbox me]$ ls -l | less

In this example, the output of the ls command is fed into less. By using this “| less” trick, you can make any command have scrolling output. You can use input redirection for looking the contents of a file by ‘<‘ operator. 

 $ cat < file1 

You can also use output redirection to add information to the file using ‘>’ operator. 

 $ cat file1 > file2 

Leave a Comment

Your email address will not be published. Required fields are marked *