Navigating the Terminal
#
Lesson ObjectivesAfter this lesson, students will be able to:
- Describe basic aspects of how classes will work
- Download and install software necessary for the class
- Open Terminal
- Describe what a command line interface is
- Describe the state of the current directory
- Change Directories
- List some keyboard shortcuts
- Create files and folders
- Navigate using relative pathing on the command line
- Navigate using absolute pathing on the command line
- Run some code
#
Describe basic aspects of how classes will work- Lesson headers
- Headers will be posted for all lessons and labs with links to markdowns and the Zoom channel.
- Markdown
- Markdown contains reference material related to the lesson
- Slides from the markdown will be posted into Slack during the lesson
- Sharing screen
- The instructor will screenshare for demonstration purposes. Double-click out of fullscreen.
- Screen real-estate: markdown, zoom, text editor, terminal, slack
- Command-tab to cycle applications
- Muting
- Be muted unless speaking
- Asking questions
- Ask away! Either in Slack or out loud
- Visibility of instructor code
- Speak up if the instructor's code is not visible (eg. too small).
- Thumbsups
- We use the thumbsup ๐ emoji to gauge completeness of exercises. Click on the thumb when you are done with an exercise.
#
Download and install software necessary for the class- Visual Studio Code installed? Or your favorite text editor.
- Node installed?
- Current version, not LTS
- Click on "Macintosh Installer"
- Install the package
#
Open Terminalโ (Command) + Space
, or open Spotlight- type
"Terminal"
Enter
- Keep it locked in your dock. Right click on the icon, highlight options, check "Keep in Dock".
#
Terminal PreferencesYou can change the color of your Terminal and the font size. It is recommended to make your Terminal output easier to read, rather than the tiny default output.
Terminal > Preferences > Pro
#
Describe what a command line interface isTerminal 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 directoryThe 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 apath
. Thispath
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 DirectoriesWe 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.
- navigates into a child directory called
cd ..
- navigates into the parent of the current directory
..
is shorthand for parent
cd
will take you back home
#
List some keyboard shortcutsIn the long term, reduce your reliance on the mouse. More Bash keyboard shortcuts:
โ K
Clear the Terminal windowoption arrow
Move cursor by word- letter[TAB]
- autocompletes (case-sensitive)
- up โฌ๏ธ / down ๐ฝ arrow
- cycle command history
#
Create files and foldersmkdir
- 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 LabyrinthUsing 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, andmkdir
to make directories. files and directories are two different things.Navigate to Desktop
mkdir Labyrinth
,cd Labyrinth
Make a directory structure like this:
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.
#
Navigate using relative pathing on the command lineChain 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 thestairway
- From the
stairway
, navigate to theparlor
- From the
parlor
, navigate to thedining room
- From the
dining room
, navigate to theescher room
- From the
escher room
, navigate to the Labyrinth root
#
Actual Activity (10 min)#
Navigate the Labyrinth (Actual)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 thedining_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 theparlor
- From the
parlor
, navigate to theescher_room
- From the
escher room
, navigate to thethrone_room
- From the
throne_room
, navigate to theball_room
#
Navigate using absolute pathing on the command lineMove 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
andmv
.
#
Activity (3 min)#
Navigate the Labyrinth IIUsing absolute pathing:
- Navigate to the throne_room
- Navigate to the ballroom
- Navigate to the parlor
#
Run some codeWe 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 Documents.
- Make a directory
w1d1_student_examples
- Go inside the directory
- Make a file
first_code.js
- Open up VSCode through Spotlight or the Dock.
- Go to
VSCode -> Command + Shift + P -> type Shell -> Shell Command : Install code in PATH
- this allows you to open VSCode using a terminal command
- Close VSCode
- From inside the
w1d1_student_examples
directory, open VSCode from the command line withcode .
(code space dot)- NOTE This might not work on some systems. If not we will get it sorted out in time. For now, Just open the directory from
File -> Open
, and open thew1d1_student_examples
directory.
- NOTE This might not work on some systems. If not we will get it sorted out in time. For now, Just open the directory from
- When VSCode opens, you should see the
w1d1_student_examples
directory and all the files inside the directory (in this case, just thefirst_code.js
file.
#
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 SEI!
We can send whatever we want to the console.
console.log('The rain in Spain falls mainly on the plain');
#
More Terminal Keyboard shortcutsIn the long term, reduce your reliance on the mouse. More Bash keyboard shortcuts:
โ K
Clear the Terminal window
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
Ctrl U
Kill line to beginning
Ctrl Y
Paste whatever was killed using Ctrl+K or Ctrl+U
cd -
toggle previous directory
There are also video tutorials here