JMU JMU - Department of Computer Science
Help Tools
Lab: Using the MS Windows CMD Shell


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Download the following files:
    to an appropriate directory/folder. (In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above.)

1. Conventions: Throughout this lab, the following typographic conventions are used:

[Enter] refers to the "Enter" key.

[Tab] refers to the "Tab" key.

[Down Arrow], [Up Arrow], etc... refer to the down arrow key, up arrow key, etc...

[Ctrl] refers to the "control" key and [Ctrl]+k means that you should press the "control" key and the "k" key at the same time.

The phrase "Enter the command xyz" means that you should type xyz in the terminal/command-shell window and then press [Enter].

2. Working with Directories: This part of the lab will help you learn how to work with directories/folders.
  1. Start a terminal/command-shell window. (For example, click on Start, pull up to Run..., enter cmd.exe in the text field, and click on OK..)
  2. Enter the command cd
  3. What output was generated?


    The current working directory which, in this case, is your home directory. (From here on in, this directory will be denoted by home.)
    Expand
  4. Enter the command mkdir cs
  5. Enter the command cd cs
  6. Enter the command cd (Note: When executed with a parameter, cd is used to change the working directory. When executed without a parameter it is used to display the name of the working directory.)
  7. What output was generated?


    home\cs
    Expand
  8. Enter the command mkdir labs
  9. Enter the command cd labs
  10. How can you determine the current working directory?


    It may be included in the prompt, but you can always use the cd command. (Note: You can change the CMD prompt using the prompt command. For example, the command prompt $P$S will change the prompt to the current working directory in followed by a space.)
    Expand
3. Compiling and Executing Java Applications: This part of the lab will help you learn how to compile and execute Java applications (a.k.a., programs) in a command shell.
  1. Change the working directory to the directory that contains the files you downloaded.
  2. Enter the command javac Tripler.java to compile the source code.
  3. What file do you know was created?


    Tripler.class
    Expand
  4. Enter the command java Tripler to execute the byte code. Enter 10 in response to the prompt.
  5. What was output?


    10 tripled is 30
    Expand
4. Redirecting Input: It can be tedious to test programs that accept input from a user because you often have to run them many times, typing the user's responses each time. One can, instead, tell the program to read from a file rather than the console. This is called input redirection. This part of the lab will help you understand input re-direction.
  1. Enter the command type test1.inp to see the contents of the file named test1.inp. (More on this command later.)
  2. What is in the file?


    It has a single line
    10
    
    Expand
  3. Enter the command java Tripler < test1.inp.
  4. Given the title of this section, what do you think the < does?


    It tells Tripler to read from the file named test1.inp rather than from the console.
    Expand
  5. What was output?


    Enter your number: 10 tripled is 30
    
    Expand
  6. Why aren't the two parts of the output on different lines?


    Because the first call to print() does not include a \n character.
    Expand
  7. Why were the two parts of the output on different lines when the user typed the response (rather than it bein redirected)?


    Because the user pressed the Enter key.
    Expand
5. Executing a Java Application Multiple Times: Especially when testing, it is sometimes necessary to run the same Java application multiple times with different command-line parameters. This part of the helps you do that.
  1. Start jGRASP.
  2. Click on File-New-Plain Text.
  3. Copy the following lines into the newly created file:
    java Tripler < test1.inp
    java Tripler < test2.inp
    

    and not that each of this lines is a valid CMD command.

  4. Save the file as tests.bat in the directory that contains Tripler.class
  5. In the CMD shell, enter the command tests
  6. What happened?


    The program Tripler was executed twice.
    Expand
  7. How could one use this when testing code?


    It is a convenient way to run multiple tests without having to type the input each time.
    Expand
6. Redirecting Output: Sometimes the output from a program can be long and confusing. In such situations, one can tell the program to redirect the output to a file rather than the console. This is called output redirection. This part of the lab will help you understand output re-direction.
  1. Enter the command java Tripler > test1.out.
  2. Type 10 and press Enter.
  3. Enter the command type test1.out.
  4. What was displayed?


    The contents of the file test1.out, which is:
    Enter your number: 10 tripled is 30
    
    Expand
  5. Why wasn't the prompt displayed on the console?


    Because all output was re-directed to the file.
    Expand
  6. How could one use this to test your programs?


    If one had a file with the expected output then one could use a file comparison utility (like Meld or Kdiff3) to compare the actual output and the expected output for differences.
    Expand
  7. For practice, use a file comparison utility (like Meld or Kdiff3) to compare test1.exp (the expected correct output) and test1.out.
  8. For more practice, Change Tripler.java so that it no longer produces the correct output, run the program again (again redirecting the output to a file), and compare this output with the expected output.
  9. Note that, because of the file names, it is fairly easy to overwrite an existing file when redirecting output. Hence, you may want to use the chmod command to make the .in and .exp files read-only. For example, chmod a-w *.in will remove write access on all files that end with .in for all users.
7. Reducing the Amount of Typing: This part of the lab will help you learn how to work more efficiently and reduce the amount of typing required to accomplish many tasks.
  1. Enter the command nkdir skills_cmd (Note: This is nkdir not mkdir!)
  2. What output was generated?


    'nkdir' is not recognized as an internal or external command, operable program or batch file.
    Expand
  3. Press the [Up Arrow] key.
  4. What happened?


    The previous command was re-typed.
    Expand
  5. Press the [Home] key
  6. What happened?


    The cursor moved to the first character on the line.
    Expand
  7. Press the [End] key.
  8. What happened?


    The cursor moved to the end of the line.
    Expand
  9. Press [Home] to go back to the first character.
  10. Press [CDelete]
  11. What happened?


    The first character was deleted.
    Expand
  12. Type m (which will insert an m as the first character).
  13. Press [Enter]
  14. Change the current working directory to your root directory by entering the command pushd \.
  15. Enter the command cd
  16. What output was generated?


    The name of your root directory.
    Expand
  17. You now need to go back to the skills_cmd directory. Save yourself some work by entering the command popd (which takes you back to the directory you were in when you changed the directory with pushd).
  18. What is the full path of your current working directory?


    home\cs\labs\skills_cmd
    Expand
  19. Move up one level in the directory hierarchy by entering the command cd ..
  20. Enter the command cd
  21. What output was generated?


    home\cs\labs
    Expand
  22. You now need to go back to the skills_cmd directory. Save yourself some work by typing cd [Tab] (which tries to "complete" the command).
  23. What happened?


    The command changed to cd skills_cmd
    Expand
  24. Press [Enter] to change to the skills_cmd directory.
  25. Enter the command mkdir temporary
  26. Press [Enter].
  27. Enter the command cd (to see the name of the current working directory).
  28. Enter the command dir (to list all of the files in this current working directory).
  29. You now need to remove the temporary directory using the rmdir command. Press [Up Arrow] until the mkdir command appears.
  30. Press [Home] to go to the first letter of the command.
  31. Press [Insert] to toggle from insert mode to overstrike mode.
  32. Type rm to replace the letters m and k.
  33. Press [Enter].
8. Working with Files: This part of the lab will help you learn how to work with files more efficiently.
  1. Change to your home directory.
  2. Enter the command dir
  3. What happened?


    All of the files in the home directory were listed.
    Expand
  4. Enter the command dir /w
  5. What happened?


    All of the files in the home directory were listed in a "wide" format.
    Expand
  6. Enter the command dir /W > filenames-short.txt (which will redirect the output of the dir command to a file named filenames-short.txt)
  7. Enter the command dir > filenames-long.txt
  8. Enter the command dir /S > filenames-all.txt (where the /S option instructs the dir command to look in all subdirectories)
  9. Enter the command dir filenames* (where the * is called the wildcard character).
  10. What happened?


    All of the files that begin with "filenames" were listed.
    Expand
  11. Move filenames-long.txt to the directory for this lab by entering the command ren filenames-long.txt cs\labs\skills_cmd
  12. How could you have used the [Tab] key to avoid having to type the file names?


    You could have pressed [Tab] twice.
    Expand
  13. Do the same thing with filenames-all.txt (Hint: Use [Up Arrow].
  14. To make sure you have moved the right files, enter the command dir filenames*
  15. To delete/remove the remaining file, enter the command del filenames-short.txt
  16. Quickly/effciently move to the skills_cmd directory by typing cd [Tab] followed by a backslash and another [Tab]
9. Getting Help: This part of the lab will help you learn how to get help in the CMD shell.
  1. Get the "help pages" for the dir command by entering the command help dir.
  2. What does the cls command do? (Hint: Use the "help pages".)


    It clears the screen.
    Expand
  3. What does the type command do?


    It displays the contents of a text file.
    Expand
  4. What does the find command do?


    Searches files for strings.
    Expand
  5. What does the copy command do?


    It copies files.
    Expand
  6. What does the more command do?


    It displays the contents of a text file one page at a time.
    Expand
10. Looking At and Inside Of Files: This part of the lab will help you look at the contents of files and search for strings within files.
  1. Show the contents of the filenames-all.txt file by entering the command type filenames-all.txt. (Note: Make sure you are in the correct directory.)
  2. Search for all occurrences of the string "2013" in all .txt files in the current working directory by entering the command find "2013" *.txt
11. Using the GUI Too: This part of the lab will give you a convenient skill.
  1. Insert a thumb drive (if you have not already done so).
  2. Type cd in the command shell window.
  3. Click and drag the icon for the thumb drive into the command shell window.
  4. What happened?


    The OS completed the cd command with the name of the thumb drive.
    Expand
Note: This part of the lab allows you to "go further" on this material. It is neither required nor for extra credit. It will, however, help you gain a deeper understanding of the material.
12. Other Shells:
  1. Learn more about CMD by entering the command help to get a list of all commands.
  2. Learn about the BASH shell used in Unix/Linux/OS X and how it differs from CMD at the course help pages.
  3. Are you using MS Windows but would like to have a BASH shell? Install MinGW and use MSYS. Follow the instructions in the section on the "Graphical User Interface Installer". (You might as well install some other programming languages while you are at it, like, Ada, C++, Fortran, abd Objective-C. You probably won't use them now but you may use them later.)

Copyright 2019