|
| 1 | +# Basic use of the command line |
| 2 | + |
| 3 | +Learning to use the command line is essential for advanced programming, but it can also |
| 4 | +improve your day to day tasks. |
| 5 | +You can access your entire computer via the command line and do everything you can normally do |
| 6 | +using the Graphical User Interface... and more. |
| 7 | + |
| 8 | +This (mini)tutorial will guide you through the very basics of using |
| 9 | +the command line. |
| 10 | + |
| 11 | +When you launch a terminal, the default path/location is your home directory, however |
| 12 | +if at any point you are unsure which folder you are "in" (which is your working |
| 13 | +directory) you only need to type the command `pwd`. |
| 14 | + |
| 15 | +## Understanding the syntax |
| 16 | +When you open the terminal you might see a *prompt* similar to: |
| 17 | + |
| 18 | +``` |
| 19 | +username@computer ~ $ |
| 20 | +``` |
| 21 | + |
| 22 | +- The `~` character is typically shorthand for your *home directory*. Here you being told that your *present working directory* is your home directory. |
| 23 | +- The `$` (Ready) indicates that the prompt is ready to accept your command; the `$` is just there, you do not need to type it. |
| 24 | + |
| 25 | +## Clear |
| 26 | +At some point you might end up with your terminal full of lines. By typing the command `clear` you can clear the terminal screen. |
| 27 | + |
| 28 | +## Listing the Directory contents |
| 29 | +If within a directory you want to see its content you can use the `ls` command. |
| 30 | + |
| 31 | +If you need more detailed information on the contents (e.g. access permissions, |
| 32 | +date the file was last modified, etc.) you can use the command `ls -l`. |
| 33 | + |
| 34 | +## Moving between directories |
| 35 | +To change your working directory you need to use the `cd` command (change directory), followed by the *pathname* of the directory you want to move into e.g: |
| 36 | + |
| 37 | +```bash |
| 38 | +cd /HelloWorld/src/ |
| 39 | +``` |
| 40 | + |
| 41 | +If you do not specify a pathname and just type `cd`, which will take you back to your home directory. |
| 42 | + |
| 43 | +If you want to go to the previous directory (or a directory closer to the root) you can type `cd ..`. This will take you back one directory at a time. |
| 44 | + |
| 45 | +If you are trying to move to a directory that has spaces in the path you need to use "" to preserve the spaces: `cd "Mini tutorial"` |
| 46 | + |
| 47 | +## Creating directories |
| 48 | +So far we have covered how to move between existing directories, but we can just as well create directories using the command line: |
| 49 | +`mkdir HelloWorld` |
| 50 | +If you want to create a folder within the HelloWorld directory you just created you can do it by typing `mkdir HelloWorld/data` without the need to change directories first. |
| 51 | + |
| 52 | +If you need to create a multiple embedded directories, instead of creating one by one you can use the `mkdir` option `-p` which will create the parent directories i.e. |
| 53 | + |
| 54 | +```bash |
| 55 | +mkdir -p Helloworld/data/myproject/test1/ |
| 56 | +``` |
| 57 | + |
| 58 | +## Creating files |
| 59 | +You can create empty files using the `touch` command, for example `touch project.scala`. For this you need to be into the directory you want the file to be created, otherwise you need to specify the full path of the file: |
| 60 | + |
| 61 | +`touch HelloWorld/data/one.txt` |
| 62 | + |
| 63 | +You can even create multiple files at a time `touch one.txt two.txt` |
| 64 | + |
| 65 | + |
| 66 | +## Deleting files |
| 67 | +To permanently delete files you can use the `rm` command e.g. |
| 68 | + |
| 69 | +```bash |
| 70 | +rm one.txt |
| 71 | +``` |
| 72 | + |
| 73 | +Or even delete various files at a time |
| 74 | + |
| 75 | +```bash |
| 76 | +rm one.txt two.txt |
| 77 | +``` |
| 78 | + |
| 79 | +To delete *empty* directories: |
| 80 | + |
| 81 | +```bash |
| 82 | +rmdir dir1/an-empty-dir |
| 83 | +``` |
| 84 | + |
| 85 | +To recursively delete directories (delete non-empty directories and their contents): |
| 86 | + |
| 87 | +```bash |
| 88 | +rm -r dir1/dir_with_stuff_in |
| 89 | +``` |
| 90 | + |
| 91 | +## Copying files |
| 92 | +You can copy files between directories by using `cp source destination` indicating the path of the file and then the path where you want the copy to be created. |
| 93 | + |
| 94 | +## But it is too much typing... |
| 95 | +We've got you covered! Let's say you want to go to your data directory. You can start use the autocomplete function you can start typing `cd HelloWorld/da` and then press the **Tab** key on your keyboard and the prompt will... well autocomplete the path for you. |
0 commit comments