All programs have access to 3 standard streams, which when run on the command line behave as follows by default:
- standard input (
stdin
): the keyboard input - standard output (
stdout
): the console output - standard error (
stderr
): the console output, used primarily to report errors.
You can control the behavior of those streams using redirection, which can
change the source of stdin
, or the destination of stdout
or stderr
.
We'll work with examples in Microsoft Windows, but the same ideas and most of the same syntax work in Linux or OSX (usually more consistently and with more powerful features); later in the semester we'll cover the command line interfaces available on those systems (or something similar).
Output redirection
Rather than printing to the console, you can signal that output (stdout
)
should go to a named file by using the >
operator. The following example
lists the contents of C:\
into a file:
dir /b C:\ > c-contents.txt
Note that if there is no output, an empty file will be created. In either
case, any existing file named c-contents.txt
will be destroyed. If you
want to add on to the file rather than overwrite it, use the >>
operator:
dir /b C:\ >> c-contents.txt
Now the output from that dir
command will be appended to the file if
it already exists (otherwise it will be created just like the first case).
You can also redirect stderr
; for example, the following command
lists the contents of C:\XYZ
into a file, and saves any error output
into another file. There will probably be error output, unless you happen
to have a directory named XYZ
.
dir /b C:\XYZ > xyz-contents.txt 2> xyz-errors.txt
On earlier versions of Windows, redirection of stderr
might not be available.
Input redirection
You can signal to a program that its input should come from a file
(rather than the keyboard) by using the <
operator.
For an example, let's look at the find
command (Windows version). This
command searches its input for a target string, and prints lines where it finds
the target. For example, find "banana"
will print back all the lines that you
type that contain "banana" (quotes not necessary). Note that when you run the
command, you'll need a way to signal that you're done. On the Windows command
line, type Ctrl-Z
and then Enter
to signal end-of-file.
That's probably not very useful, since when you're typing you already know yourself which lines contain "banana". The command is really made to search through files, and you can send input from a file by redirection:
find "banana" < banana-info.txt
The above command will print all lines in banana-info.txt
that contain the
"banana".
Pipes
Finally, you might want to get output from one program and send it directly
to another; so you combine output and input redirection. This is called
piping output, and uses the pipe character (|
) as the operator.
Here's an example:
dir /b C:\Windows\System32 | find "reg"
That particular example could be accomplished more simply by using a wildcard
(dir /b C:\Windows\System32\*reg*
), but in general pipes provide a powerful
way to combine programs to gain new functionality.
Homework
List the commands you could use to create a file (using only the command line) with the following contents:
I created this file using only commands; I don't need Notepad.
Hint: the command
echo x y z
will print "x y z" to the console.The
findstr
command works likefind
, except you can specify a regular expression to search for. Typefind /?
to see more information about its usage.How can you use the
findstr
command in combination withdir
to list all files that start with a, b, c, d, e, f, or g?The
assoc
command can list or modify file extension associations, and theftype
command can list or modify the programs used to open a particular file type.How can you use the
ftype
andfind
commands to determine all file types that are opened using Notepad? Hint: depending on your setup, you might need to tellfind
to ignore case (to not care whether words have any capital letters or not); usefind /?
to see how to do that.
To submit, either do your work in your 185-hw
repository in a folder called
hw5
(and post to your BitBucket account when you're done), or email me your
answers at jal2016@email.vccs.edu with subject CSC 185 HW5.
Other readings
- This post from 110 has a short summary of command line usage, and additional reading links.
- Commonly available commands in Windows (alternate version).
- A more advanced command line tutorial.
- Windows PowerShell (official site) is a more modern command line available in Windows (that we won't be covering in class).