The Command Line
How do we tell our computers what to do?
Last updated
Was this helpful?
How do we tell our computers what to do?
Last updated
Was this helpful?
File Explorer: part of the GUI that allows us to find and interact with the files on a computer
command line: a way to give written instructions to a computer
command line interpreter: a program that turns command line keywords into instructions that a computer can understand and execute
Command Prompt: a command line interpreter mostly used on Windows computers
directory: a place for storing files or other directories (similar to a folder)
path: a description of where a file or directory is located on a computer
Note: The instructions and the images in this section refer to a Windows computer.
In order to use computers to help us accomplish tasks, we have to be able to give computers instructions. For example, if we wanted to open a file called hello.txt
in the Documents folder on our computer, we would first have to tell the computer how to find it.
To open the file, we could click on the File Explorer icon to see all of our files, double-click on the Documents folder, and then double-click the hello.txt
file icon. Clicking on folders and files with your mouse is one way to navigate your computer and give it instructions.
Another way to communicate with your computer is through the command line, which uses written commands instead of visual icons.
The Command Prompt is a command line interpreter, which means that it can take commands written by a user and interpret them so that the computer can execute those instructions. Using Command Prompt, we can enter specific, predefined keywords that the interpreter understands to give the computer written instructions.
When we open up the Command Prompt, it should look something like this:
To find and open files using the Command Prompt, it is important to know which directory we are currently in. A directory is similar to a folder in File Explorer — it can be contained inside of another directory and it can contain other directories or files inside of it. In the Command Prompt example above, we can look to the left of the cursor to see that the current directory we are in is C:\Windows\System32
. The line C:\Windows\System32
tells you the path to that directory, separated by backslashes — the System32
directory is located in the Windows
directory, which in turn is located in the C:
directory.
Note: Your Command Prompt may not begin in the same directory as the example.
If we want to navigate to a different directory, we can use the the keyword cd
, which stands for 'change directory.' We can use this command in one of two ways.
Moving up one directory: This is the equivalent of clicking the back button in File Explorer to exit a folder and return to the folder that contains it. To do this in the command line, we can write: cd ..
. Note that this command is relative to the directory you are currently located in, which means it can only be used to move one directory up from the current directory. See Figure 3 for an example.
Moving down one directory: This is the equivalent of double-clicking on a sub-folder inside the current folder to enter the sub-folder. To do this in the command line, we can write: cd SUBDIRECTORY
. Note that you should replace SUBDIRECTORY
with the name of a valid subdirectory contained by the directory you are currently located in. See Figure 4 for an example.
It doesn't matter whether we use a forward slash (/) or backslash (\) here; the computer will recognize both.
Figure 3: The command cd ..
allows us to move up one directory. In this example, we are exiting the System32
directory and moving up to the directory that contains it, which is the Windows
directory.
Figure 4: The command cd SUBDIRECTORY
allows us to move to a subdirectory of the current directory.
The Command Prompt also allows us to do a lot of other things besides navigating through directories! For example, we can use the command line to run a program. A computer program gives the computer a set of instructions and tells it to complete them. The command prompt can also be used as a text editor — a type of program that allows us to create and edit text files. One text (and style) editor you might be familiar with is Microsoft Word. In this course, we will be teaching you how to create and run basic programs using a programming language called Python. One way to create and run these programs is using the command line.