1.3: Command Line

Learning Objectives

By the end of this lesson, you should be able to:

  • Face the command line without fear

  • Describe what the command line is.

  • Open the command line on your computer.

  • Use the command line to navigate directories and display directory contents.

  • Use the command line to create a new directory and a new file.

  • Use the command line to rename or destroy a directory and a file.

  • Use the command line to open a file or folder in a program.

Introduction

What is the Command Line?

The command line is the interface to the computer that consists of only text characters. We will use the command line to navigate our filesystem and run the Git version control system on our code.

If you are a Windows user, please use the VSCode terminal with Bash installed by default. The Windows Command Prompt runs PowerShell by default which is not compatible with Unix-based commands that we need for SWE Fundamental and coding in general. See the Windows Command Line Setup for details.

What will we be using the command line for?

In SWE Fundamentals, we will primarily use the command line to work with our version control program, Git. In this course we will practice using the command line as an everyday tool to replace simple file actions like name, copying and deleting, before moving to exploring the fundamentals of Git and GitHub.

Don't worry if the command line seems hard to use and mysterious right now, it will become more familiar through practice. It is also a topic that is tangential to our core computational thinking material. Don't worry about mastering this section before moving on.

Terminology

The terms "command line" and "terminal" are synonymous. We will be using them interchangeably. The "terminal" used to refer to hardware when computers were the size of rooms, but has since lost it's original meaning.

We normally work with "folders" inside of our graphical windows. The Unix term is "directories". For our work in the command line, both are correct.

Common Commands

We will cover the commands that mimic the kinds of file and folder manipulation tasks you would accomplish through clicking and dragging in a graphical user interface.

At any time you can press the tab key and the command line will try to auto complete for you. This works for the names of commands and also longer file or folder names.

pwd (Present Working Directory)

Your command line window already has a "location". See the "absolute" path of that location with pwd.

pwd

ls (List)

See the files in your current directory. ls accepts a Unix path argument (denoted below as <path>). If we leave it blank it means ., or the current Unix directory.

ls

cd (Change Directory)

Move your current directory. Running cd without an argument is equivalent to cd ~, or "cd to the home directory".

cd boating/sails

touch

Create a plain text file. A plain text file is one that only contains characters. You can't touch an image file or a Word Document file.

Working with files is easier in the command line if their names don't have spaces in them. If you want two words in your file name, separate them with a dash.

touch my-filename.txt

mkdir (Make Directory)

Create a directory

mkdir skateboards

cp (Copy)

Copy a file

cp sails.txt new-sails.txt

Copy a folder

cp -R boat-folder yatch-folder

mv (Move)

Move a file or folder

mv old-folder/cats.txt new-folder

Rename a file or folder

mv cats.txt my-cats.txt

Windows users may encounter a "Permission denied" error when moving or renaming files or folders with mv that are currently open in VSCode. If this happens, please try to move or rename your files and/or folders using other means.

rm (Remove)

Your removed files DO NOT go to a trash bin. Removing is irreversible.

Never, ever, ever try to run a command that removes the root directory (/).

A rule of thumb is to be careful and double check what you're doing when removing folders.

Remove a file

rm my-friends.txt

Remove a folder

rm -r my-boat-folder

Special Paths

There are a few special characters that stand in for locations in "relative" paths.

The folder that contains all your other folders on your computer. On Windows, this is the C:\ drive.

/

An absolute path always begins with the root directory.

cd /Users/susan/Documents/boating

Home

The home folder of your login user

~

Go to your current login user's home folder.

cd ~

Current Directory

The directory you're currently in

.

Copy something into the directory you're currently in.

cp ~/my-report.txt .

Parent Directory

The directory one level above your current directory

..

Move from the current location to the current location's parent directory

cd ..

Command Line Options

Almost every command has options you can add, like -R with cp . If you want to see all the options available, try man or adding --help at the end of a command. For man and cp it would be man cp . Press q to get out.

If you want to accomplish something specific on the command line, googling for a specific combination of options usually works. Googling unix commands usually requires putting "unix command" in front, as in: "unix command ls" instead of just "ls" .

Troubleshooting the Command Line

The command line has many different sub functionalities. If you get lost here are a few tips.

q (Quit)

There are certain interfaces that bring us into a page navigator, like the command for help with commands, man. man tells us how to use Unix commands. You use it by typing something like man ls - tell me about the ls command. It will change the interface to a page interface that you can navigate with the up and down arrow keys ↑↓. To get out of this interface, press q to quit. Some Git commands also bring you into this interface, such as diff and log which we'll see in the next section.

Ctrl+C

Many command-line interfaces run programs that can be exited with Ctrl+C, which sends an interruption signal to the program in the foreground. On a Mac, this is still Ctrl and not Cmd.

Cheat Sheet

Current directory

pwd

Contents of current directory

ls

Change directory

cd

Make an empty text file

touch script.js

Copy a file

cp cakes.txt cupcakes.txt

Copy a folder

cp -R pastries new-pastries

Move or rename a file or folder

mv dogs.txt pets.txt

Remove a file

rm donut-list.txt

Remove a folder

rm -r my-boats

Pro Tip: Tab Complete

Use the tab key on your keyboard to auto-complete partially-typed file or folder names. This can save us a lot of typing!

Windows OneDrive

There have been some issues with Windows OneDrive making it harder to locate files and folders. If a folder or file cannot be found, it may be in OneDrive. Don't create files and folders in OneDrive. See the screenshot here.

The Command Line interface is a deep topic. We are only covering a tiny portion of the subject in Fundamentals to give a small look into the world of developer workflow, and so that we can start using the Git version control system. Don't worry if you don't understand everything! For now we're focused on specific actions within the total Command Line interface.

Exercises

Follow along with the video or attempt the commands above. See your results in a GUI window in MacOS's Finder or Windows Explorer.

The command line interface can do everything a GUI window can do, but don't necessarily reflect each other. Remember that pwd and ls are the commands that will tell you the current state of the command line at any time.

Last updated