Linux redirection tutorial

When issuing a command in Linux, we get the result output on the screen monitor. We can send the command result to a file, for example using Linux redirection. Redirection in Linux means sending command output to another location or file. It is a great support for Linux command. Linux redirection is represented by > operator.

Linux redirection is quite an important command when working in Linux command line terminal. Here is an example of what redirect command can do:

kucing@ubuntu:~$ mkdir examples
kucing@ubuntu:~$ man man > examples/linux-manual.txt
kucing@ubuntu:~$ ls examples/
linux-manual.txt
kucing@ubuntu:~$

The example above shows redirect prints the output of man command to a file. We can use redirect to manipulates the STDOUT and prints the result to other destination. We can also use redirection to create a new file in Linux. For this purpose, we use Linux cat command. See example below:

kucing@ubuntu:~$ cat > examples/new-file.txt
This is a an example on how to use Linux redirection to create a new file in Linux. We use Linux cat command and redirection.

kucing@ubuntu:~$ ls examples/
linux-manual.txt  new-file.txt
kucing@ubuntu:~$ cat examples/new-file.txt
This is a an example on how to use Linux redirection to create a new file in Linux. We use Linux cat command and redirection.
kucing@ubuntu:~$ 

We can also use Linux redirection to add or append content to a file. Use double redirection operator (>>) for this job. See example below:

kucing@ubuntu:~$  cat >> examples/new-file.txt
This is a new content append to this file.                                
kucing@ubuntu:~$  cat examples/new-file.txt 
This is a an example on how to use Linux redirection to create a new file in Linux. We use Linux cat command and redirection.
This is a new content append to this file.
kucing@ubuntu:~$ 

As you can see, the Linux redirection is quite interesting. It can be really interesting if you are developing a program or configuring a Linux server. You can use Linux redirection to keep all results and errors in a dedicated directory. You can also use redirection to ignore error or warning by sending the output to /dev/null.

kucing@ubuntu:~$ /etc/init.d/apache2 start 2> examples/errors.txt
kucing@ubuntu:~$ /etc/init.d/apache2 start 2> /dev/null

That's all for now.

Back to Linux basic commands main page.

Nice article. But one thing

Nice article.
But one thing missing is Input Redirection.
A good example of Input Redirection is
wc < sample.txt
Here wc command takes its input from sample.txt and display the output accordingly.