1.3: Command Line
Last updated
Last updated
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 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.
Your command line window already has a "location". See the "absolute" path of that location with pwd
.
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.
Move your current directory. Running cd
without an argument is equivalent to cd ~
, or "cd
to the home directory".
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.
Create a directory
Copy a file
Copy a folder
Move a file or folder
Rename a file or folder
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.
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
Remove a 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 C:\
drive.
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.
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 -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" .
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, 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.
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
.
Current directory
Contents of current directory
Change directory
Make an empty text file
Copy a file
Copy a folder
Move or rename a file or folder
Remove a file
Remove a folder
Use the tab
key 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 pwd
and ls
are the commands that will tell you the current state of the command line at any time.