The Terminal

At some point in your exploration of programming you will need to learn how to read/write data from elsewhere on the computer or the Internet.

When you use an app on your mobile phone or on your computer it will often save your files somewhere convenient so that you can find them again, but this isn’t magic: a programmer made a choice about how to do this for you, and now that you’re learning to program you have to make that choice.

At this point, for anyone who learned how to use a computer after about 1995, coding can quickly come to seem very unfriendly indeed! That’s because when you are programming you will need to give the computer a lot more information about how to find, read, and write data.

Paths

Have you ever thought about how and where files are stored on your computer? Probably not, unless you were very, very bored. Unfortunately, when you start programming you do need to learn a bit more about this – enough, at least, to tell Python where to find the file that you want it to read… though you can do much more than that!

A few starting principles:

  1. Directories (a.k.a. folders) and files all have a unique location somewhere on your hard drive.

  2. A directory (or folder) can contain directories and files. A simple file cannot contain a directory. Only special types of file such as a Zip archive can contain a folder.

  3. The directory that sits at the bottom of the hierarchy (i.e. the one directory that is not in another directory) is known as the root directory.

  4. The directory in which your settings and documents are saved (i.e. the stuff associated with your username) is known as the home directory.

  5. A file must be stored in a directory (there are no root files).

Paths in the Finder

We’ll get to a video in a second, but on a Mac you can ‘view’ the path in the Finder simply by clicking on any finder window and typing Alt + Command + P (or selecting View > Path from the menu bar). This will show the current path in a strip along the bottom of the finder window. Pay attention to the Path view in the movie below.

Paths in a Terminal

Now, sooner or later you are going to have to learn how to use the Terminal (a.k.a. Shell) because it will make certain tedious tasks go much, much faster. It is also by far the best way to install Python libraries or other ‘libraries’ that you need to develop your code. Learning to the use the Terminal is also going to help a lot in learning to fix subtle problems with your program (e.g. checking that you are reading the right data).

So here’s the same process of navigating from a directory called KCL Modules down to the 2017-18 Geocomputation Teaching directory as you just saw above, but using the Terminal:

You’ll notice that there were several seemingly cryptic commands – we’ll examine them in more detail below, but the important ones to note in this video are:

  • ls lists the contents of the current directory
  • cd changes directory

We always say that programmers are lazy and this is a good example of that: why write list when we can write ls or change directory when we can write cd? You obviously need to learn what those bits of laziness mean, but they can help a lot in speeding up your code!

A Challenge for you!

Now that you have a geneal idea of what is the terminal, what is a path, and how to navigate in your computer’s folders without clicking buttons, you can take a look at this Software Carpentry exercise focussed on the Terminal.

This will cover the following topics and guide you in understanding how to efficiently work with the terminal:

  1. What is the shell?
  2. Navigating the filesystem
  3. Working with files and directories
  4. Automating the tedious with loops
  5. Counting and mining with the shell
  6. Working with free text

For a one-page recap on everything you need to know to start working from the terminal, check out Ian Milligan & James Baker’s Introduction to the Bash Command Line. This very well structured overview covers the same topics of the previous exercise, and can serve a useful cheat sheet if in the future you might want to go back and revise these concepts (or only have a useful list of bash commands at hand).

Credits!

Contributors:

The following individuals have contributed to these teaching materials: - James Millington - Jon Reades - Michele Ferretti - Zahratu Shabrina

License

The content and structure of this teaching project itself is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license, and the contributing source code is licensed under The MIT License.

Acknowledgements:

Supported by the Royal Geographical Society (with the Institute of British Geographers) with a Ray Y Gildea Jr Award.

Potential Dependencies:

This lesson may depend on the following libraries: None