Redirection operators

Using command redirection operators

You can use redirection operators to redirect command input and output streams from the default locations to different locations. The input or output stream location is referred to as a handle

The following table lists operators that you can use to redirect command input and output streams.

Redirection operator Description
> Writes the command output to a file or a device, such as a printer, instead of the Command Prompt window.
< Reads the command input from a file, instead of reading input from the keyboard.
>> Appends the command output to the end of a file without deleting the information that is already in the file.
>& Writes the output from one handle to the input of another handle.
<& Reads the input from one handle and writes it to the output of another handle.
| Reads the output from one command and writes it to the input of another command. Also known as a pipe.

By default, you send the command input (that is, the STDIN handle) from your keyboard to Cmd.exe, and then Cmd.exe sends the command output (that is, the STDOUT handle) to the Command Prompt window.

The following table lists the available handles.

Handle Numeric equivalent of handle Description
STDIN 0 Keyboard input
STDOUT 1 Output to the Command Prompt window
STDERR 2 Error output to the Command Prompt window
UNDEFINED 3-9 These handles are defined individually by the application and are specific to each tool.

The numbers zero through nine (that is, 0-9) represent the first 10 handles. You can use Cmd.exe to run a program and redirect any of the first 10 handles for the program. To specify which handle you want to use, type the number of the handle before the redirection operator. If you do not define a handle, the default < redirection input operator is zero (0) and the default > redirection output operator is one (1). After you type the < or > operator, you must specify where you want to read or write the data. You can specify a file name or another existing handle.

