|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Introduction to the command line |
| 4 | +--- |
| 5 | + |
| 6 | +## What is the command line? |
| 7 | + |
| 8 | +The command line is a text interface for your computer. Just like Windows Explorer on Windows or Finder on Mac OSX it let’s you navigate through the files and folders of your computer, but it is completely text based. The command line works by typing commands against a prompt, which then gets passed to the operating system of the computer that runs these commands. |
| 9 | + |
| 10 | +## How do I access the command line? |
| 11 | + |
| 12 | +To access the command line, we use a terminal emulator, usually called a terminal. On Mac OSX you can access the terminal by opening the Terminal application from your Applications folder. On Windows you should use a program called the PowerShell. |
| 13 | + |
| 14 | +## Example 1: navigating around in the terminal |
| 15 | + |
| 16 | +Once you opened up your terminal, type in the following after the $ or > sign and hit enter: ($ or > is the prompt, you don't have to retype that in the terminal, only the characters that come after them): |
| 17 | + |
| 18 | +```bash |
| 19 | +$ pwd |
| 20 | +``` |
| 21 | + |
| 22 | +> What do you think happened there? In your own words, try to explain what this command does. |
| 23 | +
|
| 24 | +### `pwd` or print working directory |
| 25 | + |
| 26 | +The `pwd` command prints out the current directory you are in. What are directories? Directories are folders, these terms are used interchangeably. If you just opened up your terminal, you are probably in the root directory of your computer, and should get an output similar to this: |
| 27 | + |
| 28 | +```bash |
| 29 | +/Users/your-username |
| 30 | +``` |
| 31 | + |
| 32 | +Now you know how to tell where you are in the folder structure of you computer, you might ask yourself: but if there is no visual user interface in the terminal how do I know what directories are in what directories? That's where the `ls` command comes in handy. |
| 33 | + |
| 34 | +### `ls` or list |
| 35 | + |
| 36 | +In your terminal type: |
| 37 | + |
| 38 | +```bash |
| 39 | +$ ls |
| 40 | +``` |
| 41 | + |
| 42 | +and hit enter. Most likely this command returned you a bunch of files and directories (folders). The `ls` command prints out the contents of a directory. If you are in the root directory of your computer you should see directories printed out such as Documents, Applications, etc. Now the question is, how do I move between directories? |
| 43 | + |
| 44 | +### `cd` or change directory |
| 45 | + |
| 46 | +The `cd` prompt allows you to move between directories. The `cd` command takes an argument, usually the name of the folder you want to change into, so the full command is `cd your-directory`. |
| 47 | + |
| 48 | +In the terminal, type: |
| 49 | + |
| 50 | +```bash |
| 51 | +$ ls |
| 52 | +``` |
| 53 | + |
| 54 | +Choose a directory you want to change into, and type: |
| 55 | + |
| 56 | +```bash |
| 57 | +$ cd your-directory |
| 58 | +``` |
| 59 | + |
| 60 | +Now type: |
| 61 | + |
| 62 | +```bash |
| 63 | +$ pwd |
| 64 | +``` |
| 65 | + |
| 66 | +This should return you name of the directory you just `cd` into. |
| 67 | + |
| 68 | +Type `ls` into the terminal, choose another directory and `cd` into it. We have just changed into a new directory. You can use these two commands to navigate around the directory structure of your computer. This is all good so far but sometimes you might want to go deeper than one level in one command. `cd` allows you to do this by chaining the directories with a `/`, so `cd your-directory` becomes `cd your-directory/directory-inside-your-directory`. |
| 69 | + |
| 70 | +We now know how to move forward but how to go back up the directory tree? In your terminal type: |
| 71 | + |
| 72 | +```bash |
| 73 | +$ cd .. |
| 74 | +``` |
| 75 | + |
| 76 | +Now do a `pwd`. You just went back one directory! Chaining works backward too, so if you type `cd ../..` you should be taken back two directories. |
| 77 | + |
| 78 | +> If you want to go back to the root directory of your computer, simply type `cd` into the terminal. `cd` without an argument takes you back to the root directory regardless of where you are currently in the directory structure |
| 79 | +
|
| 80 | +### Exercise 1: use `ls` and `cd` to move in and out of a few directories on your machine |
| 81 | + |
| 82 | +Those are the basics of navigating around in the terminal. What else would we want to do in there? How about creating directories and files? |
| 83 | + |
| 84 | +## Example 2: creating directories and files |
| 85 | + |
| 86 | +### `mkdir` or make directory |
| 87 | + |
| 88 | +Go back to the root directory of your computer, and type: |
| 89 | + |
| 90 | +```bash |
| 91 | +$ mkdir temp |
| 92 | +``` |
| 93 | + |
| 94 | +into the terminal. Now use `ls` to see the contents of the root directory. You should see a new folder, temp there. You just created a new folder! As it's name suggests, mkdir creates directories. What if we wanted to create a directory inside a directory? `cd` into temp and type: |
| 95 | + |
| 96 | +```bash |
| 97 | +$ mkdir stuff/bits |
| 98 | +``` |
| 99 | + |
| 100 | +No do an `ls` and you should see stuff retruned. `cd` into stuff and do another `ls`. Inside stuff, the directory bits was created. What if you wanted to create files? |
| 101 | + |
| 102 | +### `touch` or create files |
| 103 | + |
| 104 | +Inside bits, type: |
| 105 | + |
| 106 | +```bash |
| 107 | +$ touch bobs.txt |
| 108 | +``` |
| 109 | + |
| 110 | +Do an `ls` to check whether the file has been created. Inside bits, there should be a new file called bobs.txt. We used `touch` to create files. With touch you can create files with any extensions, just don't forget to specify what kind of file you are creating: index.html, script.js, style.css are all valid extensions. |
| 111 | + |
| 112 | +### Exercise 2: `cd` back into temp and create a couple of new folders with files in them |
| 113 | + |
| 114 | +### Bonus: type this into your terminal: |
| 115 | + |
| 116 | +```bash |
| 117 | +$ say hello |
| 118 | +``` |
| 119 | + |
0 commit comments