Marcy Lab School Docs
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • How-Tos
    • How To Code at Marcy: Code Style Guide
    • How to Do Short Response and Coding Assignments
    • How to Debug
    • How to PEDAC
    • How to Create A GitHub Organization and Scrumboard
    • How to Create Projects with Vite
    • How to Deploy on GitHub Pages
    • How to Deploy on Render
    • How to Test your API with Postman
  • Mod 0 - Command Line Interfaces, Git, and GitHub
    • Overview
    • 1. Command Line Interfaces
    • 2. Git & GitHub
    • 3. Git Pulling & Merging
    • 4. Git Branching & PRs
  • Mod 1 - JavaScript Fundamentals
    • Overview
    • 1. Intro to Programming
    • 2. Errors
    • 3. Node & Node Modules
    • 4. Variables, Functions & String Methods
    • 5. Control Flow, typeof, and Math
    • 6. Loops
    • 7. Arrays
    • 8. Objects
    • 9. Higher Order Functions: Callbacks
    • 10. Higher Order Functions: Array Methods
    • 11. Regex
  • Mod 2 - HTML, CSS & the DOM
    • Overview
    • 1. HTML
    • 2. CSS
    • 3. Accessibility (a11y)
    • 4. The Document Object Model (DOM) API
    • 5. Events
    • 6. Forms
    • 7. The Box Model and Positioning
    • 8. Flexbox
    • 9. Grid & Media Queries
    • 10. ESModules
    • 11. Vite
    • 12. LocalStorage
  • Mod 3 - Async & APIs
    • Overview
    • 1. Promises
    • 2. Fetch
    • 3. Building a Fetching App
    • 4. Async & Await
    • 5. A Generic Fetch Handler
  • Mod 4 - Project Week!
    • Important How Tos and Guides
      • How to Create a GitHub Organization and Scrum Board
      • How To Start a Project with Vite
      • How To Deploy a Project with GitHub Pages
    • Project Week Overview
    • Agile Methodologies
    • Deliverables & Milestones
    • Technical Requirements Checklist
    • Free API List
    • Collaborative GitHub
  • Mod 5 - Object-Oriented Programming
    • Overview
    • 1. Intro to OOP, Encapsulation, Factory Functions, and Closure
    • 2. Classes
    • 3. Private & Static
    • 4. UML Diagrams & Has Many/Belongs To Relationships
    • 5. Challenge: Implementing Has Many/Belongs To
    • 6. Inheritance
    • 7. Polymorphism
    • 8. Review and Practice
    • MDN: Object Prototypes
  • Mod 6 - Data Structures & Algorithms
    • Overview
    • Important How Tos and Guides
      • How to Debug
      • How to PEDAC
    • 1. Nodes & Linked Lists
    • 2. Singly & Doubly Linked Lists
    • 3. Stacks & Queues
    • 4. Recursion
    • 5. Trees
  • Mod 7 - React
    • Overview
    • Important How Tos and Guides
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
    • 1. Intro to React
    • 2. Events, State, and Forms
    • 3. Fetching with useEffect
    • 4. React Router
    • 5. Building a Flashcards App
    • 6. React Context
    • 7. Global Context Pattern
  • Mod 8 - Backend
    • Overview
    • Important How Tos and Guides
      • How to Deploy on Render
      • How to Test your API with Postman
      • Postgres Setup
    • 1. Intro to Express
    • 2. Building a Static Web Server with Middleware
    • 3. Securing API Keys and Environment Variables
    • 4. RESTful CRUD API
    • 5. Model-View-Controller Architecture
    • 6. SQL and Databases
    • 7. JOIN (Association) SQL Queries
    • 8. Knex
    • 9. Your First Fullstack App!
    • 10. Migrations & Seeds
    • 11. Schema Design & Normalization
    • 12. Hashing Passwords with Bcrypt
    • 13. React Express Auth Template Overview
  • Mod 9 - Civic Tech Hackathon
    • Overview
    • Rubric
  • Mod 10 - Capstone
    • Overview
Powered by GitBook
On this page
  • Slides
  • Overview
  • Terminal vs. Finder/Explorer
  • Using the Terminal in VS Code
  • Commands
  • Printing Directory Information with pwd and ls
  • Navigating Between Directories with cd
  • Be Careful when using the cd command!
  • Making Files and Directories with mkdir and touch
  • Executing JavaScript files with node
  • Terminating a Program with Control+C
  • Unfinished Double Quotes and echo
  • The cat Command and Combining Commands with &&
  • Removing, Renaming, and Copying
  • Challenges
  1. Mod 0 - Command Line Interfaces, Git, and GitHub

