Using the Command Line
Up till now, most people have only interacted with their computer by clicking around on graphical interfaces created to make things easy. However, many tasks, such as editing files, browsing our file system, and running code, can be done through commands entered into our computer's command line. Command line tools are critical for anyone interested in delving into software engineering; they are also the main way we'll be interacting with the file system while doing web development. Don't worry if a lot of this seems complicated or foreign. The goal is to just get acquainted with the tool here and then to grow comfortable through experience using it.
Windows Users (prior to Windows 10 Bash)
This tutorial asks you to copy and past commands into your terminal. If you have a windows computer prior to the introduction Windows 10 bash, you don't have a terminal. Instead, you have a Command-Line or Command-Prompt. They do the same things, but the commands might differ a bit. I'd recommend following along until you see that a command doesn't work. At that point, just Google what the correct command is and all will be well :)
Note: From here on out I will be referring to the command-line as Terminal, even though I mean CMD if you are on a Windows.
Opening and Using Terminal (pwd)
As mentioned in the previous section, Mac users should open up the Terminal and Windows users should open up Cygwin. When you open it up you should see the following application:
While the formatting of your terminal might look different (colors, font, etc), the general idea is that there is a cursor and area for you to type into. What you essentially are looking at is the equivalent of looking into a folder in your Finder file system. When you open your terminal, you always open to your home directory. To find out what directory you currently in, type the command pwd, or present working directory:
As you can see here, I am currently two folders deep inside of the Rohan/ folder which is contained inside of the Users/ folder. Note that the highest level folder is called "root", which is denoted by a single "/".
Viewing Files in Directory (ls)
While we know we are inside a specific folder, we aren't able to see the contents of the folder the same way we can do that when we are using Finder. However, fear not! The ls, or list, command shows us the contents of the directory that we are currently in:
Here notice how ls showed me other folders, like Desktop/, and also files, like wwooo.py.
Changing Directories (pwd)
Now that we can see what folder we are in with pwd, and see what is inside of that folder with ls, the next logical thing would be to change into different folders and move around our file system. The command to do this is cd, or change directories:
Here I am changing into my Desktop/ from Rohan/. Note that once you change directories and then do pwd, the present working directory reflects the changed directory.
In order to go backwards, or up your file system tree, (i.e. go from Desktop/ back to Rohan/), you have to use the command "cd ..":
All of the navigation between folders that we have done so far has all been relative, i.e. we are changing from our current folder to another folder that is inside of the current folder or one level higher than the current folder. However, sometimes we will want to go to a folder that is far away from where we currently are. In order to do this we have to change directories to an absolute path. Absolute paths start with a "/" indicating that the folder path you are giving starts from the root folder.
Making a folder (mkdir)
Now that we can easily navigate through our file system, let's start actually interacting with it by changing it! First, the way to make a new directory is with the mkdir command:
Notice that when we change into the new folder1/ that we created, ls shows us that it is empty. This is what we expect because we only just made it!
Making and Editing files (vim)
Now, let's talk about how to actually make a file and edit that file inside of the terminal. There are a few tools out there to edit files inside of the terminal and there are large debates over which one is best. I was taught how to use vim first and so I am just going to go with that (please don't take this as a political statement).
The way to open a file in vim is to type vim <filename> where <filename> is the file you are trying to open. I the file doesn't exist in the current directory that you are in, it will create a blank file with that name.
When you press enter and run the "vim test.py" command, you should see a screen that looks like the image above. What you are looking at is the vim command line editor. The main thing to understand about using vim is that it has two different modes:
- Command mode: This is the mode that it is in when you open it. When it is in command mode, typing doesn't produce text in the file. Instead, different characters stand for different commands that we use to perform different actions like saving, undoing, copying and pasting, etc.
- Insert/Edit mode: This mode can be switched into from command mode by pressing "i" while in command mode. While in insert mode, you can type as a mostly normal text editor. To exit insert mode and go back to command mode, you press escape.
The last thing we need to know to use this editor is understand how to save files. In order to save a file, first use Esc to be in command mode. Then type in the characters ":wq" and then press enter. "w" is the command to save and "q" is the command to quit. Thus, this effectively saves and quits out of the file, successfully creating the new file that we wanted to make.
Running a Python File (python)
Now that we have made a python file, let's run it from the command line! In order to do this you simply type the python <filename>:
In summary, there are a few different commands that are important to know in order to use the command-line:
- pwd: shows the present working directory
- ls: lists the files in the current directory
- cd <path>: changes directories from current directory to the directory specified by <path>
- mkdir <name>: creates a folder with the name <name> inside the current directory
- vim <filename>: opens <filename> to be edited in vim. If <filename> doesn't exist in the current directory, it creates a new file with that name
Now we'll talk about installing the modules we need and setting up the environment in which we are going to do development.wd