Skip to the content of the web site.

Cygwin terminal

IEEE Spectrum Magazine    MIT Technology Review    Wired Magazine    EDN Network    EE Times    Skeptic's Guide to the Universe

In Windows, after you have installed Cygwin, you can launch a Cygwin64 Terminal:

Associated with the state of the terminal is a working directory, or in the parlance of Windows, a current directory. Specifically, by default when you open a Cygwin64 Terminal, the working directory is /home/your-user-id.

dwharder@CHURCHILL ~
$ pwd
/home/dwharder

By default, there are no files in your home directory. To view a directory listing, use the ls command, which gives a directory listing:

dwharder@CHURCHILL ~
$ ls

To change the working directory, you will use the cd command. If you use the cd command without any arguments, it will always change the working directory back to your home directory.

If you want to change to a different directory, you must either specify a relative path or an absolute path. For example, an alias for the parent directory is always .. while an alias for the current directory is ., so you can always move to the parent directory by using the command cd ..:

dwharder@CHURCHILL ~
$ cd ..

dwharder@CHURCHILL /home
$ ls
dwharder

Of course, you will see your user id as the only sub-directory.

dwharder@CHURCHILL /home
$ cd ..

dwharder@CHURCHILL /
$ ls
bin       Cygwin.bat  Cygwin-Terminal.ico  etc   lib   sbin  usr
cygdrive  Cygwin.ico  dev                  home  proc  tmp   var

You will note that the directory listing includes all files and directories. If you want some more information, use the -l (or long) option:

dwharder@CHURCHILL /
$ ls -l
dwharder@CHURCHILL /
total 337
drwxr-xr-x+  1 pc-admin None                0 May 21 12:43 bin
dr-xr-xr-x   1 dwharder None                0 May 28 13:35 cygdrive
-rwxr-xr-x   1 pc-admin None               88 May 21 12:45 Cygwin.bat
-rw-r--r--   1 pc-admin Administrators 157097 May 21 17:28 Cygwin.ico
-rw-r--r--   1 pc-admin Administrators  53342 May 21 17:28 Cygwin-Terminal.ico
drwxr-xr-x+  1 pc-admin None                0 May 21 12:45 dev
drwxr-xr-x+  1 pc-admin None                0 May 21 12:45 etc
drwxrwxrwt+  1 pc-admin None                0 May 21 17:30 home
drwxr-xr-x+  1 pc-admin None                0 May 21 12:43 lib
dr-xr-xr-x  10 dwharder None                0 May 28 13:35 proc
drwxr-xr-x+  1 pc-admin None                0 May 21 12:43 sbin
drwxrwxrwt+  1 pc-admin None                0 May 28 13:11 tmp
drwxr-xr-x+  1 pc-admin None                0 May 21 12:43 usr
drwxr-xr-x+  1 pc-admin None                0 May 21 12:43 var

Directories are highlighted by an initial d. Other information includes the file size and the owner as well as the date and time the file was last modified. You can see here that I installed Cygwin on May 21st.

To get to the rest of your files, Cygwin links to your Windows drives in the /cygdrive directory:

dwharder@CHURCHILL /
$ cd cygdrive

dwharder@CHURCHILL /cygdrive
$ ls
c   d

In the c sub-directory is the root of C:\ in Windows:

dwharder@CHURCHILL /cygdrive
$ cd c

dwharder@CHURCHILL /cygdrive/c
$ ls
'$Recycle.Bin'             PerfLogs               swapfile.sys
 cygwin64                 'Program Files'        'System Volume Information'
'Documents and Settings'  'Program Files (x86)'   Temp
 MSOCache                  ProgramData            Users
 pagefile.sys              Recovery               Windows

If a file or directory name has a special character in it (a space, a dollar sign, etc.), the name appears in single quotes.

Now that you understand how to navigate in the Cygwin64 Terminal we give you two options for generating and editing code in the terminal environment:

  1. Frist, we will assume you have been using Eclipse and would like to either compile or edit your source code in the Eclipse workspace.
  2. Second, if you prefer, you can simply use the tools in Cygwin to create, edit, compile and test your source code.

You may choose either.

Compiling and possibly editing source code created in Eclipse

dwharder@CHURCHILL /cygdrive/c
$ cd Users/dwharder/eclipse-workspace

The directory Users is a sub-directory of the current working directory, dwharder is a sub-directory of Users, and eclipse-workspace is a sub-directory of my Windows home directory.

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace
$ ls
'Project 0'

Thus, currently, I have a single project in this directory.

To return to the Cygwin home directory, we have

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace
$ pwd
/cygdrive/c/Users/dwharder/eclipse-workspace

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace
$ cd

dwharder@CHURCHILL ~
$ pwd
/home/dwharder

You will now notice something else: up until now, the current working directory appears in the line containing dwharder@CHURCHLL; however, when you are in your home directory, the directory name is replaced by ~. The tilde (till-deh) is an alias for your home directory. Thus, any time I want to write /home/dwharder, I could replace this absolute path with ~.

You can also change to a directory using an absolute path from the root directory:

dwharder@CHURCHILL ~
$ cd /cygdrive/c/Users/dwharder/eclipse-workspace

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace
$ pwd
/cygdrive/c/Users/dwharder/eclipse-workspace

This would, however, be frustrating to always type the absolute directory. Consequently, we will create a symbolic link in your Cygwin home directory:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace
$ cd

dwharder@CHURCHILL ~
$ ln -s /cygdrive/c/Users/dwharder/eclipse-workspace eclipse-workspace

dwharder@CHURCHILL ~
$ ls
eclipse-workspace

We could now type cd eclipse-workspace, but this, too is tedious. The Cygwin64 Terminal allows you to use the Tab key to complete a unique name, so start typing:

