πŸ’» Week 02 - Lab Roadmap (90 min)

DS105 - Data for Data Science

Author
Published

01 September 2022

This week, we will learn to access and navigate your computer in a shell called bash, the most common way to access computers in the cloud (the topic of next week’s lecture and lab).

By default, bash is available on Unix-based operating systems (Linux and macOS) but not on Windows. Those using Windows will have to install some additional software first.

Step 1: Setting up (5-20 min)

Step 1: Setting up

Windows logo Windows users

Sadly, Windows command line interfaces differ from those used in Unix-based operating systems (Linux and macOS). Therefore, the commands we will learn today do not apply to Windows.

But fear not! Recent versions of Windows have a feature called Windows Subsystem for Linux, or WSL 1, which you can use to install Linux distributions. You just have to enable it first.

🎯 ACTION POINTS Follow Steps 1-4 of the tutorial β€œInstall Ubuntu on Wsl2 on Windows 11 with GUI Support.”

Apple logo macOS users

🎯 ACTION POINTS

  1. Open the terminal

  2. Now, to ensure you are using the bash shell, type the following and hit ENTER:

    echo $SHELL

    If you see the following message printed on the terminal screen, you are all good to move to the next step.

    /bin/bash
  3. If instead, you saw the message below:

    /bin/zsh

    This means your computer came pre-packed with a different terminal called Z Shell. Read and follow the instructions in this tutorial to change your shell to bash 2.

Linux logo Linux users

You probably already know how to get to the terminal β€” typically the shortcut Ctrl+T will get you there.

Just ensure you are running the bash shell. If not, follow the same instructions given to macOS users above.
Step 2: Locate yourself (20 min)

Step 2: Locating yourself

We will follow the instructions below step by step together while answering whatever questions you might encounter along the way.

  1. Open the bash shell using the instructions from Step 1 above

  2. Type pwd and hit ENTER:

     pwd

    You should get a string of text indicating the full path of where you are inside the terminal.

  3. Are you currently in your $HOME directory? To find out where your home directory is, use the following command:

     echo $HOME
  4. Are there any files/sub-directories inside the directory you are currently in? Use ls to investigate:

     ls

    or

     ls .
  5. Are there any hidden files?

     ls -a
  6. Now, let’s explore what is above your current directory:

     ls ..
  7. Do you get what the different dots . and .. mean 3?

  8. Now, more than just look at the parent directory, let’s move there:

     cd ..

    then use the same ls commands you explored above. Did you notice anything different now?

  9. Let’s force ls to print the same information in a different format. Try the following command:

     ls . -lth

    What does the -l, -t and -h arguments mean? You can find out by looking at the ls manual:

     man ls

    Use the arrow keys in your keyboard to scroll up and down the manual. Type the character q when you are done browsing.

  10. Now, try to go back to the directory you were at the start of this tutorial. Hint: if you get stuck, try pwd again and compare the output you get now to the one you got the first time you used this command.

For your future research, if you see β€œ$” sign at the beginning of the code snippets, it means you are in a system shell and should type your commands in the shell.

 $ pwd
Step 3: Wandering away from $HOME (20 min)

Step 3: Wandering away from $HOME

Now, given what we practiced, try to follow the steps below (help from colleagues is fine) and take note of your answers to the questions.

We will go over the solutions once everyone has finished this step.

🎯 ACTION POINTS

  1. Go to the root of the filesystem:

     cd /
  2. What do you see with ls? What does it mean?

  3. Walk around freely up and down the directories and sub-directories you encounter along the way. Check the links on πŸ”– Week 02 Appendix - Linux and the Terminal page to understand where you are navigating.

  4. Which directory(ies) under / were you not allowed to enter? Why?

  5. What is the difference between the /root directory and the root filesystem /?

  6. Go back $HOME

Step 4: Installing software (10 min)

Step 4: Installing software

πŸ’­ How do you install apps in your computer or smart phone?

It is very likely that you just head to a software store (Play Store, Apple Store or Microsoft Store) and download apps from there. Linux has something similar, you can access and download software using the apt command. Let’s use it to download the text editor vim.

🎯 ACTION POINTS

  1. Whenever you want to download software with apt, you have to ensure you have the most up-to-date information from the central β€œstore”. You can do this via the following command:

     sudo apt update

    This command requires you to have admin rights (as root), which is why we run it with sudo 4. You might be asked to type your password.

  2. Install vim:

     sudo apt install vim
  3. Now you can open vim:

     vim

    To quit, press Esc, then type :q and hit ENTER. Still stuck and cannot quit vim? You are not alone.