1. Command Line Interfaces

PreviousOverviewNext2. Git & GitHub

Last updated 7 months ago

Slides

Overview

At the end of the day, a program is just a text file on a computer. So, before we begin programming, we need to learn how programmers create, organize, and otherwise manage the files on their computers.

In this lesson, we'll learn about the Terminal, a program for interacting with a computer's files and executing programs through a command line interface (CLI).

Objectives

You will be able to…

  • Understand what a command line interface (CLI) is.

  • Compare and Contrast CLIs and Graphical User Interfaces (GUIs)

  • Navigate your local file tree

  • Create, copy, delete, and move files and directories

  • How to run a JavaScript program using your CLI

  • Explain what an "argument" is

Key Terms

  • Terminal — A program for interacting with a computer's files and executing programs through a command line interface.

  • Command Line Interface — a type of user interface (UI) that let's a users perform actions by entering text-based commands.

  • Graphical User Interface — a type of user interface that uses visual elements such as icons, buttons, windows, and dialog boxes, allowing users to perform actions such as clicking, drag-and-drop, and more.

  • Directory — Another term for a "folder" in your computer that contains references to files or possibly other directories.

  • Working Directory — The directory where your commands will be executed.

  • Command - A single action to be performed on your computer. Examples include creating a new file, listing the contents of the current directory, navigating to a different directory, or executing a program.

  • Argument — An additional piece of information provided to a command to change the command's behavior.

  • Node — A program for executing JavaScript code directly on your computer (as opposed to in a browser).

Important CLI commands

Note: In the commands below, argument placeholders will be written like this: <argument>. When using these commands, replace the <argument> with your desired inputs, making sure to leave out the <> as well.

  • node <file_name.js> — execute a given .js file using Node.

  • Control + C — Terminate the currently running program

  • pwd — Print the working directory, a.k.a where your terminal navigation currently is located.

  • ls — Prints ("lists") the contents of the working directory

  • cd <directory> — Change directories to the given directory

  • cd ../ — Change directories to the parent of the working directory

  • mkdir <dir_name> — Make a new directory with the given name.

  • touch <file_name> — Make a new file with the given name

  • cp <file> <dest> — Move a file to the given directory

Terminal vs. Finder/Explorer

As a programmer, you must become a master of the files on your computer. If you've ever dug into your computer's file system, you likely have done so using a program like Finder or Windows Explorer.

These applications allow you to manage files through a graphical user interface (GUI) — a user interface with buttons and icons that let you do things like click and drag-and-drop.

While these features are very beginner-friendly, they are often too slow for "power users" who want to perform more complex tasks or rapidly perform many repeated tasks.

Instead, programmers typically use a tool called the Terminal. The Terminal is a program for interacting with a computer's files and executing programs through a command line interface (CLI).

In the screenshot above, you can see this user entering commands:

pwd
ls
mkdir unit-5
ls
ls unit-0
cd unit-0
touch bye.txt hey.txt
ls
echo "hello world"
echo "hello world" >> output.txt
ls && cat output.txt
rm hey.txt && ls
mv bye.txt goodbye.txt && ls

Note that some commands like pwd can be entered on their own. Other commands like mkdir may use inputs called arguments. Some commands like ls can be used on their own or with arguments.

Q: Why use The Terminal?? It would be wayyy faster to do this in the Finder

Use the Tab key to autocomplete commands and filenames! Just start typing and hit Tab to autocomplete.

For this particular task it might be faster to use a GUI file manager like Finder, however there are many tasks where a CLI like the Terminal can outpace a GUI like Finder. In addition, there are some things that Finder simply can't do, like execute files with code.

Using the Terminal in VS Code

While you can use the Terminal application that comes with your laptop, it is often just as convenient to use the one that comes built into your VS Code code editor.

To open up the Terminal panel, go to File > Terminal and it should show up at the bottom:

Commands

Let's go through some of the most important and commonly used commands.

Printing Directory Information with pwd and ls

A directory is another term for a "folder" in your computer's file system that contains references to files or possibly other directories.

The working directory is the current location of your terminal's navigation through that file system and where all commands are executed. Think of it as the "you are here" icon in a map.

