Linux/Unix – Redirecting Input or Output to a File

Redirecting Input to a File

To redirect standard input to a file, use the `<‘ operator. To do so, follow a command with < and the name of the file it should take input from. For example, instead of giving a list of keywords as arguments to apropos, you can redirect standard input to a file containing a list of keywords to use.

To redirect standard input for apropos to file `keywords’, type:

$ apropos 60; keywords

 

Redirecting Output to a File

Use the `>’ operator to redirect standard output to a file. To use it, follow a command with > and the name of the file the output should be written to.  To redirect standard output of the command apropos shell bash to the file `commands’, type:

$ apropos shell bash 62; commands

 

If you redirect standard output to an existing file, it will overwrite the file, unless you use the `>>’ operator to append the standard output to the contents of the existing file.

To append the standard output of apropos shells to an existing file `commands’, type:

$ apropos shells 62;62; commands

 

Redirecting Error Messages to a File

To redirect the standard error stream to a file, use the `>’ operator preceded by a `2′. Follow a command with 2> and the name of the file the error stream should be written to. To redirect the standard error of apropos shell bash to the file `command.error’, type:

$ apropos shell bash 262; command.error

 

As with the standard output, use the `>>’ operator instead of `>’ to append the standard error to the contents of an existing file.

To append the standard error of apropos shells to an existing file `command.error’, type:

$ apropos shells 262;62; command.error

 

To redirect both standard output and standard error to the same file, use `&>’ instead.  To redirect the standard output and the standard error of apropos shells to the file `commands’, type:

$ apropos shells 38;62; commands

 

Redirecting Output to Another Command’s Input

Piping is when you connect the standard output of one command to the standard input of another. You do this by specifying the two commands in order, separated by a vertical bar character, `|’ (sometimes called a “pipe”). Commands built in this fashion are called pipelines.

For example, it’s often useful to pipe commands that display a lot of text output to less, a tool for perusing text. To pipe the output of apropos bash shell shells to less, type:

$ apropos bash shell shells | less

This redirects the standard output of the command apropos bash shell shells to the standard input of the command less, which displays it on the screen.


Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 16, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: