Using Windows via Command Line

created by Carlos Aguilera
updated on June 2017

The Lab machines are linux based and some of you have Macintosh or Windows. This tutorial will cover using windows via command line and also using Cygwin, a linux-like environment.

Console

To get started, open up a Command Prompt (from the Start Menu under Windows System). Alternatively, you can use the Cortana search bar to find the command prompt by typing cmd. As the linux tutorial I'll refer to your interface as the console window. The console is a text-based interface where we can enter commands. These commands run programs with input that we provide, and then displays the output of that program.


Before we enter any commands, you should understand a little more about the console output. Look at the window below:

The text to the left with the > at the end gives basic information such as the current directory or folder. In the above example, the user is Carlos. We can tell because we are under the Users directory and in Carlos. Unfortunatley, windows does not have ssh capabilities. What we can do is install cygwin to assist us. However, below we will cover the basic windows commands. In UNIX based systems to list the content in the current directory we type ls. In windows we type dir.


In UNIX based systems to change directories we type cd. Convinently, it is the same for windows or alternatively you can use chdir in the command prompt. In addition, in windows, cd, tells us the current directory which is helpful if we cannot tell where we are in the directory structure. Another helpful command in windows is copy which works similar to cp in UNIX. So if you need to copy files you can use the copycommand in windows. Furthermore, if you want to delete or remove files or directories the rd and del commands are helpful. rd removes directories and rd /s removes directories recursively. rd /s /q removes directories recursively forcefully which is the equivalent of the UNIX rm -rf. del only deletes files so del /f /q deletes a file forcefully. Moreover, to create a directory you can use mkdir or md.
Here you can see three commands in action: looking at the current location with cd, making a new directory, and changing directories.

C:\Users\Carlos>cd
C:\Users\Carlos

C:\Users\Carlos>mkdir CS110

C:\Users\Carlos>cd CS110

C:\Users\Carlos\CS110>
        

You can type dir at the new location and see the output. This will give you familiarity with the console. You can refer to the second image above to get an idea of the output for dir but it should be different because the directories are different and we have different content in our computers.

Installing Cygwin

You can follow this guide or you can follow this online resource: http://www.mcclean-cooper.com/valentino/cygwin_install/ which is where I got the photos.
Follow this link to the installation page: Install Cygwin
Be sure to download the setup-x86.exe for either the 32-bit or 64-bit depending on the version of windows you are runnning. Once the download completes run the executable. Hit next until you reach the 'Choose a download source.'

Then choose install from internet. Press next and choose the directory of where you want to install Cygwin. The default root directory is fine if you don't wish to specifiy the location. Hit next and then you can pick where downloads will be placed. If you want to add extra features to the Cygwin environment. Hit next and choose the appropriate option of internet connection. If you are not sure choose direct connection. Then choose a download site. (The websites are mirrors of the original cygwin website but just servers to relieve server traffic under a different url). Hit next to pick the packages you want. Some of the packages you would want are
  • Openssh
  • Python
  • keychain
  • GNU compiler or gcc
  • gdb
  • vi
  • emacs
  • git
  • make
  • subversion
  • bash
Some of this packages might not be needed for you class but they will be needed in future classes. However, if you want to add packages later you can. You just go through the same steps above except downloading the installer and then get the packages part and install anything else you need. Hit next until the installation begins.

After the installation you will need to add Cygwin to your class path.

Select [the windows logo] -> Windows System -> Control Panel

In the control panel search for System and select the [Edit the system environment] variables

select [Environment Variables]

Under user variables select [New] and proceed to add an new variable as follows:

Variable name: HOME

for the value you want to put the location you would want to act as your home directory. For example C:\\Users\Carlos\Documents but with your username instead of Carlos.

Variable value: location of home directory

Under the Environmental variables window select Path under "System Variables" window

select [edit]

Append a new path such as C:\\cygwin\bin and you may need a semicolon depending on the version of windows that you have. If you look at the current Path value you can notice if you need to add a semicolon or not.

Select "Ok" to finish editing. Select "Ok" again to finsih editing the environment variables. Select "Ok" to close the System properties.

Close the control panel.

Next you will create a shortcut for cygwin. I would recommend on the desktop. So go to your desktop right click and select new and then select shortcut.

select browse and then find the location of your cygwin terminal. It should be under the location you said to install Cygwin (the default is C:\\cygwin\) then under bin called mintty. So it could look like this C:\\cygwin\bin\mintty.

Then select next. Type a name for the shortcut for example Cygwin Terminal. Select [finish].

Cygwin Console

After installing Cygwin, we can use cygwin to run unix commands such as ls the only downside is that we cannot execute programs or scripts in a unix based terminal fashion. Therefore, the tutorial will now concern itself with the Cygwin console.
You can either jump to Using Linux via Command Line or continue on this tutorial.

Linux Instructions

At the beginning of the tutorial I gave UNIX and windows comparisons. You can refer to those comparisons with this table:

Windows UNIX Notes
del rm remove a file
rd rmdir removes a directory
dir [directory name] ls list the contents of the directory
cd or chdir pwd Tells the current directory location
cd or chdir [directory name] cd change the directory
rd /s /q rm -rf remove the directory recursively and forcefully
del /f rm delete a file
mkdirormd [directory name] mkdir make a new directory
To understand how to use terminal there are certain commands one needs to learn in order to get comfortable with the terminal. Some of the commands have been discussed but to reiterate or to recap the commands we want to be able to do is list the content of the directory, remove files or directories, change directories, copy files or directories, and optionally move files or directories.
First we will going over how to change directories. Changning directories is helpful and useful in many cases. To clarify, if you are not familiar with a direcctory it is just a folder. Hence, to change directories means to change folders. When you launch a terminal window you start at the home directory. That is indicated with the tilde ~. The $ indicates the console. We'll practice below with some
Carlos@hrn23502 ~
$ cd cs110

Carlos@hrn23502 ~/cs110
$
      

Other Resources

There are several Linux Tutorials on the web. Here are some useful resources recommended by Sophie Engle:

The best way to learn something is to just try it. It will be easier to remember if applying the concepts.