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:

[email protected]:~$ mkdir examples
[email protected]:~$ man man > examples/linux-manual.txt
[email protected]:~$ ls examples/
linux-manual.txt
[email protected]:~$

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:

[email protected]:~$ 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.

[email protected]:~$ ls examples/
linux-manual.txt new-file.txt
[email protected]:~$ 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.
[email protected]:~$

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

[email protected]:~$ cat >> examples/new-file.txt
This is a new content append to this file.
[email protected]:~$ 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.
[email protected]:~$

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.

[email protected]:~$ /etc/init.d/apache2 start 2> examples/errors.txt
[email protected]:~$ /etc/init.d/apache2 start 2> /dev/null

That's all for now.

Back to Linux basic commands main page.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.