Terminal
Reading Time: 8.3 minutes
Before we dive in into the world of programming, let's get familiar first with our terminal. The terminal or command prompt (for Windows users) are essential tool and I can say as a must have in programming. Most of the time, we will be interacting with it in one form or another.
What is a Terminal?
Command Prompt from Microsoft Windows 2000
A terminal or command prompt refers to the program that allows us to interact with the computer system or operating system via a text-based commands.
In the early days of computing, terminals were physical devices consisting of a keyboard and a screen (or printer) that were connected to a central mainframe or minicomputer. Users interacted with the mainframe by typing commands on the terminal's keyboard, and the mainframe would respond by displaying text-based output on the terminal's screen or printing it on paper.
So now in short, we will be using our terminal to fire up commands that are not available on GUI programs that we already have (the programs we normally use with windows and buttons).
Scope
In this lesson, we will be dealing with basic commands that will help us get in our way to the journey of programming. We will not learn every command that exists, just the basics.
Before we start, just a few reminders:
- Commands are not the same for Windows and Unix-Like Operating Systems (OS) such as Linux and Mac
- There exists custom terminal emulators that are not pre-built with our OS.
Opening the Terminal
The first lesson we will learn is how can we open our terminal.
So you should know that there are different ways in opening a terminal and in this lesson, we will be focusing on Windows and MacOS.
MacOS
To open the terminal, the easiest way is to open it like any other applications in our computer, by using the spotlight.
So to do so, press command
+ space bar
. This will open an input text where we can type in and seach all the programs available to us.
Now type in terminal
.
Above is the preview of spotlight in computer and as you can see, by typing in the word 'terminal', it gives a suggestion below, and by pressing return
on my keyboard, my terminal will now open.
Windows
For Windows users, you can press your start button (the button in the keyboard that has the Windows logo in it) and start typing in cmd
or command prompt
. This will also give you suggestions for available applications that matches your search.
To demonstrate, I have an online emulator for Microsoft Windows 2000 and this should feel familiar for Windows users.
By clicking in on Start
button the select Run
from the menu, I will be able to open the Run
dialog. This allows us to enter a program name to open. Let us type in cmd
.
Click on Ok
button and we will be able to open the command prompt.
Listing of Files and Directories
One of the most important commands in the terminal is this, the listing of all files and folders in a specific directory.
This is our way of visualizing the contents of a specified directory without using Finder (for MacOS) or Explorer (for Windows).
Tips
Note: Most of the time, we will be dealing with listing of contents in the current directory where we are in the file system.
MacOS
For MacOS or Linux, the command for listing all the files and folders in a current directory is ls
. So just type ls
and press return
key in the keyboard from your terminal and it will list all files and directories in the current directory where you are in.
My warp terminal emulator showing the directory where I am in and the command I fired and the result below the command
In the image above, the directory I am currently in is ~/Dev/tutorial/codingpanda
. It's actually the directory of the source code of this tutorial.
The command I gave is ls
which is represented in bold from above and then below that are the files I have in the directory including sub-directories:
- docs
- node_modules
- package-lock.json
- package.json
Note
Now take note that these are the visible files, all files that starts in .
are hidden by default.
Take note also that I am not using the default terminal on my Mac here that's why it looks a little different. I am using Warp Terminal
Windows
In Windows, the corresponding command would be dir
short for directory. When you fire this command, it will list all files and directories in the specified directory. If none were given, it will display the files and directories in the current directory.
Now if we try it using CMD:
The commdand prompt (cmd) of Windows 2000 opened at C:\>
showing all the files and directories inside.
Now we can try opening the WINNT
directory using the command
C:\>dir WINNT
This will list all the files and sub-directories inside the WINNT
.
Tips
Note the in Windows, commands are not case sensitive so you can also type both dir
and DIR
.
Navigating The File System
In navigating within the file system (between directories), both Windows and MacOS has the same command, cd
or change directory.
For example, I have below my terminal (warp) at the location from the previous topic (Listing Files and Directories):
Note
Note that here, I added an option -l
to the ls
command so that the files and directories are properly listed vertically (For MacOS and Linux).
From the list above, the items
- docs, and
- node_modules
are directories, meaning we can go into those directories.
Navigate to a Sub-Directory
Here we will navigate to the docs
directory using the command
cd docs
As you can see, we have navigated using the above command to the docs
directory shown by the command on the first box. The on the second box, you will notice that we are now in a different directory ~/Dev/tutorial/codingpanda/docs
.
This was validated by the ls -l
command to show the files and directories in the current directory.
Navigate to the Parent Directory
Now to navigate back to the previous directory (codingpanda
), we will use:
cd ..
This will bring us back one step up the file system hierarchy.
Tips
To navigate twice back, meaning to the parent of the parent directory, we use
cd ../..
Opening Application Using Terminal
We can treat the previous commands as using programs by the terminal.
For example, the ls
command is a program run in the terminal. The cd
is a program the we gave an argument for example ..
to navigate to the parent directory.
Running a Program
Running a program by the terminal is straightforward. This is assuming that the programs are listed in what is called the Environment Variables. This is the collection of locations of our programs in an operating system so that they can be easilly accessed by the terminal. If a program is listed there, you can fire up the terminal anywhere in your file system and call the program.
Example
Opening Notepad in Windows
In this example, we will be using our terminal to open the notepad application in Windows. Notepad is natively included in the Windows operating system for a long time now. It is used for creating and editing text files.
We have two options:
- Open the notepad app (notepad app only).
- Open or create a file with notepad from the commandline.
First we will do the item 1 from above, just type in notepad
in the command prompt then press Enter
in the keyboard:
Now we can see that the Notepad application has opened.
Next we will include an argument in the notepad
command. Using this command, if we passed in a text, notepad assumes this as a path to a text file. If if exists in the current directory, it will open it, otherwise, it will ask us if we want to create the file.
Let's say we want to open or create a file called hello.txt
.
Now we have demonstrated how to use command prompt to open or run an application in Windows. Let's proceed for MacOS.
Opening TextEdit in MacOS
In MacOS, if we want to open a common app, we will use open -a <app_name>
.
For example we want to open the Microsoft Word app, we can enclose it within quotation marks since Microsoft Word are separated by a space and the commandline will treat them as separate entity. So if I have that installed we can say open -a "Microsoft Word"
and the app will open if installed.
That's it for the Microsoft Word. To open a pre-installed app with MacOS, we will try the TextEdit
app.
Now that window is a file dialog opened by TextEdit for the user to select a file.
Now that's it! Congratulations on completing the Basics of Using the Terminal!
Now that we have completed the most basic commands and concepts of the terminal, I think we are ready to start the journey in programming! In the next chapter, we are going to create a Checklist App.