Redirectors, Combining Commands & Pipes
Windows Command Line (CMD)
What We'll Learn:
This guide is all about input or output redirection, combining commands and pipes.
- In this lecture we are going to learn, what redirectors are.
- How we can use them to create files from the command line.
- How to combine multiple commands.
- And everything in between.
Lets get started!
What are Redirectors?
Usually the only way to send input to the command line is with your keyboard.
However, we can also send input or output using a redirector.
A redirector is a special symbol that is used to, as the name suggests redirect the input or output from one command to another.
Let’s take a look at a few ways we can use redirectors.
Like we said before we can use redirectors to send input or output form one command to another. Lets try it.
For example we can redirect or transfer the output of the echo command and write it into a file by using a redirector.
- Echo is primarily used to print variables or strings to the command line, similar to “print” or “Console.WriteLine” in other languages.
However, for now we are going to redirect its output and use it to create a file.
- There are multiple types of redirectors but for now we are going to use the Greater Than symbol.
It redirects the output of the echo command, in this case into a text file.
You can create multiple types of files using redirecotrs, however for this example we are going to create a text file because it’s the easiest to grasp.
Let’s type echo, followed by the text we want our file to have, along with the greater than symbol, and the name we want our file to have along with its extension.
echo Hello World > File.txt
And our file has been created.
Using the greater than redirector will only create a new file.
To add text to an existing file we need to type the greater than symbol twice.
echo Goodbye World >> File.txt
Once again using the type command we can see that our text was added successfully.
Pretty simple right?
Combining Commands & Pipes
In the windows command line, you can enter multiple commands with one line of code.
We used a redirector in the previously to redirect the output of the echo command into a file.
This time we are going to use a redirector to redirect from one command to another.
Let’s go through all the redirectors you can use to combine two commands.
- && The most commonly used redirector is the Double Ampersand. It only runs the second command if the first runs successfully.
Command1 && Command2
- & Next up is the single Ampersand. It runs the second command regardless if the first command returns an error or not.
Command1 & Command2
- || Then we have the Double Vertical bar. Which only runs the second command if the first one fails.
Command1 || Command2
- | And finally, the single vertical bar. Which redirects the output of the first command to the second.
Command1 | Command2
You can also combine multiple redirectors. For example:
Command1 && Command2 || Command3
If the first command runs successfully command2 will be run if not command3 will be run instead.
You can use any of the above redirectors or a combination of them to complete any task you wish. For example, to create a file, move it to a new folder and view its contents you need to type:
echo Success > File.txt && mkdir "My Folder" && move File.txt "My Folder" && type "My Folder\File.txt"
Try using a combination of commands on your own using the redirectors you just learned, to make sure you understand them properly.
Redirect the output of a command into a file using the greater than redirector.
- Make use of redirectorrs and pipes and to connect two commands in various ways.
- Redirectors can be used to combine multiple commands and can make time consuming tasks a lot easier.
You now know what redirectors are and how you can use them.
If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows command Line.