Overview
The command line lets you communicate with your computer without using your mouse. It's a text-based interface where you communicate using text alone. The commands differ between Unix-based systems (e.g., Mac / Linux) and Windows computers. On Apple and Windows computers the command line tool is called the Terminal and Command Prompt, respectively.
With this building block, we will help you navigate the command line (Terminal and Command Prompt) and describe the various commands that are used to do so.
But before we start, it is important to be familiar with two important terms: Directories and file paths.
Directories
In programming, all folders are called directories and a directory within another directory is called a subdirectory
. A working directory
is the directory in which the user is working currently. The default working directory when the user logs in is known as the home directory
. So when you open up a new Terminal window, you are automatically situated in your home directory.
File Paths
Your computer understands two kinds of paths, absolute and relative ones. An absolute path starts from the root (or home directory). A relative path, on the other hand, starts from the current directory (for instance, your desktop).
absolute_path = "/Users/Pieter/Desktop/images/photo1.jpg"
relative_path = "images/photo1.jpg" # provided that your working directory is "Desktop"
How to open the Command Line?
Mac
Open the Terminal by going into your Applications folder, and then into your Utilities folder, and then double-clicking on the Terminal icon. Alternatively, you can use Spotlight to search for the program (e.g. Cmd + Space).
Windows
Type cmd
into the search bar and hit enter, it should launch the Windows Command Prompt.
Commands
Print Working Directory
Mac
To see the full path for your working directory, you can use the pwd
command, which stands for print working directory.
*Windows* If you type `pwd` in the Windows Command prompt, it doesn't recognize the command. Instead, type `cd` (or `chdir`) to view the current working directory. ### List Files *Mac* Unlike the graphical user interface, you cannot see what other directories or files are present just by being in a particular directory. You have to explicitly tell your computer to list them for you using the `ls` command.
If you’d like to explore another directory, you need to use ls with a path. For example, `ls ~/Documents` will show you what's inside Documents. *Windows* The `dir` command lists all files and folders in the current directory. In addition, it returns the file size, file type, and the date and time the file was last updated.
Press the up and down arrows on your keyboard to cycle through previously used commands in the terminal. Also, you can press TAB
for auto-complete suggestions.
A handy shortcut to start in a specific directory immediately is to look up the folder in Finder, right-click on it, and choose "New Terminal at Folder".
Generally speaking, it's better to avoid special characters in directory names, you can use quotes to create a folder name that includes spaces (e.g., mkdir "to do lists" Documents
).
You can't undo the rm
and del
commands, so be careful when you delete files. Removing files in command line is not the same as moving a file to the trash - it's permanent!
# move directories or files to a new location
mv [FILE_NAME] [LOCATION]
# rename files
mv [FILE_NAME] [NEW_FILE_NAME]