Terminal/Command Line for Beginners

The command line is a powerful text-only interface for computers. If you have ever seen a show where hackers are furiously typing into a black window, that's the command line. It is an alternative to the mouse or finger-based graphical user interface familiar to most computer users. A typical user will never need to use the command line but software developers do because certain tasks can only be done with it: running programs, installing software, using Git for version control, or connecting to servers in the cloud. With a little practice most developers find that the command line is actually a faster and more powerful way to navigate and control a computer.

Given its minimal user interface--just a blank screen and a blinking cursor--the command line is intimidating to newcomers. There is often no feedback after a command has run and it is possible to delete an entire computer with a single command: no warning will pop up. As a result, the command line must be used with caution. Make sure not to blindly copy and paste commands you find online; only rely on trusted resources for any command you don't fully understand.

The Command Line

In practice, multiple terms are used to refer to the command line: Command Line Interface (CLI), console, terminal, shell, or prompt. Technically speaking, the terminal is the program that opens up a new window to access the command line, a console is a text-based application, the shell is the program that runs commands on the underlying operating system, and the prompt is where commands are typed and run. It is easy to be confused by these terms initially but they all essentially mean the same thing: the command line is where we run and execute text-only commands on our computer.

On Windows, the built-in terminal and shell are both called PowerShell. To access it, locate the taskbar on the bottom of the screen next to the Windows button and type in "powershell" to launch the app. It will open a new window with a dark blue background and a blinking cursor after the > prompt. Here is how it looks on my computer.

PS C:\Users\wsv>

Before the prompt is PS which refers to PowerShell, the initial C directory of the Windows operating system, followed by the Users directory and the current user which, on my personal computers, is wsv. Your username will obviously be different. At this point, don't worry about what comes to the left of the > prompt: it varies depending on each computer and can be customized at a later date. The shorter prompt of > will be used going forward for Windows.

On macOS, the built-in terminal is called appropriately enough Terminal. It can be opened via Spotlight: press the Command and space bar keys at the same time and then type in "terminal." Alternatively, open a new Finder window, navigate to the Applications directory, scroll down to open the Utilities directory, and double-click the application called Terminal. This opens a new screen with a white background by default and a blinking cursor after the % prompt. Don't worry about what comes to the left of the % prompt. It varies by computer and can be customized later on.

Wills-Macbook-Pro:~ wsv%

If your macOS prompt is $ instead of % that means you are using Bash as the shell. Starting in 2019, macOS switched from Bash to zsh as the default shell. While most of the commands in this book will work interchangeably, it is recommended to look up online how to change to zsh via System Preferences if your computer still uses Bash.

Shell Commands

There are many available shell commands but most developers rely on the same handful over and over again and look up more complicated ones as needed.

In many cases, the commands for Windows (PowerShell) and macOS are similar. For example, the command whoami returns the computer name/username on Windows and just the username on macOS. As with all shell commands, type the command itself followed by the return key. Note that the # symbol represents a comment and will not be executed on the command line.

# Windows
> whoami
wsv2021/wsv

# macOS
% whoami
wsv

Sometimes, however, the shell commands on Windows and macOS are completely different. A good example is the command for outputting a basic "Hello, World!" message to the console. On Windows the command is Write-Host while on macOS the command is echo.

# Windows
> Write-Host "Hello, World!"
Hello, World!

# macOS
% echo "Hello, World!"
Hello, World!

A frequent task on the command line is navigating within the computer filesystem. On Windows, the default shell should show the current location but it can also be outputted with Get-Location. On macOS use pwd (print working directory).

# Windows
> Get-Location

Path
----
C:\Users\wsv

# macOS
% pwd
/Users/wsv

Navigation

Navigating the filesystem of your computer is one of the most important command line tasks. On Windows, the default shell should show the current location but it can also be outputted with Get-Location. On macOS use pwd (print working directory).

# Windows
> Get-Location

Path
----
C:\Users\wsv

# macOS
% pwd
/Users/wsv

This directory is known as the home directory or root directory. When you open a new command line it will take you here.

It would be nice to know what's in our current location so to list files and directories we can use ls.

# Windows 
> ls
Directory of C:\Users\wsv
04/15/2021 09:00 AM <DIR>   Applications
...

# macOS
% ls
Desktop
Downloads
...

To change the current directory we type cd followed by the directory name. Then check if it changed using cd on Windows and pwd on macOS/Linux.

# Windows 
> cd Desktop
> Get-Location

Path
----
C:\Users\wsv\onedrive\desktop

# macOS
% cd Desktop
% pwd
/Users/wsv/Desktop

Tip: The tab key will autocomplete a command so if you type cd d and then hit tab the command line will automatically fill in the rest of the name. If there are more than two directories that start with d, hit the tab key again to cycle through them.

These three navigational commands--cd or pwd, ls, and cd--are by far the most commonly used shell commands. Knowing where you are on the filesystem and navigating to the correct location is half the battle!