dwharder@CHURCHILL ~
$ cd ec

Now press the Tab key, and because there is only one directory or file that starts with ec, it will fill in the remaining characters, adding a / at the end, as it is a directory:

dwharder@CHURCHILL ~
$ cd eclipse-workspace/

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace
$ pwd
/cygdrive/c/Users/dwharder/eclipse-workspace

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace
$ ls
'Project 0'

You can now change to this directory by typing

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/
$ cd P

and pressing the Tab key:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/
$ cd Project\ 0/

The \ that appears in front of the space indicates that the space is part of the name of directory. The backspace is the escape character in Cygwin64 Terminal (we will discuss escape characters in class when we describe strings). If you do not have spaces in your project names you will not ever have to deal with escape characters while using the Cygwin64 Terminal.

If you do a directory listing, you will see that your source file is there, but Eclipse also created a Debug directory:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ ls
Debug  main.cpp

We will ignore the debug directory and look at compiling the source file. The compiler is a program that takes a source file as an argument and compiles that source file into an executable.

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ g++ main.cpp

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ ls
a.exe  Debug  main.cpp

The default name of the executable is a.exe. You can change the name by using the output option -o:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ g++ -o main.exe main.cpp

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ ls
a.exe  Debug  main.cpp  main.exe

To delete a file, you can either to back into Windows and remove the file using the File Explorer application, or you can use the remove command:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ rm a.exe

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ ls
Debug  main.cpp  main.exe

Okay, now let's run this executable file:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ main.exe
-bash: main.exe: command not found

But the file is there!? You may be wondering why it is not found, especially if you are already familiar with Window's Command Prompt.

You may recall that when you installed Cygwin, you had to update the Path environment variable. There is a similar PATH environment variable in Unix:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ echo $PATH
/usr/local/bin:/usr/bin:/cygdrive/c/Program Files (x86)/Common Files/O
racle/Java/javapath:/cygdrive/c/windows/system32:/cygdrive/c/windows:/
cygdrive/c/windows/System32/Wbem:/cygdrive/c/windows/System32/WindowsP
owerShell/v1.0:/cygdrive/c/windows/System32/OpenSSH:/cygdrive/c/Progra
m Files/MiKTeX 2.9/miktex/bin/x64:/cygdrive/c/Program Files/PuTTY:/cyg
drive/c/Program Files/MATLAB/R2019a/runtime/win64:/cygdrive/c/Program 
Files/MATLAB/R2019a/bin:/cygdrive/c/Cygwin64/bin:/cygdrive/c/Users/dwh
arder/AppData/Local/Microsoft/WindowsApps

These are the only places that Cygwin64 Terminal will look for an executable to run, and it does not include the current working directory. Rather than looking at this text, here are these directories:

  • /usr/local/bin
  • /usr/bin
  • /cygdrive/c/Program Files (x86)/Common Files/Oracle/Java/javapath
  • /cygdrive/c/windows/system32
  • /cygdrive/c/windows
  • /cygdrive/c/windows/System32/Wbem
  • /cygdrive/c/windows/System32/WindowsPowerShell/v1.0
  • /cygdrive/c/windows/System32/OpenSSH
  • /cygdrive/c/Program Files/MiKTeX 2.9/miktex/bin/x64
  • /cygdrive/c/Program Files/PuTTY
  • /cygdrive/c/Program Files/MATLAB/R2019a/runtime/win64
  • /cygdrive/c/Program Files/MATLAB/R2019a/bin
  • /cygdrive/c/Cygwin64/bin
  • /cygdrive/c/Users/dwharder/AppData/Local/Microsoft/WindowsApps

You can even ask where an executable is:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ which g++
/usr/bin/g++

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ which ls
/usr/bin/ls

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ which pwd
/usr/bin/pwd

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ which cd
which: no cd in (/usr/local/bin:/usr/bin:/cygdrive/c/Program Files (x8
6)/Common Files/Oracle/Java/javapath:/cygdrive/c/windows/system32:/cyg
drive/c/windows:/cygdrive/c/windows/System32/Wbem:/cygdrive/c/windows/
System32/WindowsPowerShell/v1.0:/cygdrive/c/windows/System32/OpenSSH:/
cygdrive/c/Program Files/MiKTeX 2.9/miktex/bin/x64:/cygdrive/c/Program
 Files/PuTTY:/cygdrive/c/Program Files/MATLAB/R2019a/runtime/win64:/cy
gdrive/c/Program Files/MATLAB/R2019a/bin:/cygdrive/c/Cygwin64/bin:/cyg
drive/c/Users/dwharder/AppData/Local/Microsoft/WindowsApps)

You will note that almost all commands are executable files, with very few exceptions like cd.

If you want to execute a file in the current directory, you must give either an absolute path relative path to the directory. You will recall that the alias for the current directory is ., so the following works:

dwharder@CHURCHILL /cygdrive/c/Users/dwharder/eclipse-workspace/Project 0
$ ./a.exe
Hello world!

If you want to edit your source file, first make absolutely sure that you have closed the corresponding project in Eclipse. There are many options for editing source code:

Eclipse
You can continue to use the Eclipse editor for making changes to your source code.
vim
If you want to spend a lot of time learning a very useful editor, the improved version of vi, or vim is always installed on all Unix platforms.
Notepad++
A Windows program where you can use File Explorer to navigate to the appropriate directory and then edit the source file.
gvim
If you liked vim so much, you can also install gvim on Windows.

There are hundreds of other editors available: you are welcome to ask your peers and friends what they use.

Creating, editing and compiling source code created in Cygwin

If you intend to create, edit, compile and test your source code in the Cygwin environment, you can simply enter the command cd which will take you back to your home directory in Cygwin, and you can continue you develop your code as if you were in Unix.