Introduction to the Command-line Interface
May 17, 2018
The following steps will show you how to use 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.
Note Please note that throughout these tutorials we use the terms ‘directory’ and ‘folder’ interchangeably but they are one and the same thing.
What is the command line?
The window, which is usually called the command line or command-line interface, is a text-based application for viewing, handling, and manipulating files on your computer. It’s much like Windows Explorer or Finder on the Mac, but without the graphical interface. Other names for the command line are: cmd, CLI, prompt, console or terminal.
Open the command-line interface
To start some experiments we need to open our command-line interface first.
Windows
Go to Start menu → Windows System → Command Prompt.
On older versions of Windows, look in Start menu → All Programs → Accessories → Command Prompt.
OS X
Go to Applications → Utilities → Terminal.
Linux
It’s probably under Applications → Accessories → Terminal, but that may depend on your system. If it’s not there, just Google it. :)
Prompt
You now should see a white or black window that is waiting for your commands.
OS X and Linux
If you’re on Mac or Linux, you probably see $
, just like this:
$
Windows
On Windows, it’s a >
sign, like this:
>
Each command will be prepended by this sign and one space, but you don’t have to type it. Your computer will do it for you. :)
Just a small note: in your case there may be something like
C:\Users\ola>
orCL-MacBook-Air:~ ola$
before the prompt sign, and this is 100% OK.
The part up to and including the $
or the >
is called the command line prompt, or prompt for short. It prompts you to input something there.
In the tutorial, when we want you to type in a command, we will include the $
or >
, and occasionally more to the left. You can ignore the left part and just type in the command which starts after the prompt.
Your first command (YAY!)
Let’s start by typing this command:
OS X and Linux
$ whoami
Windows
> whoami
And then hit enter
. This is our result:
$ whoami
cl
As you can see, the computer has just printed your username. Neat, huh? :)
Try to type each command; do not copy-paste. You’ll remember more this way!
Basics
Each operating system has a slightly different set of commands for the command line, so make sure to follow instructions for your operating system. Let’s try this, shall we?
Current directory
It’d be nice to know where are we now, right? Let’s see. Type this command and hit enter
:
OS X and Linux
$ pwd
/Users/cl
Note: ‘pwd’ stands for ‘print working directory’.
Current directory: Windows
> cd
C:\Users\cl
Note: ‘cd’ stands for ‘change directory’. With powershell you can use pwd just like on Linux or Mac OS X.
You’ll probably see something similar on your machine. Once you open the command line you usually start at your user’s home directory.
List files and directories
So what’s in it? It’d be cool to find out. Let’s see:
OS X and Linux
$ ls
Applications
Desktop
Downloads
Music
...
Windows
> dir
Directory of C:\Users\cl
05/08/2014 07:28 PM Applications
05/08/2014 07:28 PM Desktop
05/08/2014 07:28 PM Downloads
05/08/2014 07:28 PM Music
...
Note: In powershell you can also use ls
like on Linux and Mac OS X.
Change current directory
Now, let’s go to our Desktop directory:
OS X and Linux
$ cd Desktop
Windows
> cd Desktop
Check if it’s really changed:
OS X and Linux
$ pwd
/Users/cl/Desktop
Windows
> cd
C:\Users\cl\Desktop
Here it is!
PRO tip: if you type
cd D
and then hittab
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 thetab
key twice to get a list of options.
Create directory
How about creating a practice directory on your desktop? You can do it this way:
OS X and Linux
$ mkdir practice
Windows
> mkdir practice
This little command will create a folder with the name practice
on your desktop. You can check if it’s there just by looking on your Desktop or by running a ls
or dir
command! Try it. :)
PRO tip: If you don’t want to type the same commands over and over, try pressing the
up arrow
anddown arrow
on your keyboard to cycle through recently used commands.
Exercise!
A small challenge for you: in your newly created practice
directory, create a directory called test
. (Use the cd
and mkdir
commands.)
Solution:
OS X and Linux
$ cd practice
$ mkdir test
$ ls
test
Windows
> cd practice
> mkdir test
> dir
05/08/2014 07:28 PM test
Congrats! :)
Clean up
We don’t want to leave a mess, so let’s remove everything we did until that point.
1. Get back to Desktop
First, we need to get back to Desktop:
OS X and Linux
$ cd ..
Windows
> cd ..
Using ..
with the cd
command will change your current directory to the parent directory (that is, the directory that contains your current directory).
2. Check where you are
OS X and Linux
$ pwd
/Users/cl/Desktop
Windows
> cd
C:\Users\cl\Desktop
3. Delete practice
Directory
Now time to delete the practice
directory:
Attention: Deleting files using
del
,rmdir
orrm
is irrecoverable, meaning the deleted files will be gone forever! So be very careful with this command.
Windows Powershell, OS X and Linux
$ rm -r practice
Windows Command Prompt
> rmdir /S practice
practice, Are you sure ? Y
4. Confirm deletion
Done! To be sure it’s actually deleted, let’s check it:
OS X and Linux
$ ls
Windows
> dir
Exit
That’s it for now! You can safely close the command line now. Let’s do it the hacker way, alright? :)
OS X and Linux
$ exit
Windows
> exit
Cool, huh? :)
Summary
Here is a summary of some useful commands:
These are just a very few of the commands you can run in your command line, but you’re not going to use anything more than that today.
If you’re curious, ss64.com contains a complete reference of commands for all operating systems.