The pwd command prints the full file path to the working directory while the ls command prints the contents of the working directory:

Note: In computing, most actions fall into one of the four categories called CRUD: creating, reading, updating, or deleting data. Which of these actions do you think pwd and ls are?

Navigating Between Directories with cd

The cd <directory> command allows you to move to another directory in the file system. However, unlike the previous commands, it requires an argument.

An argument is an additional piece of information that changes that behavior of a given command. For the cd command, we have to also provide a destination.

For example, suppose we were located in the /Users directory inside the following file system:

I could navigate to "down" to the /Users/smith directory with the command:

cd smith

You can also extend the directory provided with a / to quickly navigate to directories within directories. For example, if I were located in the /Users directory, I could navigate to the /Users/smith/Documents directory in one command:

cd smith/Documents

To navigate back "up" to the "parent" directory, you can use the special directory name .. which always refers to the parent directory of the working directory.

For example, if I were in the directory /Users/smith/Documents and wanted to go back up to the /Users/smith directory, I could enter the command:

cd ..

If I had wanted to go up two levels from /Users/smith/Documents to /Users, I can again use / to extend the provided directory name:

cd ../..
Suppose I were located in the /Users/smith/Documents directory, how could I navigate to the /Users/jones/Desktop directory in one cd command?
cd ../../jones/Desktop

Be Careful when using the cd command!

Using the cd command on its own will send you to the root of your entire file system (~/). This is the equivalent of using the command:

cd ~

Making Files and Directories with mkdir and touch

mkdir <dir_name> creates a new directory in the working directory

touch <file_name> creates a new file in the working directory. Make sure to include the file extension!

You can also create multiple files/directories at once by listing multiple file/directory names:

touch file1.txt file2.txt
mkdir dir1 dir2 dir3

Executing JavaScript files with node

A JavaScript program is any file with a .js extension, like hello.js

The code can be as simple as console.log("Hello World")

To run the program, use the command node hello.js

Terminating a Program with Control+C

Many programs will end ("terminate") on their own when each statement has been executed.

Other programs can run forever, requiring us to stop them ourselves. For example, when we use the node command on its own, it will start the Node REPL (Read, Evaluate, Print Loop) program which just waits for Javascript input, executes it, and then prints the result:

To terminate the program, use the keyboard shortcut Control+C (you may need to cancel twice).

Unfinished Double Quotes and echo

Another common occurrence is an unfinished string. You can test this with the echo command which will print a given string straight to the terminal.

You can also use the output from an echo command and send the output into another file using the >> append operator.

For example, this command will take the text "hello world" and append it to the file output.txt (it will create output.txt if it doesn't exist)

echo "hello world" >> output.txt

The cat Command and Combining Commands with &&

The cat command is used to read the contents of a given file.

You can combine any two commands with the && operator.

ls && cat output.txt

For example, the command above lists the contents of the current working directory and print the contents of output.txt

Removing, Renaming, and Copying

rm <file_name> removes a file from the working directory

rm -r <directory_name> removes a directory and all of its contents from the working directory. -r stands for "recursive".

mv <file_name> <new_file_name> renames a file in the working directory or allows you to move the file to another directory.

cp <file_name> <dir_name> copies a file into a directory.

Challenges

We're using Linux commands so whenever you are researching a topic, add "Linux" to the end of your search so that you get appropriate results! For example, for the first challenge you may search "how to unzip .tar.gz file linux".

Challenges 1-19 are the basics of what you'll want to know for this course.

Challenges 20 you should skip. It involves executing a file that we can't see the contents of.

Challenges 21-50 are more advanced.

Check out these for additional practice. A lot of these challenges go way beyond what we've learned in this lesson and will push you to research and learn on your own!

awesome challenges
Slides
Overview
Terminal vs. Finder/Explorer
Using the Terminal in VS Code
Commands
Printing Directory Information with pwd and ls
Navigating Between Directories with cd
Be Careful when using the cd command!
Making Files and Directories with mkdir and touch
Executing JavaScript files with node
Terminating a Program with Control+C
Unfinished Double Quotes and echo
The cat Command and Combining Commands with &&
Removing, Renaming, and Copying
Challenges
The finder program
The Terminal program
Use the keyboard shortcut Control+` to open/close the Terminal
The Node REPL is useful for testing out expressions.
An unfinished double quote will be produce dquote> in the Terminal, waiting for you to finish the string.