|
| 1 | +--- |
| 2 | +layout: ../../../layouts/TutorialLayout.astro |
| 3 | +title: Introduction to the command-line interface |
| 4 | +currentOS: linux |
| 5 | +order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +# Introduction to the command-line interface |
| 9 | + |
| 10 | +> For readers at home: this chapter is covered in the [Your new friend: Command Line](https://www.youtube.com/watch?v=jvZLWhkzX-8) video. |
| 11 | +
|
| 12 | +It's exciting, right?! You'll write your first line of code in just a few minutes! :) |
| 13 | + |
| 14 | +__Let us introduce you to your first new friend: the command line!__ |
| 15 | + |
| 16 | +The following steps will show you how to use the black window all hackers use. It might look a bit scary at |
| 17 | +first but really it's just a prompt waiting for commands from you. |
| 18 | + |
| 19 | +> **Note** Please note that throughout this book we use the terms 'directory' and 'folder' interchangeably |
| 20 | +but they are one and the same thing. |
| 21 | + |
| 22 | +## What is the command line? |
| 23 | + |
| 24 | +The window, which is usually called the __command line__ or __command-line interface__, is a text-based |
| 25 | +application for viewing, handling, and manipulating files on your computer. It's much like Windows Explorer |
| 26 | +or Finder on the Mac, but without the graphical interface. Other names for the command line are: *cmd*, |
| 27 | +*CLI*, *prompt*, *console* or *terminal*. |
| 28 | + |
| 29 | +## Open the command-line interface |
| 30 | + |
| 31 | +To start some experiments we need to open our command-line interface first. |
| 32 | + |
| 33 | +It's probably under Applications → Accessories → Terminal, or Applications → System → Terminal, but that may |
| 34 | +depend on your system. If it's not there, you can try to Google it. :) |
| 35 | + |
| 36 | +You now should see a white or black window that is waiting for your commands. |
| 37 | + |
| 38 | +### The command-line Prompt |
| 39 | + |
| 40 | +If you're on Linux, you probably see a `$`, like this: |
| 41 | + |
| 42 | +`command-line` |
| 43 | +``` |
| 44 | +$ |
| 45 | +``` |
| 46 | + |
| 47 | +Each command will be prepended by a `$` and one space, but you should not type it. Your computer will do it |
| 48 | +for you. :) |
| 49 | + |
| 50 | +> Just a small note: in your case there may be something like `ola@Olas-PC:~ $` before the prompt |
| 51 | +sign, and this is 100% OK. |
| 52 | + |
| 53 | +The part up to and including the `$` or the `>` is called the *command line prompt*, or *prompt* for short. |
| 54 | +It prompts you to input something there. |
| 55 | + |
| 56 | +In the tutorial, when we want you to type in a command, we will include the `$` or `>`, and occasionally |
| 57 | +more to the left. Ignore the left part and only type in the command, which starts after the prompt. |
| 58 | + |
| 59 | +## Your first command (YAY!) |
| 60 | + |
| 61 | +Let's start by typing this command: |
| 62 | + |
| 63 | +`command-line` |
| 64 | +``` |
| 65 | +$ whoami |
| 66 | +``` |
| 67 | + |
| 68 | +And then hit `enter`. This is our result: |
| 69 | + |
| 70 | +`command-line` |
| 71 | +``` |
| 72 | +$ whoami |
| 73 | +olasitarska |
| 74 | +``` |
| 75 | + |
| 76 | +As you can see, the computer has just printed your username. Neat, huh? :) |
| 77 | + |
| 78 | +> Try to type each command; do not copy-paste. You'll remember more this way! |
| 79 | +
|
| 80 | +## Basics |
| 81 | + |
| 82 | +Each operating system has a slightly different set of commands for the command line, so make sure to follow instructions for your operating system. |
| 83 | + |
| 84 | +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. |
| 85 | + |
| 86 | +Let's try this, shall we? |
| 87 | + |
| 88 | +### Current directory |
| 89 | + |
| 90 | +It'd be nice to know where are we now, right? Let's see. Type this command and hit `enter`: |
| 91 | + |
| 92 | +`command-line` |
| 93 | +``` |
| 94 | +$ pwd |
| 95 | +/Users/olasitarska |
| 96 | +``` |
| 97 | + |
| 98 | +> **Note:** 'pwd' stands for 'print working directory'. |
| 99 | +
|
| 100 | +You'll probably see something similar on your machine. Once you open the command line you usually start at |
| 101 | +your user's home directory. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +### Learn more about a command |
| 106 | + |
| 107 | +Many commands you can type at the command prompt have built-in help that you can display and read! For |
| 108 | +example, to learn more about the current directory command: |
| 109 | + |
| 110 | +macOS and Linux have a `man` command, which gives you help on commands. Try `man pwd` and see what it says, |
| 111 | +or put `man` before other commands to see their help. The output of `man` is normally paged. Use the space |
| 112 | +bar to move to the next page, and `q` to quit looking at the help. |
| 113 | + |
| 114 | +### List files and directories |
| 115 | + |
| 116 | +So what's in it? It'd be cool to find out. Let's see: |
| 117 | + |
| 118 | +`command-line` |
| 119 | +``` |
| 120 | +$ ls |
| 121 | +Applications |
| 122 | +Desktop |
| 123 | +Downloads |
| 124 | +Music |
| 125 | +... |
| 126 | +``` |
| 127 | + |
| 128 | +### Change current directory |
| 129 | + |
| 130 | +Now, let's go to our Desktop directory: |
| 131 | + |
| 132 | +`command-line` |
| 133 | +``` |
| 134 | +$ cd Desktop |
| 135 | +``` |
| 136 | + |
| 137 | +Note that the directory name "Desktop" might be translated to the language of your Linux account. If that's |
| 138 | +the case, you'll need to replace `Desktop` with the translated name; for example, `Schreibtisch` for German. |
| 139 | + |
| 140 | +Check if it's really changed: |
| 141 | + |
| 142 | +`command-line` |
| 143 | +``` |
| 144 | +$ pwd |
| 145 | +/Users/olasitarska/Desktop |
| 146 | +``` |
| 147 | + |
| 148 | +Here it is! |
| 149 | + |
| 150 | +> PRO tip: if you type `cd D` and then hit `tab` on your keyboard, the command line will automatically fill |
| 151 | +in the rest of the name so you can navigate faster. If there is more than one folder starting with "D", hit |
| 152 | + the `tab` key twice to get a list of options. |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +### Create directory |
| 157 | + |
| 158 | +How about creating a practice directory on your desktop? You can do it this way: |
| 159 | + |
| 160 | +`command-line` |
| 161 | +``` |
| 162 | +$ mkdir practice |
| 163 | +``` |
| 164 | + |
| 165 | +`command-line` |
| 166 | +``` |
| 167 | +$ cd practice |
| 168 | +$ mkdir test |
| 169 | +$ ls |
| 170 | +test |
| 171 | +``` |
| 172 | + |
| 173 | +Congrats! :) |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +### Clean up |
| 178 | + |
| 179 | +We don't want to leave a mess, so let's remove everything we did until that point. |
| 180 | + |
| 181 | +First, we need to get back to Desktop: |
| 182 | + |
| 183 | +`command-line` |
| 184 | +``` |
| 185 | +$ cd .. |
| 186 | +``` |
| 187 | + |
| 188 | +Using `..` with the `cd` command will change your current directory to the parent directory (that is, the |
| 189 | +directory that contains your current directory). |
| 190 | + |
| 191 | +Check where you are: |
| 192 | + |
| 193 | +`command-line` |
| 194 | +``` |
| 195 | +$ pwd |
| 196 | +/Users/olasitarska/Desktop |
| 197 | +``` |
| 198 | + |
| 199 | +Now time to delete the `practice` directory: |
| 200 | + |
| 201 | +> __Attention__: Deleting files using `del`, `rmdir` or `rm` is irrecoverable, meaning _the deleted files |
| 202 | +will be gone forever_! So be very careful with this command. |
| 203 | + |
| 204 | +`command-line` |
| 205 | +``` |
| 206 | +$ rm -r practice |
| 207 | +``` |
| 208 | + |
| 209 | +Done! To be sure it's actually deleted, let's check it: |
| 210 | + |
| 211 | +`command-line` |
| 212 | +``` |
| 213 | +$ ls |
| 214 | +``` |
| 215 | + |
| 216 | +### Exit |
| 217 | + |
| 218 | +That's it for now! You can safely close the command line now. Let's do it the hacker way, alright? :) |
| 219 | + |
| 220 | +`command-line` |
| 221 | +``` |
| 222 | +$ exit |
| 223 | +``` |
| 224 | + |
| 225 | +Cool, huh? :) |
| 226 | + |
| 227 | +## Summary |
| 228 | + |
| 229 | + Here is a summary of some useful commands: |
| 230 | + |
| 231 | +Command (Windows) | Command (Mac OS / Linux) | Description | Example |
| 232 | +----------------- | ------------------------ | -------------------------- | --------------------------------------------- |
| 233 | +exit | exit | close the window | **exit** |
| 234 | +cd | cd | change directory | **cd test** |
| 235 | +cd | pwd | show the current directory | **cd** (Windows) or **pwd** (Mac OS / Linux) |
| 236 | +dir | ls | list directories/files | **dir** |
| 237 | +copy | cp | copy file | **copy c:\test\test.txt c:\windows\test.txt** |
| 238 | +move | mv | move file | **move c:\test\test.txt c:\windows\test.txt** |
| 239 | +mkdir | mkdir | create a new directory | **mkdir testdirectory** |
| 240 | +rmdir (or del) | rm | delete a file | **del c:\test\test.txt** |
| 241 | +rmdir /S | rm -r | delete a directory | **rm -r testdirectory** |
| 242 | +[CMD] /? | man [CMD] | get help for a command | **cd /?** (Windows) or **man cd** (Mac OS / Linux) |
| 243 | + |
| 244 | +These are just a very few of the commands you can run in your command line, but you're not going to use |
| 245 | +anything more than that today. |
| 246 | + |
| 247 | +If you're curious, [ss64.com](https://ss64.com) contains a complete reference of commands for all operating systems. |
| 248 | + |
| 249 | +## Ready? |
| 250 | + |
| 251 | +Let's dive into Python! |
0 commit comments