11/8/13www.cs.princeton.edu/courses/archive/spr05/cos126/cmd-prompt.html Windows Command Prompt in 15 Minutes There are only a few Command Prompt commands that you will need to master to make it through COS 126. The following is a brief tutorial covering the most important ones. What is Command Prompt? The Command Prompt program allows you to work in an environment that looks more like a traditional operating system as opposed to the icon based Windows environment. In Command Prompt, you will use your keyboard. You won't use your mouse at all. Command Prompt works at a lower level than Windows. This means that you will have more control over the machine. The disadvantage is that it is less user-friendly. You will need the command prompt in COS 126 to compile and execute your Java programs. Learning the Command Prompt also provides a gradual transition to Unix and Linux systems, which are prevalent in science, engineering, and industry. To launch Command Prompt select Start -> Run and type c m din the box. The Command Prompt shows up as a black terminal window. The command prompt should look something like: C : \ > This is where you type commands. The boldface type below (that follows the command prompt) is what you should type as you work through this tutorial. Windows does not care if you use upper or lower case. That means that command c dis the same as C D . It also means that, in Windows, file H e l l o W o r l d . j a v ais the same as h e l l o w o r l d . j a v a . This is NOT true in the system to which you will be submitting your files. Be very careful!!! Some Useful Commands javac: To compile a Java program, use the j a v a ccommand. Your program should compile without any errors or warnings (or if there are warnings be absolutely sure that they do not indicate a flaw in your program). C : \ > j a v a cH e l l o W o r l d . j a v a java: To run a successfully compiled Java program, use the j a v acommand. C : \ > j a v aH e l l o W o r l d more: Display the contents of a file one screenful at a time. C : \ > m o r eH e l l o W o r l d . j a v a exit: Exit the Command Prompt program and close the terminal window. www.cs.princeton.edu/courses/archive/spr05/cos126/cmd-prompt.html 1/7 princeton. Others are directories.11/8/13 www. 5 5 1 . B A T 0 1 : 3 6P M 0C O N F I G . S Y S 0 1 : 3 6P M 1 2 6H e l l o W o r l d . use the c dcommand with the name of a directory. C : \ >d i r V o l u m ei nd r i v eCh a sn ol a b e l . 9 4 0 . j a v a . like i n t r o c s . j a v a 1 2 : 1 1A M D I R D o c u m e n t sa n dS e t t i n g s 0 8 : 5 9P M D I R i n t r o c s 0 8 : 3 1P M D I R j 2 s d k 1 . cd: It is frequently useful to know in which directory you are currently working. It is useful to be familiar with both interfaces for managing files. C : \ >c di n t r o c s Now. dir: To view the contents of a directory. 2 _ 0 6 0 7 : 1 5P M D I R P r o g r a mF i l e s 0 7 : 3 3A M D I R W I N D O W S 3F i l e ( s ) 1 2 6b y t e s 5D i r ( s ) 3 2 .princeton. In order to find out.edu/courses/archive/spr05/cos126/cmd-prompt. C : \ >c d C : \ To change directories. V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ i n t r o c s www. V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ 1 0 / 2 6 / 2 0 0 4 1 0 / 2 6 / 2 0 0 4 0 2 / 1 0 / 2 0 0 5 1 2 / 0 9 / 2 0 0 4 0 2 / 1 0 / 2 0 0 5 1 1 / 0 2 / 2 0 0 4 1 2 / 2 9 / 2 0 0 4 0 1 / 1 3 / 2 0 0 5 0 1 : 3 6P M 0A U T O E X E C . the command prompt will be: C : \ i n t r o c s > To see what is in this directory type: C : \ i n t r o c s >d i r V o l u m ei nd r i v eCh a sn ol a b e l . 0 9 6b y t e sf r e e There are 7 items in this directory. type d i r . type c dat the command prompt.html 2/7 .cs. like H e l l o W o r l d .cs. Some of them are files. It is analogous to clicking on a Windows folder to see what's inside.html C : \ > e x i t Working with Files and Directories You can also use Command Prompt commands to organize files into a directory hierarchy. 4 .edu/courses/archive/spr05/cos126/cmd-prompt. This command will list all the files and directories within the current directory. These commands are equivalent to corresponding commands that you access via the Windows point-and-click interface. C : \ i n t r o c s >m o v eH e l l o W o r l d . C : \ i n t r o c s >m k d i rh e l l o To see that it actually worked. V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ i n t r o c s 0 2 / 1 0 / 2 0 0 5 0 2 / 1 0 / 2 0 0 5 0 2 / 1 1 / 2 0 0 5 0 2 / 0 3 / 2 0 0 5 0 1 / 1 7 / 2 0 0 5 0 8 : 5 9P M D I R 0 8 : 5 9P M D I R 0 2 : 5 3P M D I R 1 1 : 5 3P M 0 1 : 1 6A M 2F i l e ( s ) 3D i r ( s ) . t x th e l l o C : \ i n t r o c s >d i r V o l u m ei nd r i v eCh a sn ol a b e l . Then use the d i rcommand to see what www. V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ i n t r o c s 0 2 / 1 0 / 2 0 0 5 0 8 : 5 9P M D I R 0 2 / 1 0 / 2 0 0 5 0 8 : 5 9P M D I R 0 2 / 1 1 / 2 0 0 5 0 2 : 5 3P M D I R 0F i l e ( s ) 3D i r ( s ) . C : \ > mkdir: To create a new directory.edu/courses/archive/spr05/cos126/cmd-prompt. t x t 3 8 2b y t e s To return to the previous directory.cs. C : \ i n t r o c s >d i r V o l u m ei nd r i v eCh a sn ol a b e l . . use the command mkdir. h e l l o 1 2 6H e l l o W o r l d .princeton. use the d i rcommand. .princeton. j a v a 2 5 6r e a d m e . To access the two files.html 0 2 / 1 0 / 2 0 0 5 0 2 / 1 0 / 2 0 0 5 0 2 / 0 3 / 2 0 0 5 0 1 / 1 7 / 2 0 0 5 0 8 : 5 9P M D I R 0 8 : 5 9P M D I R 1 1 : 5 3P M 0 1 : 1 6A M 2F i l e ( s ) 2D i r ( s ) . j a v aand r e a d m e . t x t 3 8 2b y t e s move: Now. The following command creates a directory named h e l l o .11/8/13 www. j a v a 2 5 6r e a d m e . h e l l o 0b y t e s The two files are no longer visible from the current directory. 1 2 6H e l l o W o r l d .html 3/7 . which you can use to to store all of your files associated with the Hello World assignment. j a v ah e l l o C : \ i n t r o c s >m o v er e a d m e . change directories with the c dcommand. move the two files H e l l o W o r l d . . C : \ i n t r o c s >c d.edu/courses/archive/spr05/cos126/cmd-prompt. . use the c dcommand. . but this time followed by a space and two periods. .cs. . t x tinto the hello directory using the m o v ecommand. This is especially useful when you modify a working program. V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ i n t r o c s \ h e l l o 0 2 / 1 0 / 2 0 0 5 0 2 / 1 0 / 2 0 0 5 0 2 / 0 3 / 2 0 0 5 0 1 / 1 7 / 2 0 0 5 0 8 : 5 9P M D I R 0 8 : 5 9P M D I R 1 1 : 5 3P M 0 1 : 1 6A M 2F i l e ( s ) 2D i r ( s ) .java. j a v aH e l l o W o r l d . . t x t 3 8 2b y t e s You can also use m o v eto rename a file. j a v a 2 5 6r e a d m e . j a v a C : \ i n t r o c s \ h e l l o >d i r V o l u m ei nd r i v eCh a sn ol a b e l . The following command creates a backup copy of our HelloWorld. to Windows.11/8/13 www. . 1 2 6H e l l o W o r l d . t x t 3 8 2b y t e s C : \ i n t r o c s \ h e l l o >m o v eh e l l o w o r l d . t x t 3 8 2b y t e s It takes two moves because Windows won't let you move to an already existing filename and.edu/courses/archive/spr05/cos126/cmd-prompt. h e l l o w o r l d .html 4/7 . V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ i n t r o c s \ h e l l o 0 2 / 1 0 / 2 0 0 5 0 2 / 1 0 / 2 0 0 5 0 2 / 0 3 / 2 0 0 5 0 1 / 1 7 / 2 0 0 5 0 8 : 5 9P M D I R 0 8 : 5 9P M D I R 1 1 : 5 3P M 0 1 : 1 6A M 2F i l e ( s ) 2D i r ( s ) . 1 2 6h e l l o w o r l d .html is in this new directory.edu/courses/archive/spr05/cos126/cmd-prompt. Simply specify a new filename instead of a directory name. j a v ais the same as H e l l o W o r l d . C : \ i n t r o c s \ h e l l o >d i r V o l u m ei nd r i v eCh a sn ol a b e l . j a v a 2 5 6r e a d m e . j a v at e m p .princeton. Suppose you accidentally messed up the upper and lower case and had saved HelloWorld. 1 2 6H e l l o W o r l d . . j a v a 2 5 6r e a d m e .cs. copy: To make a copy of a file. . j a v a C : \ i n t r o c s \ h e l l o >m o v et e m p . C : \ i n t r o c s >c dh e l l o C : \ i n t r o c s \ h e l l o >d i r V o l u m ei nd r i v eCh a sn ol a b e l . j a v a . but might want to revert back to the original version if your modifications don't succeed. .java as helloworld. Use two move commands to fix it.cs. . V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ i n t r o c s \ h e l l o 0 2 / 1 0 / 2 0 0 5 0 2 / 1 0 / 2 0 0 5 0 2 / 0 3 / 2 0 0 5 0 1 / 1 7 / 2 0 0 5 0 8 : 5 9P M D I R 0 8 : 5 9P M D I R 1 1 : 5 3P M 0 1 : 1 6A M 2F i l e ( s ) 2D i r ( s ) .java program.princeton. www. use the c o p ycommand. b a k C : \ i n t r o c s \ h e l l o >d i r V o l u m ei nd r i v eCh a sn ol a b e l . we write a program C e n t e r o f M a s s . the user types the command "j a v aC e n t e r o f M a s s " and enters double type values in triplets: x p o s i t i o ny p o s i t i o nm a s sfrom the keyboard. t x t 3 8 2b y t e s del: Subsequently.princeton. 1 2 6H e l l o W o r l d . . and copy all of the files in the h e l l odirectory C : \ i n t r o c s \ h e l l o \into this newly created directory type: C : \ i n t r o c s >m k d i rl o o p s C : \ i n t r o c s >c o p yc : \ i n t r o c s \ h e l l o \ *l o o p s Here the *matches all files in the C : \ i n t r o c s \ h e l l odirectory. .html 5/7 . Redirection Two important abstractions in Command Prompt are standard input and standard output . The name of the backup file will be the name of the original file with a ~at the end. and m o v ecommands to several files (or directories) at once. For example.java and writes output using System. It copies them to your newly created l o o p sdirectory. The results appear in the terminal window. and standard output is your computer screen.edu/courses/archive/spr05/cos126/cmd-prompt. you might want to clean up useless files. j a v aand not H e l l o W o r l d . .println().princeton.edu/courses/archive/spr05/cos126/cmd-prompt. j a v aH e l l o W o r l d . j a v a 2 5 6r e a d m e . j a v a ~which is an old version of the file and has the wrong name.html C : \ i n t r o c s \ h e l l o >c o p yH e l l o W o r l d . in Assignment 1.d e l . To create a new directory called l o o p s . the jEdit program will automatically save a backup copy of your original file in the same directory. www. b a k C : \ i n t r o c s \ h e l l o >d i r V o l u m ei nd r i v eCh a sn ol a b e l . t x t 3 8 2b y t e s WARNING: When you revise a file in jEdit. V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ i n t r o c s \ h e l l o 0 2 / 1 0 / 2 0 0 5 0 2 / 1 0 / 2 0 0 5 0 2 / 0 3 / 2 0 0 5 0 1 / 1 7 / 2 0 0 5 0 8 : 5 9P M D I R 0 8 : 5 9P M D I R 1 1 : 5 3P M 0 1 : 1 6A M 2F i l e ( s ) 3D i r ( s ) .cs.out. 1 2 6H e l l o W o r l d . j a v a 2 5 6r e a d m e . By default standard input is your keyboard. The d e lcommand deletes a file. When you submit your program be careful to submit H e l l o W o r l d . To run our program.cs. wildcards: You can also apply the c o p y . C : \ i n t r o c s \ h e l l o >d e lH e l l o W o r l d .11/8/13 www. j a v athat reads input using StdIn. V o l u m eS e r i a lN u m b e ri sC 8 C 7 B D C D D i r e c t o r yo fC : \ i n t r o c s 0 2 / 1 0 / 2 0 0 5 0 2 / 1 0 / 2 0 0 5 0 2 / 0 3 / 2 0 0 5 0 1 / 1 7 / 2 0 0 5 0 8 : 5 9P M D I R 0 8 : 5 9P M D I R 1 1 : 5 3P M 0 1 : 1 6A M 2F i l e ( s ) 3D i r ( s ) . . txt now contains exactly the same data as o u t p u t . First. no output appears on the screen. C : \ i n t r o c s \ l o o p s >j a v aC e n t e r o f M a s s>o u t p u t . Using a text editor (like jEdit). Piping www. C : \ i n t r o c s \ l o o p s >j a v aC e n t e r o f M a s s<i n p u t . if there are lots of input values (there are over 700 inputs for Assignment 2) it would be tedious to retype them in each time we run our program. but the file output2. we use the redirection symbol "<". even the statement that tells the user what to do. 5 2 0 This produces exactly the same result as if the user had typed the numbers. we can use the output redirection symbol '>'. Be careful. 5 0 . t x t 0 . without waiting for user interaction. 5 2 0 Redirecting standard input. Second.princeton. 5 0 . This means that your grader can process your homework programs without typing in the input values by hand each time.) p h o e n i x . t x talready exists. create a file named i n p u t . t x t 001 0 111 0 Then to read the integers from the file instead of the keyboard. It is often useful to use both redirection operations simultaneously. As an alternative. E D U %m o r eo u t p u t . Continuing with the same example. t x t After executing this command.html C : \ i n t r o c s \ l o o p s >j a v aC e n t e r o f M a s s 001 0 111 0 0 . Note that all printf output is sent to the file. This is especially useful for two reasons.cs. it allows programs to be automated. if the file o u t p u t . t x t . t x t>o u t p u t 2 .edu/courses/archive/spr05/cos126/cmd-prompt. Redirecting standard output.edu/courses/archive/spr05/cos126/cmd-prompt. type the following command to verify that you entered the integers correctly: C : \ i n t r o c s \ l o o p s >m o r ei n p u t . C : \ i n t r o c s \ l o o p s >j a v aC e n t e r o f M a s s<i n p u t .princeton. but instead of sending the output to the screen. it will be overwritten.cs. P r i n c e t o n . (To append use '>>' instead.11/8/13 www.html 6/7 . and type in the six numbers. t x t . t x t Redirecting standard input and standard output. it is sent to the file named o u t p u t . except that the user has no opportunity to enter numbers from the keyboard. t x t 001 0 111 0 The user still types in the input values from the keyboard. After saving the file in the l o o p sdirectory. if we want to save the output permanently. we can create a file that consists of the same six input numbers. Similarly it is possible to redirect the output to a file instead of to the screen. t x tabove. html 7/7 .txt to standard input (as is done here) or by simply using the filename (as is done at the beginning of the document). Instead. Donna Gabai and Kevin Wayne.princeton. but there is so much that it whizzes by on the screen too fast to read. especially if your program goes into an infinite loop and you want to see the first few values that it prints. t x t C : \ i n t r o c s >m o r e<t e m p . Written by Jake Brenner.cs. C : \ i n t r o c s >j a v aR a n d I n t s>t e m p .cs.edu/courses/archive/spr05/cos126/cmd-prompt. we could do this in one line using the pipe symbol '|' C : \ i n t r o c s >j a v aR a n d I n t s|m o r e This is often useful when debugging a program.princeton. j a v aprints out a bunch of random integers. For example.11/8/13 www.) One possible way to accomplish this is to type the following two commands. www.html Another useful abstraction is piping. (The program R a n d I n t s . t x t Note that m o r ewill work by redirecting the file temp. Piping is when the output of one program is used as the input of another program. suppose we want to view the output of a program.edu/courses/archive/spr05/cos126/cmd-prompt.