How to use the Terminal
Command-Line Interface
The Command-line interface (CLI) is an interface for entering text-based commands.
Most personal computer users will never use the CLI. Instead every application they use has a Graphical User Interface (GUI) with menus, buttons, and shortcut key combinations to execute commands.
Computer programmers, however, use the command line constantly. Creating a GUI application for every program adds a lot of unnecessary overhead and complexity. So, many programs are executed from the command line.
You can also open the default CLI application as a separate panel within VS Code. Open VS Code > click the Terminal menu > Select New Terminal. A Terminal panel will open at the bottom of the Editor window.
Most personal computer users will never use the CLI. Instead every application they use has a Graphical User Interface (GUI) with menus, buttons, and shortcut key combinations to execute commands.
Computer programmers, however, use the command line constantly. Creating a GUI application for every program adds a lot of unnecessary overhead and complexity. So, many programs are executed from the command line.
Open the CLI
The default CLI application:- On Mac is called Terminal. Open it with Launchpad (it is under Others).
- Opening Terminal automatically opens the Unix Shell program (called Z Shell).
- On Windows is called PowerShell. In All Apps it is listed as Terminal.
- PowerShell interfaces with File Explorer.
You can also open the default CLI application as a separate panel within VS Code. Open VS Code > click the Terminal menu > Select New Terminal. A Terminal panel will open at the bottom of the Editor window.
Enter your first command: pwd
This will return the present working directory. That is the path to the directory you are in.
Terms
- Unix: is a family of operating systems first published in 1971. It is used by MacOS, iOS, Android, and the majority of servers.
- Unix-like operating systems: Unofficial umbrella term for operating systems that behave like a Unix system, whether or not they are certified as following the Single UNIX Specification. Includes Linux.
- CLI/Terminal/Console: The terms command-line interface (CLI), terminal, and console are often used interchangeably. They refer to the software application that provides an interface for entering text commands.
- The Terminal application is the name of Mac's default terminal application. It emulates a computer terminal providing a command-line interface as well as some Graphical User Interface (GUI) features like tabs, copy/paste, etc. It automatically opens a Unix Shell application which interfaces with the computer's operating system.
- Shell program: A Shell program is a command-line interpreter that executes commands on the operating system's services.
- PowerShell: is the default command-line shell for Windows. It interfaces with File Explorer.
- Unix shell program: Command-line interpreter that interfaces with the Unix operating system. There are several Unix shell programs. The two most commonly used are:
- Z Shell (zsh): This is the default Unix Shell program used in MacOS.
- Bash: is the default command shell for most Linux distributions
So on a Mac, the Terminal application opens the Z Shell Unix Shell program, providing a command-line interface for interacting with the Unix operating system.
On Windows the default Terminal application is called PowerShell. It provides a command-line interface for interacting with the Windows operating system.
Command Syntax
PowerShell command syntax is different, but the below Unix commands are accepted as aliases.
The command format is the command name followed by the optional parameters: options and arguments.
- command [-options] [arguments]
- The square brackets [] are just a notation to let you know they are optional. Don't put the square brackets in the actual command.
- Command: The name of the command comes first.
- Options (aka flags): Options modify how the command does its job.
- -<character> Unix options use single dash and one character.
- -<char1><char2> or -<char1> -<char2> Applies multiple options to the command.
- --option-keyword Some Unix variants allow long options. Use two dashes followed by a keyword.
- Arguments: Arguments pass information to the program you want to run. There can be zero, one, or multiple arguments. Typically they specify the data upon which the program will perform an action.
Example: ls command
We will use the list command as an example.- ls The list command name is ls. It lists the files and folders in the current directory, excluding hidden system files.
Add options:
- ls -p The -p option distinguishes directory names from file names by appending "/" after a directory name.
- ls -A The -A option shows all files, including hidden system files. In Unix, systems files start with a dot and are hidden.
- ls -p -A or ls -pA Applies both the -p and -A options to the ls command.
Add an argument:
- ls ~/Documents The ls command takes the directory to get the list from as the argument. The current directory is the default. The ~/Documents argument gets the list from the user's Documents director. Unix knows it is an argument and not an option because it does not start with a dash.
Add options and an argument:
- ls -pA ~/Documents You can use options AND arguments in the same command.
When typing a command:
- enter/return Press enter/return to execute the command. The cursor does not have to be at the end of the line.
- left | right arrow Use the left arrow to move the cursor one character to the left. Right arrow to move one character to the right.
- Control+A Moves the cursor to the beginning of the line.
- Control+E Moves the cursor to the end of the line.
- up | down arrow Press the upArrow to to get the previously entered command. Press it multiple times to get earlier entered commands. DownArrow goes back down one command.
This can save you a lot of typing. You can up arrow to a previous command, then press enter to execute it again.
- partial command + tab Unix tries to guess the rest of the command or filename.
Other notes on Unix syntax:
Case: Unix is case sensitive. As a general rule of thumb, use lower case where possible.
Quotes: You can use either single or double quotes around text. The echo command prints the argument: echo 'Hello'; echo "Hello"Basic Commands
Now let's go over some basic terminal and Unix commands.Mac Terminal | Win Powershell keyboard shortcuts:
We'll start with some basic keyboard shortcuts for the Mac Terminal application and the corresponding command for the PowerShell application.- Cmd+N | Ctrl+Shift+N Open new terminal window.
- Cmd+T | Ctrl+Shift+T Open new terminal tab.
- Cmd+F | Ctrl+Shift+F Find a search term on your Terminal screen if it exists.
- Cmd+C | Ctrl+C Copy selected text.
- Cmd+V | Ctrl+V Paste.
- clear | Ctrl+L Clear the screen. Scrolls to last line without removing previous lines.
- Cmd+K | clear Clear the screen. Removes all previous lines.
Run Other Shell Programs in the Terminal:
- program_name Starts the program.
- Example: node Launches the Node.js shell program if it's installed.
- Example: python Launches the Python shell program if it's installed.
- Control+C Closes an operation in the program (without exiting the program).
- Control+D Exits the program.
- program_name --version Returns the version of the program if it is installed.
- Example: node --version Returns the version of Node.js if it is installed.
- which program_name Returns the executable file path which is called by the command.
- Example: which node Returns the location of the node.js executable file if it is installed.
File System
You can use the command line to interact with your computer's file system.
Directories
We'll start with directories.
Paths
- The Unix file system is like an upside down tree with the root at the top and branches going down.
/
Root directory.
- Paths can be absolute or relative:
- Absolute path: starts from the root directory
/
. - Relative path: starts from your present working directory.
- In Unix, you must use the forward slash to indicate directories
/
. - In Windows you can use either the forward slash
/
or the back slash\
to indicate directories. - If a folder has a space in it you must put it in quotes:
/"program files"/directoryname
- For Windows, including
c:
when referencing the root directory is optional. - Case sensitivity: Unix is case sensitive, but case sensitivity for directory and file names depends on the operating system's file system. MacOS's file system, for instance, is not case sensitive.
Change Directories
When you open your Terminal application, by default it will open to your home directory which is at:
/Users/username
pwd pwd is a utility command that returns the absolute pathname of the present working directory (i.e., the directory you are in).
cd path Change directory to the absolute or relative path argument you provide.
Relative paths:
- ./ is the current directory.
- cd .. Two dots mean go up one directory.
- cd ../.. Go up two directories.
- cd directory_name Go down one directory.
- cd path/to/directory_name Go down three directories.
- cd ../../path/to/directory_name Go up two directories then down three.
Absolute paths:
- cd / Takes you to the root directory.
- cd /User/username/Documents Takes you to the Documents directory.
Home directory:
Unix has a home directory for the current user's files and directories.
The home directory on both Windows and Mac is at
/Users/username
- cd /Users/username Takes you to your home directory.
- cd ~ The tilde is a shortcut for your home directory.
You can use your home directory in an absolute path:
- cd ~/Documents will take you to your Documents directory.
List Files and Directories
Now let's talk about listing files and directories with the ls command.
- ls Lists all files and directories in the current directory.
- To list the content of a different directory you can either cd into it, or add a path argument:
ls path List all files & directories in the given path.
ls / Lists the files & directories in the root directory.
ls ~ Lists the files & directories in your home directory.
ls ~/Documents Lists the files & directories in your Documents directory.
ls options: There are a number of options available to the list command.
- ls -pA is a useful option combination. It distinguishes directories from files and shows hidden files.
- ls -p The -p option distinguishes directories from files by appending a
/
after directory names. - ls -A Shows all files & directories including hidden files. Unix system files have a dot in front of them, and are hidden by default.
Directory path argument:
- ls directoryPath List the files and directories in the given path.
- ls / See all the files and directories in the root directory.
- ls ~ See all the files and directories in the home directory.
- ls ~/Documents See all the files and directories in the Documents directory.
- ls -pA ~/Documents See the list with p and A options on the ~/Documents directory.
Make a Directory:
To create a new directory, use the mkdir command.
mkdir directory_name Creates a directory mkdir temp Creates a directory named temp.