Let us introduce you to your first new friend: the command line!
This is the black window all hackers use. It might look a bit scary at first but really it's just a prompt waiting for commands from you.
The command line is a text-based application for controlling your computer. Other names for it are: cmd, command-line interface (CLI), prompt, console or terminal.
It can be used for viewing, handling, and manipulating files, much like Windows Explorer or Finder on the Mac, but without the graphical interface. Instead of clicking, you'll type in commands using a keyboard!
Why do it this way? Graphical windows and buttons are familiar to most people, but they have two disadvantages:
Although it may not be the easiest way to start, this workshop is about giving you a taste of what programmers do, so we're starting here.
The way to open the command line (also known as console or terminal) is different on different systems:
If you don't know what to do, try Googling or asking someone more experienced.
After opening the console, you will be greeted by a prompt: a line that asks you to enter a command.
The prompt ends with the character $
on Unix systems (such as Linux and macOS); and with >
on Windows.
Before the $
or >
symbol, there will probably be some additional information, which is different on each computer.
The examples here (like most other instructions you find on the internet) will omit that information, and just show the final $
or >
like this:
$
>
On Windows, if the font is too small, click on the window icon and select Options. In the Font tab, you can then choose a larger font.
On other systems, look for settings or try Ctrl++ and Ctrl+- (or with Shift).
Let's start with your first command: whoami
(the question “who am I” without spaces).
Type it after the $
or >
and press Enter.
Your login name will appear.
For example, if your name was Helena the conversation would look like this:
$ whoami
helena
> whoami
computer\Helena
The character $
or >
in this example is only there to make it clear that you are entering a command into the command line.
The computer will display it, usually with something before it, so
don't type it in! Just enter whoami
and press Enter.
Similarly, the computer will display the login name on its own.
Let's try some more commands! Each computer can have a slightly different set of commands, so make sure to follow instructions for your operating system.
It'd be nice to know where are we now, right?
The command line always works in some directory, also known as a folder (these mean one and the same thing).
The command pwd
or cd
(from print working directory or current directory) will tell you in which directory you currently are.
Try it! You'll see something like this:
$ pwd
/home/helena/
> cd
C:\Users\helena
The current directory is usually also displayed somewhere in the command line prompt, before the $
or >
sign.
But in the future, you may have to work on a computer that displays it differently or not at all, so it's good to know the pwd
/cd
command.
You might be familiar with a “current directory” from the window used to select files in graphical programs. Typically, this window shows the contents of a single directory.
The command line can also show what's in the current directory, but you have to ask for it.
The command ls
or dir
(short for list or directory) will show you what the current directory contains: all the files, including subdirectories.
Try it!
$ ls
Applications
Desktop
Downloads
Music
…
> dir
Directory of C:\Users\helena
05/08/2014 07:28 PM <DIR> Applications
05/08/2014 07:28 PM <DIR> Desktop
05/08/2014 07:28 PM <DIR> Downloads
05/08/2014 07:28 PM <DIR> Music
…
Now, let's go to your Desktop
directory.
(If you don't see Desktop in the output from ls
or dir
, choose a different
one.)
Type cd
, a space, and the name of the directory you want to go to.
If you use Linux or macOS, be careful with letter case: on these systems, Desktop
and desktop
are two different names.
$ cd Desktop
$ pwd
/home/helena/Desktop
> cd Desktop
> cd
C:\Users\helena\Desktop
Afterwards, verify that you are in the right place using pwd
or cd
.
If you use Windows, you have already used cd
- this command behaves differently depending on whether you write something after it or not.
Note for Windows
If you are switching to a directory on a different drive, for example D:
instead of C:
,
you need to enter the name of the drive with a colon (for example, D:
) as a separate command, in addition to cd
.
PRO tip
If you type cd D
and then hit tab on your keyboard,
the command line will automatically fill in the rest of the name
so you can navigate faster.
If there is more than one folder starting with D,
hit the tab key twice to get a list of options.
How about creating a practice directory on your desktop?
For that you can use the command mkdir
(short for make directory) followed
by the name of the directory you want to create.
Try it:
$ mkdir test
> mkdir test
After you do this, try to list the current directory using ls
or dir
.
One of the listed directories will be the newly created test
.
Next, navigate to the new directory it in the same way
as to Desktop
a moment ago:
$ cd test
> cd test
The command line is powerful, but sometimes it's not the right tool for what you need to do. Let's learn how to open the command line's current directory elsewhere.
Open your system's file browser.
You can navigate by clicking around, but that can take too long and sometimes you can get lost. Let's instead copy a directory name from the command line and paste it into the file browser.
First, you need something to copy.
Use cd
or pwd
to display the full name of your current directory, test
:
$ pwd
/home/helena/Desktop/test
> cd
C:\Users\helena\Desktop\test
Unfortunately, the way to copy text is different on every system. Even more unfortunately, on most systems you can't use the well-known shortcuts Ctrl+C and Ctrl+V to copy/paste in the command line.
On Linux, select the text with your mouse and then either:
On macOS, select the text with your mouse and then press ⌘ Command+C.
On Windows, in the command prompt menu (icons in the upper left corner), select Edit → Mark, select the text with your mouse, and copy it using Enter.
On Linux, depending on the file browser you are using, either:
On macOS, select the Go menu → Go to Folder, paste the directory name using ⌘ Command+V and confirm by pressing Enter.
On Windows, click on the name of the directory at the top. This will make the text editable. Delete the existing directory and use Ctrl+V to paste the new one instead. Confirm with Enter.
Now, you should have Desktop open in your graphical window.
Check that you can see test
, the directory you created earlier.
Sometimes you'll open a directory in the file browser and you will want to switch to it in the command line.
Copy the directory name from the file browser. (You might need to make the text editable, as when pasting.)
Then, paste it into the command line. The shortcut for that can again be different than what you're used to:
If there are spaces or other special characters such as *#$%^()><;"?
in the name, you must enclose it in quotes in the command line. Before and after the name, type "
, for example:
$ cd "my super directory"
You can also rename directories and files to remove any spaces and special characters. That'll make them easier to work with in the command line.
The files on your computer can be manipulated by both graphical programs and the command line. Let's test that!
In your graphical browser, you should have the command line's current directory open. Create a new file or directory there.
Then switch to the command line, and use ls
or dir
to check that it was actually created.
Now delete it in the graphical program – and verify that you can't see it any more in the command line.
Remember cd
? There's one more trick you'll need.
If you are now in the directory Desktop/test
or Desktop\test
, how do you get back to Desktop
?
The command cd Desktop
will not work: it would tell the computer to switch to Desktop
in the current directory, or Desktop/test/Desktop
.
But there's no directory like that.
And the command line does not dare second-guess what you mean.
The parent directory (the one contains the current one) has a special name: ..
– two dots.
Go to it by typing cd ..
and then make sure that you are really back in Desktop
.
$ cd ..
$ pwd
/home/helena/Desktop
> cd ..
> cd
C:\Users\helena\Desktop
Another cd ..
would move you to the next parent directory - in our example, helena
.
There are many more commands you can use on the command line.
When you learn them, you can fully control the computer just using text: create files, delete them, run programs, change settings, and so on. However, it would take a separate course to cover all of that.
So let's wrap up with just one more command – one that closes the command line:
$ exit
This command is the same on all systems, so I didn't give a separate example for Windows.
And in the single example, I used the Unix-style prompt, $
rather than >
.
Many online tutorials do that.
They mean the same thing: the computer is asking for your command.
Here is a table of basic commands that you will need to get started.
Unix | Windows | Description | Example |
---|---|---|---|
cd directory |
cd directory |
Change directory | cd test cd .. |
pwd |
cd |
Print working directory | pwd cd |
ls |
dir |
List directory contents | ls dir |
exit |
exit |
Exit | exit |
There are many others, and we'll learn about them when as we need them.