Create Directories

Let's create a new directory on our Desktop with mkdir, change into it, and confirm our location.

# Windows
> mkdir testdir
> cd testdir
> Get-Location
C:\Users\wsv\Desktop\testdir

# macOS
% mkdir testdir
% cd testdir
% pwd
/Users/wsv/Desktop/testdir

Easy enough, right? So far we have only nagivated into child directories but what if we wanted to return to the parent Desktop directory? We can use the command cd ...

# Windows
> cd ..
> Get-Location
C:\Users\wsv\Desktop

# macOS
% cd ..
% pwd
/Users/wsv/Desktop

If we wanted to return to the home directory we could run cd .. again or we could also provide the full filepath of our destination.

# Windows
> cd C:\Users\wsv
> Get-Location
C:\Users\wsv

# macOS
% cd /Users/wsv
% pwd
/Users/wsv

There are thus 3 ways to navigate using cd:

Move and Copy Directories

We can also move and copy directories from one location to another. Let's first create a directory called practice within the new testdir directory.

# Windows
> cd C:\Users\wsv\Desktop\testdir
> Get-Location
C:\Users\wsv\Desktop\testdir
> mkdir practice
> dir
...

# macOS
% cd /Users/wsv/Desktop/testdir
% pwd
/Users/wsv/Desktop/testdir
% mkdir practice
% ls
practice

Now move the practice directory to the Desktop with the mv command. The format is mv source destination where source is the current location and destination is where we want it to go.

# Windows 
> mv testdir C:\Users\wsv\Desktop
> dir
...
> cd ..
> Get-Location
C:\Users\wsv\Desktop
> dir
testdir

# macOS
% mv testdir /Users/wsv/Desktop
% ls
% cd ..
% pwd
/Users/wsv/Desktop
% ls

A variation of move is to copy a directory where the original version remains but a copy is placed in a new location. The command for this is cp followed by source and destination. Let's copy the practice directory on the Desktop into the testdir directory which is currently empty.

# Windows
> cp practice C:\Users\wsv\Desktop\testdir
> dir
testdir
practice
> cd testdir
> dir
practice

# macOS
% cp practice /Users/wsv/Desktop/testdir
% ls
testdir
practice
% cd testidr
% ls
practice

Simple enough, right? And especially if you are moving or copying something multiple directories away it can be much faster than clicking with a mouse if you know your computer's filesystem well enough.

Delete Directories

At this point we have added three new directories to our computer: testdir and practice directories on our Desktop, and within testdir a subdirectory called practice. To clean up after ourselves, let's delete the testdir directory. The deletion command for a file is rm but to delete a directory and its contents the flag -r (for recursive) must be added.

# Windows
> Get-Location
C:\Users\wsv\Desktop\testdir
> cd ..
> Get-Location
C:\Users\wsv\Desktop\
> dir
testdir
practice
> rm -r testdir
> dir
practice

# macOS
% cd Desktop
% pwd
/Users/wsv/Desktop
% ls
testdir
practice
% rm -r testdir
% ls
practice

Caution: Using rm is permanent: the directory or file will be gone forever. So use it very, very carefully. Be especially careful about any flags that you apply after it if you do not understand exactly what the command will do.

Helper Commands

As we conclude, here are a few helper commands that are good to know. The clear command will clear the terminal of past commands and outputs so you have a clean slate. The tab command autocompletes the line as we've discussed. And the and keys cycle through previous commands to save yourself from typing the same thing over and over again.

There is a built-in manual for every command that is easily accessible. For example, to look up the whoami command, the format is whoami/? on Windows and man whoami on macOS.

# Windows 
> whoami/?

# macOS
% man whoami

To exit you could close the command line shell with your mouse but the hacker way is to use use the shell command exit instead. This works by default on Windows but on macOS the Terminal preferences need to be changed. At the top of the screen click on Terminal, then Preferences from the drop down menu. Click on Profiles in the top menu and then Shell from the list below. There is a radio button for "When the shell exits:". Select "Close the window."

# Windows
> exit 

# macOS/Linux 
% exit

Kinda cool, right? With practice, the command line is a far more efficient way to navigate and operate your computer than using a mouse. Most software developers are not command line experts. They know the basic commands listed above and how to look up anything more complex.

Next Steps

We've only just scratched the surface of what can be accomplished on the command line. It's possible to write automated scripts, connect to servers, and a host of other activities. Some developers become quite expert at the command line in their careers while others find that, for the most part, the above commands cover 99% of their needs. A complete list of commands on every operating system can be found at ss64.com.

If you wish to learn more, two excellent books are PowerShell for SysAdmins if you are on Windows and The Linux Command Line if you are on macOS or using Linux servers.

Join My Newsletter

Subscribe to get the latest tutorials/writings by email.

    No spam. Unsubscribe at any time.