Introduction to the command-line interface
For readers at home: this chapter is covered in the Your new friend: Command Line video.
Itâs exciting, right?! Youâll write your first line of code in just a few minutes! :)
Let us introduce you to your first new friend: the command line!
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 this book 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.
Itâs probably under Applications â Accessories â Terminal, or Applications â System â Terminal, but that may depend on your system. If itâs not there, you can try to Google it. :)
You now should see a white or black window that is waiting for your commands.
The command-line Prompt
If youâre on Linux, you probably see a $, like this:
command-line
$
Each command will be prepended by a $ and one space, but you should not type it. Your computer will do it
for you. :)
Just a small note: in your case there may be something like
ola@Olas-PC:~ $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. Ignore the left part and only type in the command, which starts after the prompt.
Your first command (YAY!)
Letâs start by typing this command:
command-line
$ whoami
And then hit enter. This is our result:
command-line
$ whoami
olasitarska
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.
If you make a typo, you can use the left and right arrow keys to move your cursor, backspace and delete to edit the command. Most command lines donât support using the mouse to move the cursor.
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:
command-line
$ pwd
/Users/olasitarska
Note: âpwdâ stands for âprint working directoryâ.
Youâll probably see something similar on your machine. Once you open the command line you usually start at your userâs home directory.
Learn more about a command
Many commands you can type at the command prompt have built-in help that you can display and read! For example, to learn more about the current directory command:
macOS and Linux have a man command, which gives you help on commands. Try man pwd and see what it says,
or put man before other commands to see their help. The output of man is normally paged. Use the space
bar to move to the next page, and q to quit looking at the help.
List files and directories
So whatâs in it? Itâd be cool to find out. Letâs see:
command-line
$ ls
Applications
Desktop
Downloads
Music
...
Change current directory
Now, letâs go to our Desktop directory:
command-line
$ cd Desktop
Note that the directory name âDesktopâ might be translated to the language of your Linux account. If thatâs
the case, youâll need to replace Desktop with the translated name; for example, Schreibtisch for German.
Check if itâs really changed:
command-line
$ pwd
/Users/olasitarska/Desktop
Here it is!
PRO tip: if you type
cd Dand then hittabon 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 thetabkey twice to get a list of options.
Create directory
How about creating a practice directory on your desktop? You can do it this way:
command-line
$ mkdir practice
command-line
$ cd practice
$ mkdir test
$ ls
test
Congrats! :)
Clean up
We donât want to leave a mess, so letâs remove everything we did until that point.
First, we need to get back to Desktop:
command-line
$ cd ..
Using .. with the cd command will change your current directory to the parent directory (that is, the
directory that contains your current directory).
Check where you are:
command-line
$ pwd
/Users/olasitarska/Desktop
Now time to delete the practice directory:
Attention: Deleting files using
del,rmdirorrmis irrecoverable, meaning the deleted files will be gone forever! So be very careful with this command.
command-line
$ rm -r practice
Done! To be sure itâs actually deleted, letâs check it:
command-line
$ ls
Exit
Thatâs it for now! You can safely close the command line now. Letâs do it the hacker way, alright? :)
command-line
$ exit
Cool, huh? :)
Summary
Here is a summary of some useful commands:
| Command (Windows) | Command (Mac OS / Linux) | Description | Example |
|---|---|---|---|
| exit | exit | close the window | exit |
| cd | cd | change directory | cd test |
| cd | pwd | show the current directory | cd (Windows) or pwd (Mac OS / Linux) |
| dir | ls | list directories/files | dir |
| copy | cp | copy file | copy c:\test\test.txt c:\windows\test.txt |
| move | mv | move file | move c:\test\test.txt c:\windows\test.txt |
| mkdir | mkdir | create a new directory | mkdir testdirectory |
| rmdir (or del) | rm | delete a file | del c:\test\test.txt |
| rmdir /S | rm -r | delete a directory | rm -r testdirectory |
| [CMD] /? | man [CMD] | get help for a command | cd /? (Windows) or man cd (Mac OS / Linux) |
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.
Ready?
Letâs dive into Python!