1.3: Command Line
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.
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.
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.
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.
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
tabkey 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.
Your command line window already has a "location". See the "absolute" path of that location with
See the files in your current directory.
lsaccepts a Unix path argument (denoted below as
<path>). If we leave it blank it means
., or the current Unix directory.
Move your current directory. Running
cdwithout an argument is equivalent to
cd ~, or "
cdto the home directory".
Create a plain text file. A plain text file is one that only contains characters. You can't
touchan 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.
Create a directory
Copy a file
cp sails.txt new-sails.txt
Copy a folder
cp -R boat-folder yatch-folder
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
mvthat are currently open in VSCode. If this happens, please try to move or rename your files and/or folders using other means.
Your removed files DO NOT go to a trash bin. Removing is irreversible.
Never, ever, ever try to run a command that removes the
A rule of thumb is to be careful and double check what you're doing when removing folders.
Remove a file
Remove a folder
rm -r my-boat-folder
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
An absolute path always begins with the root directory.
The home folder of your login user
Go to your current login user's home folder.
The directory you're currently in
Copy something into the directory you're currently in.
cp ~/my-report.txt .
The directory one level above your current directory
Move from the current location to the current location's parent directory
Almost every command has options you can add, like
cp. If you want to see all the options available, try
--helpat the end of a command. For
cpit would be
man cp. Press
qto 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" .
The command line has many different sub functionalities. If you get lost here are a few tips.
There are certain interfaces that bring us into a page navigator, like the command for help with commands,
mantells us how to use Unix commands. You use it by typing something like
man ls- tell me about the
lscommand. 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
qto quit. Some Git commands also bring you into this interface, such as
logwhich we'll see in the next section.
Contents of current directory
Make an empty text file
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
Remove a folder
rm -r my-boats
tabkey on your keyboard to auto-complete partially-typed file or folder names. This can save us a lot of typing!
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.
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
lsare the commands that will tell you the current state of the command line at any time.