To specify redirection to existing handles, use the ampersand (&) character followed by the handle number that you want to redirect (that is, &handle#). For example, the following command redirects handle 2 (that is, STDERR) into handle 1 (that is, STDOUT):

1<&2

Duplicating handles

The & redirection operator duplicates output or input from one specified handle to another specified handle. For example, to send dir output to File.txt and send the error output to File.txt, type:

dir>c:\file.txt 2>&1

When you duplicate a handle, you duplicate all characteristics of the original occurrence of the handle. For example, if a handle has write-only access, all duplicates of that handle have write-only access. You cannot duplicate a handle with read-only access into a handle with write-only access.

Top of page

Redirecting command input (<)

To redirect command input from the keyboard to a file or device, use the < operator. For example, to get the command input for the sort command from File.txt:

sort<file.txt

The contents of File.txt appear in the Command Prompt window as an alphabetized list.

The < operator opens the specified file name with read-only access. As a result, you cannot write to the file when you use this operator. For example, if you start a program with <&2, all attempts to read handle 0 fail because handle 2 is initially opened with write-only access.

Note

  • Zero is the default handle for the < redirection input operator.

Top of page

Redirecting command output (>)

Almost all commands send output to your Command Prompt window. Even commands that send output to a drive or printer display messages and prompts in the Command Prompt window.

To redirect command output from the Command Prompt window to a file or device, use the > operator. You can use this operator with most commands. For example, to redirect dir output to Dirlist.txt:

dir>dirlist.txt

If Dirlist.txt does not exist, Cmd.exe creates it. If Dirlist.txt exists, Cmd.exe replaces the information in the file with the output from the dir command.

To run the netsh routing dump command and then send the command output to Route.cfg, type:

netsh routing dump>c:\route.cfg

The > operator opens the specified file with write-only access. As a result, you cannot read the file when you use this operator. For example, if you start a program with redirection >&0, all attempts to write handle 1 fail because handle 0 is initially opened with read-only access.

Note

  • One is the default handle for the > redirection output operator.

Top of page

Using the <& operator to redirect input and duplicate

To use the redirection input operator <&, the file you specify must already exist. If the input file exists, Cmd.exe opens it as read-only and sends the characters contained in the file as input to the command as if they were input from the keyboard. If you specify a handle, Cmd.exe duplicates the handle you specify onto the existing handle in the system.

For example, to open File.txt as input read to handle 0 (that is, STDIN), type:

< file.txt

To open File.txt, sort the contents and then send the output to the Command Prompt window (that is, STDOUT), type:

sort< file.txt

To find File.txt, and then redirect handle 1 (that is, STDOUT) and handle 2 (that is, STDERR) to the Search.txt, type:

findfile file.txt>search.txt 2<&1

To duplicate a user-defined handle 3 as input read to handle 0 (that is, STDIN), type:

<&3

Top of page

Using the >& operator to redirect output and duplicate

If you redirect output to a file and you specify an existing file name, Cmd.exe opens the file as write-only and overwrites the file’s contents. If you specify a handle, Cmd.exe duplicates the file onto the existing handle.

To duplicate a user-defined handle 3 into handle 1, type:

>&3

To redirect all of the output, including handle 2 (that is, STDERR), from the ipconfig command to handle 1 (that is, STDOUT), and then redirect the ouput to Output.log, type:

ipconfig.exe>>output.log 2>&1

Top of page

Using the >> redirection operator to append output

To add the output from a command to the end of a file without losing any of the information already in the file, use two consecutive greater than signs (that is, >>). For example, the following command appends the directory list produced by the dir command to the Dirlist.txt file:

dir>>dirlist.txt

To append the output of the netstat command to the end of Tcpinfo.txt, type:

netstat>>tcpinfo.txt

Top of page

Using the pipe operator (|)

The pipe operator (|) takes the output (by default, STDOUT) of one command and directs it into the input (by default, STDIN) of another command. For example, the following command sorts a directory:

dir | sort

In this example, both commands start simultaneously, but then the sort command pauses until it receives the dir command’s output. The sort command uses the dir command’s output as its input, and then sends its output to handle 1 (that is, STDOUT).

Top of page

Combining commands with redirection operators

You can create custom commands by combining filter commands with other commands and file names. For example, you can use the following command to store the names of files that contain the string "LOG":

dir /b | find "LOG" > loglist.txt

The dir command’s output is sent through the find filter command. File names that contain the string "LOG" are stored as a list of file names (for example, NetshConfig.log, Logdat.svd, and Mylog.bat) in the Loglist.txt file.

To use more than one filter in the same command, separate the filters with a pipe (|). For example, the following command searches every directory on drive C:, finds the file names that include the string "Log", and then displays them in one Command Prompt window at a time:

dir c:\ /s /b | find "LOG" | more

By using a pipe (|), you direct Cmd.exe to send the dir command output through the find filter command. The find command selects only file names that contain the string "LOG." The more command displays the file names that are selected by the find command, one Command Prompt window at a time. For more information about filter commands, see Using filters

Leave a Comment

Message for the day 02-09-10

Message for the day 02-09-10

You can remain stable when you learn to apply a full-stop.

Checking:

In any difficult situation, check if you are having thoughts like, "why do things happen with me like this or why is this person behaving in this way" etc. you can never remain stable when you have such questions.

Practice:

Tell yourself that it is much easier to put a full-stop(.) than putting a question mark(?). understand the difference between worrying and finding solutions and worrying. If there is a solution, find it, if there isn’t let things take care of themselves and put a full-stop.

Brahmakumaris-B & I Wing, Building name "OMSHANTI", Plot no.48, SWASTIK CHS, N.S. Road no.3, JVPD scheme, Vile Parle – West,Mumbai, Maharashtra-400056, INDIA

To unsubscribe or change subscriber options visit:
http://www.aweber.com/z/r/?bAzsLGxstCysnKws7GxstEa07OyMDIwMnA==

?l=Beo3l&m=1ZsNEHfjuNqP8H

Leave a Comment

Message for the day 06-07-10

Message for the day 06-07-10

To be free from wants is to be free from waste.

Expression:

The one who is free from wants is the one who is free from expectations. When there are no expectations, there is not too much thought about what is not there or what should be there. Since the mind is free from all these kind of waste thoughts, whatever is done is the best.

Experience:

When I am free from wants or desires, I am able to always remain content. I can then appreciate and enjoy everything that comes my way, and do not expect anything both from situations and people. So the mind is free from waste thoughts and questions.

Leave a Comment

Hilarious I.T. jokes !!!

Leave a Comment

srini koppuri life history..

Leave a Comment

What SKSVMA taught me…

TRUE STORY: Friday. 2PM. Lunch-Break. 3 engineers are in the urinals.

The first engineer finished and walked over to the sink to wash his hands. He then proceeded to dry his hands very carefully. He used paper towel after paper towel and ensured that every single spot of water on his hands was dried. Turning to the other two engineers, he said, “At NIT, we are trained to be extremely thorough.”

The second engineer finished his task at the urinal and he proceeded to wash his hands. He used a single paper towel and made sure that he dried his hands using every available portion of the paper towel. He turned and said, “At IIT, not only are we trained to be extremely thorough, but we are also trained to be ECO-Friendly.”

The third engineer finished and walked straight for the door, shouting over his shoulder,

“At SKSVMA, we don’t pee on our hands.”

__________________________

Leave a Comment

Conversation from “The Fontain Head”

One of the best Conversation’s ….

"When did you decide to become an architect?"

"When I was ten years old."

"Men don’t know what they want so early in life, if ever. You’re lying."

"Am I?"

"Don’t stare at me like that! Can’t you look at something else? Why did you decide to be an architect?"

"I didn’t know it then. But it’s because I’ve never believed in God."

"Come on, talk sense."

"Because I love this earth. That’s all I love. I don’t like the shape of things on this earth. I want to change them."

"For whom?"

"For myself."

"How old are you?"

"Twenty-two."

"When did you hear all that?"

"I didn’t."

"Men don’t talk like that at twenty-two. You’re abnormal."

"Probably."

"I didn’t mean it as a compliment."

"I didn’t either."

"Got any family?"

"No."

"Worked through school?"

"Yes."

"At what?"

"In the building trades."

"How much money have you got left?"

"Seventeen dollars and thirty cents."

"When did you come to New York?"

"Yesterday."

Leave a Comment

Nenjukkul peidhidum

Nenjukkul peidhidum

Nenjukkul peidhidum maa mazhai
Neerukul moozhgidum thaamarai
Sattendru maarudhu vaanilai
Penne un mel pizhai

Nillamal veesidum peralai
Nenjukkul neenthidum tharagai
Pon vannam soodiya kaarigai
Penne nee kaanchanai

Oh shaanti shaanti oh shaanti
Yen uyirai uyirai neeyenthi
Yen sendrai sendrai yennai thaandi
Ini neethan yenthan andhathi

Nenjukkul peidhidum maa mazhai
Neerukul moozhgidum thaamarai
Sattendru maarudhu vaanilai
Penne un mel pizhai

Yedho ondru ennai eerka
Mookin nuni marmam serka
Kalla-thanam yedhum illa
Punnagaiyo boganvilla

Nee nindra idam endral Vilai yeri pogadho
Nee sellum vazhi ellam panikatti aahadho
Yennodu vaa veedu varaikkum
Yen veetai paar yennai pidikkum

Ival yaaro yaaro theriyadhey
Ival pinnal nenje pogadhey
Idhu poyyo meyyo theriyadhey
Ival pinnal nenje pogadhey

Nenjukkul peidhidum maa mazhai
Neerukul moozhgidum thaamarai
Sattendru maarudhu vaanilai
Penne un mel pizhai.. oh.o.

Nillamal veesidum peralai
Nenjukkul neenthidum tharagai
Pon vannam soodiya kaarigai
Penne nee kaanchanai

Thookangalai thooki sendrai
Yekkangalai thoovi sendrai
Unnai thaandi pogum podhu
Veesum kaatrin veechu veru

Nil endru nee sonnal en kaalam nagaradhey
Nee soodum poovellam oru podhum udhiradhey
Kadhal enai ketka villa
Ketkathadhu kadhal illa

Yen jeevan jeevan neethaney
Yena thondrum neram idhuthane
Nee illai illai yendrale
Yen nenjam nenjam thaangadhey

Nenjukkul peidhidum maa mazhai
Neerukul moozhgidum thaamarai
Sattendru maarudhu vaanilai
Penne un mel pizhai.. oh.o.

Nillamal veesidum peralai
Nenjukkul neenthidum tharagai
Pon vannam soodiya kaarigai
Penne nee kaanchanai

Oh shaanti shaanti oh shaanti
Yen uyirai uyirai neeyenthi
Yen sendrai sendrai yennai thaandi
Ini neethan yenthan andhathi

Leave a Comment

From Srirangam to Mumbai — a tale of three generations

Leave a Comment

Quitters or Winners

Quitters or Winners

Leave a Comment

Older Posts »
Follow

Get every new post delivered to your Inbox.