Terminal Navigation Morning Exercise

Describe what a command line interface is

Terminal provides a Command Line Interface (CLI) to the operating system. With it you can give your computer direct, text-based instructions. It is the most powerful piece of software on your computer! Think of it as the central hub of your development process. For now, we will use it to navigate the files and folders in our computer.

When terminal launches, it will start in your computer's home directory (whatever you named your computer). Your home directory is denoted by the tilde ~.

In Terminal-land, Directories are the same as Folders (we just call them Directories).

In Finder, we can also navigate our computer's folders and files: folders contain files and more folders:

Describe the state of the current directory

The Command Line understands commands written in the bash scripting language. The commands are abbreviations of English words, or acronyms.

  • pwd - will print the current working directory. It shows you a path. This path shows you where you are currently located in the filesystem. It's like sending up a flare or homing beacon, where you can see how far you have 'traveled' from the root directory.

  • ls - Lists the contents of the current directory. You can see

    • the immediate child directories (the directories inside this directory)
    • the files in this directory

  • Bash commands can take flags denoted by a dash -

    • ls -a - list content including hidden files and directories. Hidden files and directories begin with a period, for example, .git.
    • ls -l - list content and give meta information about each item

Directories (folders) can have directories within them, and there can be directories inside those directories, ad infinitum. This creates a tree structure where directories can have many children with many different branches.

Change Directories

We can navigate to other directories relative to the current working directory.

  • cd some_directory

    • navigates into a child directory called some_directory. some_directory is a child of the current directory.
  • cd ..

    • navigates into the parent of the current directory
    • .. is shorthand for parent
  • cd will take you back home

List some keyboard shortcuts

In the long term, reduce your reliance on the mouse. More Bash keyboard shortcuts:

  • ⌘ K Clear the Terminal window on a Mac & type clear the press enter on Linux or PC
  • option arrow Move cursor by word on a Mac & ctrl arrow on a Linux or PC
  • letter[TAB]

    • autocompletes (case-sensitive)
  • up / down arrow

    • cycle command history

Create files and folders

  • mkdir

    • makes a directory
    • Example:
    • mkdir directory_name

      • makes a directory called 'directory_name'`
  • touch

    • creates an empty file
    • A file typically will have a file extension like .txt whereas a directory will not.
    • Example:
    • touch file.txt

      • makes a .txt file

Activity (10 min)

Construct a Labyrinth

Using what you know about navigating directories and creating files and folders, construct a 'labyrinth' on your desktop.

Precision is important. There are a few layers to this exercise. Be patient.

  • Make sure you are in the correct directory when you go to create another directory or file.
  • Make sure you use touch to make files, and mkdir to make directories. files and directories are two different things.
  • Navigate to Desktop
  • mkdir Labyrinth, cd Labyrinth
  • Make a directory structure like this:

labyrinth

parlor and stairway are child directories of the Labyrinth directory.

sarah_williams.txt is a file inside the the ballroom directory.

If you make a mistake, don't worry, just keep adding the right stuff to the right place.

Chain more directories to the current path with the / separator

  • Go down the chain into child directories

    • cd parent_directory
    • cd parent_directory/child_directory
    • cd parent_directory/child_directory/grandchild_directory
  • Go up the chain into parent directories

    • cd ..
    • cd ../..
    • cd ../../..
  • Go sideways into a sibling directory by first going up, then down

    • cd ../sibling_directory
  • Go into an aunt or uncle directory by first going up to the parent, then the grandparent, then down again on another branch:

    • cd ../../auntie_directory

Activity

Navigate the Labyrinth

  • Navigate to the Labyrinth root directory
  • From the Labyrinth root directory, navigate to the stairway
  • From the stairway, navigate to the parlor
  • From the parlor, navigate to the dining room
  • From the dining room, navigate to the escher room
  • From the escher room, navigate to the Labyrinth root

Activity (10 min)

Navigate the Labyrinth

For each of these, write your command on one line, using full paths:

  • Navigate to the Labyrinth root directory
  • From the Labyrinth root directory, navigate to the dining_room
  • From the dining room, navigate back up the root directory
  • From the Labyrinth root directory, navigate to the stairway
  • From the stairway, navigate to the parlor
  • From the parlor, navigate to the escher_room
  • From the escher room, navigate to the throne_room
  • From the throne_room, navigate to the ball_room

Move anywhere relative to the home directory:

cd ~/ - the path starts in home directory

Example:

  • cd ~/Desktop/Labyrinth/stairway/escher_room

Navigates to the escher room no matter where you are currently located in your filesystem

NOTE: You can combine absolute and relative pathing when copying or moving files from one location to another with cp and mv.

Activity (3 min)

Navigate the Labyrinth

Using absolute pathing:

  • Navigate to the throne_room
  • Navigate to the ballroom
  • Navigate to the parlor

Run some code

We are going to:

  • make a file
  • open it in our text editor
  • write some code
  • run the code in Terminal

Code Along (5 min)

Create Files/Directories

  • In Terminal, navigate to your home directory with ~.
  • make a directory called software_classwork
  • Go inside the directory
  • make a directory called unit_1 in ~/software_classwork
  • Go inside the directory
  • Make a directory w1d1_student_examples in ~/software_classwork/unit_1
  • Go inside the directory
  • Make a file first_code.js in ~/software_classwork/unit_1/w1d1_student_examples
  • Open folder in VSCode code .

Write/Run Code

  • Check version of Node node -v. You should have a version greater than 8.
  • Let's send a message to the console. It is somewhat of a tradition to write a 'Hello World' message as the first thing you do in programming.
console.log('Hello World!');

Run the code in Terminal

node filename.js => Hello World!

Congrats! You've written your first "Hello World" of wdi-remote!

We can send whatever we want to the console.

console.log('The rain in Spain falls mainly on the plain');

More Terminal Keyboard shortcuts

In the long term, reduce your reliance on the mouse. More Bash keyboard shortcuts:

option arrow Move cursor by word

Ctrl A Go to beginning of line

Ctrl E Go to end of line

Ctrl K Kill line to end / Cut line to End

Ctrl U Kill line to beginning / Cut Line to Beginning

Ctrl Y Paste whatever was killed using Ctrl+K or Ctrl+U