Apple logo MacOS users

🎯 ACTION POINTS

You can use β€˜Homebrew’ to install software in a very similar way to apt command in a Linux system.

  1. First, you need to be sure if homebrew is installed on your system. You can check it with the terminal.

    brew
  • If you receive an error:

    zsh: command not found: brew

    You should install Homebrew in your system. You can do this with the following command:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    if you have not installed XCode before, the installation script will ask you to install it as well, please accept to continue.

  1. You can install vim now:

    brew install vim
  2. You can open vim:

    vim
  • You can update brew with the command:

    brew update
Step 5: Creating and editing files and directories (20 min)

Step 5: Creating and editing files and directories

🎯 ACTION POINTS

  1. Go to your $HOME directory or a folder of your choice. Then, create and enter a directory, let’s call it DS105:

     cd $HOME
     mkdir DS105
     cd DS105
  2. Create a text file and call it README.txt:

     touch README.txt
  3. Open this file with vim:

     vim README.txt
  4. Inside vim, write something to the file (your name, a verse, a sentence, whatever comes to mind).

To insert text characters, you need to switch to the Insert Mode by pressing i. In the left bottom, you can see –INSERT– and start to write. You can shift back to β€˜Normal Mode’ by pressing ESC.

Mainly, vim has three modes: Normal, Insert and Visual.

  • Vim initialises with Normal Mode by default in which you enter the commands but you cannot insert any character directly. In normal mode;

    You can navigate your cursor with β€œh, j, k, l”
    delete any line with β€œdd”
    delete a character with β€œx”
    insert a character after the cursor with β€˜a’
    insert the previously deleted text after the cursor with β€˜p’

    There are many β€œVIM Commands” waiting to be discovered by you.

  • In Insert Mode you basically can enter your text/characters.

  • You can also switch to Visual Mode by pressing β€˜v’ while in normal mode, and you can manipulate text groups by cutting, copying, and deleting.

  1. Now save your changes to the file. To save a file when inside vim, press Esc then type :w and hit ENTER.

  2. Quit vim: press Esc, then type :q and hit ENTER 5.

  3. Print the content of your file with the cat command:

     cat README.txt
Formative assignment

Formative assignment

This problem set is not graded but you will get written feedback if you submit.

Follow the steps below and, when asked, copy your responses to the Moodle page of this assignment.

P1: Identify yourself

  1. On the terminal, type the two commands below:

     whoami
     uname -a
  2. Take a screenshot (how?) of your terminal and submit to Moodle, under the field identified as P1.

P2: Edit a file

  1. Go to the DS105 folder you created earlier:

     cd ~/DS105
  2. Rename the README.txt to README.md (a markdown file):

     mv README.txt README.md
  3. Edit the README.md file following the template below, replacing all occurrences of <> with your responses:

    # DS105
    
    Name: <>
    Date: <>
    Candidate Number: <>
    Degree Program: <>
    
    ## Notes
    
    - I want to learn data science because <>
  4. Save and quit vim.

  5. Print some metadata about your file to prove you were the one editing it:

     stat README.md
  6. Print the contents of your file to the terminal:

     cat README.md
  7. Take a screenshot (how?) of your terminal and submit to Moodle, under the field identified as P2. Make sure that both the output of stat and cat commands are visible.

(Bonus) 🏠 Practice at home: vimtutor

Practice at home: vimtutor

🎯 ACTION POINTS

  1. Open the terminal and type:

     vimtutor
  2. Follow the instructions on the screen and complete the tutorial (a total of 7 short lessons).

Check out the πŸ”– Week 02 Appendix - πŸ“ƒ Text editors section for more info on vim.

Footnotes

  1. Microsoft Tutorial: β€œWhat Is Windows Subsystem for Linuxβ€β†©οΈŽ

  2. Curious about the different shells on Mac πŸ€“ ? Checkout the full article on β€œLearn Enough Tutorials: Using Z Shell on Macsβ†©οΈŽ

  3. See the answer to this question on Stackoverflow: β€œWhat is double dot(..) and single dot(.) in Linux?β€β†©οΈŽ

  4. The sudo Wikipedia entry has quite a good summaryβ†©οΈŽ

  5. Still stuck and cannot quit vim? You are not alone.β†©οΈŽ