Quick Guide to Using the Command Line

When you compile and run your C++ code, you will need to do so using the command line. I will give you the exact commands you need to run for compiling, but there are a few other important things to know about the command line so you can use it effectively.

I will discuss "running commands" in this guide, that means to type in the text of the command into your command line and then press "enter". When I talk about running a command in a directory, that means to make sure you have navigated into that directory (explained below) before running that command.

Cheat Sheet

These commands are described in more detail in the sections below.

Command Line

Command Usage
pwd Print the current "working directory", the one you are in
ls List everything in the current directory
cd <directoryname> Move to the directory named directoryname
mkdir <directoryname> Create a directory named directoryname in the current directory
cp <filename1> <filename2> Make a copy of filename1 called filename2
mv <filename1> <filename2> Move the contents of filename1 to filename2 (deletes filename1)
rm <filename> Delete (permanently and irreversibly) the file filename
vim <filename> Open the file named filename in vim

Vim

Command Usage
i Enter Insert Mode
Esc Exit whatever mode you're in
:w Save the file (write)
:q Exit vim
:wq Write the file and exit vim

Navigating Directories

When you open a file on your computer, first you have to open the folder it’s stored in (and perhaps a few folders, depending on your organization). You can do the exact same thing on the command line, just without the graphical user interface (GUI). Instead, you type commands to move between folders.

Where am I?

If you want to know what folder you are currently in, you use the pwd command. It stands for "print working directory".
Screenshot of running pwd on CSIL
The output will always start with a slash, and there will be a slash between each folder name. In this case, I am in my home directory on CSIL, which is named “maradowning”. There are also the student and cs folders higher up, but since this is CSIL, I don’t actually get to access those.

What's here?

Normally when you look at a folder on your computer, you can see everything stored in it. You can do the same thing with the command line, you just need to type the right command: ls.
Screenshot of running ls on CSIL
Note that this is an "L", not a one. This one means "list", because it lists out the things in your folder. In this case, I just have two folders stored here, "example", and "old_stuff".

How do I move to a different folder?

To change folders, you want the cd command, which stands for "change directory". You also want to give it the name of the directory you want to go to. This can be an "absolute path" or a "relative path", and the trick here is whether or not it begins with a slash. To move into the "example" directory, I could run either
cd /cs/student/maradowning/example
or
cd example
Both will do the exact same thing. The difference is that the first, absolute path, will work regardless of the directory I am currently in, whereas the second only works from the "maradowning" directory.

While relative paths work great here where you’re moving down in the directory structure, what if you want to go back up?
The command line has names for both the directory you are currently in, and the "parent" directory (the directory that this one is stored in). The one you are currently in is called ".", which means you could always run
cd .
and stay exactly where you are. This isn’t super useful. The parent directory, on the other hand, is named "..", and so you can run
cd ..
to move up one directory in the hierarchy.

How do I make a folder?

You can run the mkdir command ("make directory"). It will make a new directory inside of your current one:
Screenshot of running mkdir on CSIL

Dealing with Files

Two very similar commands you can use for files are cp (copy) and mv (move). These both take two arguments: the file to be copied or moved, and a destination file. For example, if I want to copy myfile.txt into mysecondfile.txt, I would run:
cp myfile.txt mysecondfile.txt Now I have two identical files in the same directory, except for their names.

Moving a file can both be used to move it to a new directory, or to rename it. For example, if I want to move a file into the parent directory, I run:
mv myfile.txt ../myfile.txt or mv myfile.txt ../
Both of these are ok because the mv command recognizes that .. is a directory, not a file name, so the file is just moved into that directory with its original name. It is still ok to tell it exactly the name though, as in the left example.

If we want to rename a file with mv, we can run:
mv myfile.txt myrenamedfile.txt
This will essentially delete myfile.txt but move its contents into myrenamedfile.txt, which is in effect the same as renaming it.

Another super important command when working with files is rm (remove). You should be a little careful with this one, because unlike moving a file to trash, rm is irreversible. To use rm, you just list any files you want to delete:
rm myfile.txt
rm myfile.txt mysecondfile.txt
You can list as many files as you want, separated by spaces.

VERY IMPORTANT NOTE ON RM: If you are in the habit of using * as a “wild” character, be extra extra careful when using it alongside rm. You do not want to accidentally delete everything in your folder.

Editing Files with Vim

Vim is a super useful and incredibly complicated text editor for the command line. It comes pre-installed on CSIL. To open a file with vim, just run:
vim samplevimfile.txt
The file you open (in this case filename.txt) doesn’t have to exist in advance, if you type in a filename that doesn’t exist yet vim will just create that file for you and open up an empty text editor. For the sake of this tutorial, let’s assume this is a file that exists and has some text in it.
From here, you will be in the vim editor:
Screenshot of open VIM
Currently we just have one line in it---the blue tildes indicate that the lower lines don’t really exist, but our terminal window is longer than the file. On the bottom we see 1,1: this means we’re on the first character of the first line, which we can see as our cursor is currently over the H. When you’re looking at line numbers in compiler errors, this will be super useful.
The most important mode we will use in vim is Insert Mode, which we can enter by tapping "i".
Screenshot of VIM in insert mode
If you are unsure what mode you are in, the bottom left corner should tell you. Once you enter insert mode, you can type as usual, and use the arrow keys to move around. However, as this is still within the command line, clicking will not move your cursor, only arrow keys.
When you are finished writing in your file (or if you accidentally entered a mode you don’t want to be in), use the escape key to get out. This will return you to the initial state, with an empty bottom left corner.

When you want to save your file, you will need to first escape insert mode, and then you type “:w” and hit enter. This is known as “writing” the file—it writes the contents to storage. You will still be in vim after you do this though, so if you want to exit back to the command line you will then need to type “:q” and hit enter. If you are looking for a bit more speed while working, you can combine these into “:wq” and save and exit your file at the same time.

Vim is an absolutely gigantic program, and these few commands are only the absolute basics of working with it---if you choose to use vim primarily to edit files in this class, you will most likely want to look up some more. However, just as the basics, these commands will let you open, edit, and save files without too much hassle.

Another common text editor for the command line is nano. I won’t go in depth into how to use nano, mostly because if you open up a file with nano, it lists all of the useful commands right at the bottom of the screen for you. The most importnat thing to remember is that when nano lists a command with the "^" character, this means the Ctrl key.