Basics of the Terminal | ASW Hack Club


Introduction

The terminal is a tool that you use a lot if you are programming. MacOS is a Unix-like operating system, it is similar to Linux. Since ASW uses macbooks, everything in this guide will be about using the MacOS terminal, which is called zsh" or zshell.

The first thing we are going to is learn aobut how to move around your computer on the terminal. Unix-like systems all have the same commands here, so you should be fine (meaning that you are able to use the same commands across Linux. MacOS, and FreeBSD and others)

If for some reason you don't know how to open the terminal, it is iTerm on MacOS and Terminal. I prefer Terminal as it is lighter. You can also open a terminal in VSCode (if you do not have it installed, look at our guide here), in the top of your screen with the options like File and Edit, select Terminal > New Terminal. This will open your zsh terminal on MacOS.

I am not sure where to inlcude it, but it is a really useful command, the clear command clears the terminal. Very useful if you have a lot of text and it is overwelming, just returns the terminal into one line at the top of the screen.


Files.

All of the commands related to making, editting. moving, copying, reading, and deleting files in Linux or other Unix systems.

Command Function
ls
List the files in the current directory you are in.
cd *folder*
Change directory, with a directory to change to. Directory means a folder.
mkdir *folder name*
Make directory, with the name of the directory to make. Directory means a folder.
cat
A joke from the other commands being head and tail, catting out a file is showing it to you in the terminal.
head
Like mentioned in cat, it will give you the head (the front) of the file, just the first few lines.
tail
Also like mentioned in cat, it will give you the tail (the back) of the file, just the last few lines.
touch *filename*
Creates an empty file with the named supplied.
rm *filename*
Deletes a file. This will not work on folders. "Error: is a directory"
rm -rf *filename*
Deletes a file or folder recursively. This will work on folders and files. Be careful!
cp *origin* *destination*
Copy a file, accepts input file to copy and a place to copy the file into.
mv *origin* *destination*
Moves a file. Takes an input of a file and a place to move it into.
nano *filename*
Nano is the text editor a real man uses. More information later.

That's all of the most important file utlility commands that I think are super important. You will slowly be able to memorize these the more that you work with them.


Package Managers

All Unix systems usually control the software that they have installed from the terminal through a package manager. MacOS uses homebrew or brew to manage packages in the terminal. Other operating systems like Ubuntu/Debian use snap, apt and flatpak, with Arch Linux using pacman and more.

You can install Brew in your MacOS system with the following command, also available on their official site.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

You are able to use package managers easily. We will demo using each one with installing the gimp package, GNU Image Manipulation Program.

MacOS (brew):

brew install gimp

Arch Linux (pacman):

pacman -S gimp

Ubuntu/Debian (apt):

apt install gimp

Ubuntu/Debian (flatpak):

flatpak install flathub org.gimp.GIMP

Ubuntu/Debian (snap):

snap install gimp

You are able to replace gimp with any other package you want to install. This is just a brief overview of package managers.


Internet

You most likely are going to need to use some of these at some point.

Curl

Curl or cUrl is a tool in the terminal that throws things at you. Just kidding. It is a really useful tool for you to be able to get files off of the internet and acts just like the cat command. For example, Ollama, a popular tool to run large language models locally on your computer has an install script that you can run with cUrl. curl -fsSl https://ollama.com/install.sh | sh will get the file off of the ollama.com and pipe it into shell to make it run as a shell script. This is really useful for installers, that is why I chose that as the example. If you want to learn more about the | or the pipe, it is what we will talk about next after Internet

Wget

WGet is a useful tool to be able to download a file off of a server over the internet. For example, if I want to have a local copy of the silverflag.net main page, I am able to run wget https://silverflag.net to be able to download index.html which will be the default response of the server without anything added to the URL. This command has a large variety of uses.

Ping

Ping ... Pong! Ping is used a lot in technology to make sure that a network or form of communication is operating correctly. For exmaple, if I want to make sure that I have internet or that silverflag.net is operating correctly, I could run ping silverflag.net which will send a ping packet to my website and wait for a response, with information about how long it took to get a response from the server, known as ping time (lower ping time is desirable in a lot of cases, like in gaming). The ping command has a lot of different options to play around with but I won't get into those right now.

Traceroute

Traceroute is a tool to debug networks. It is able to tell you all of the connections that your request over the internet travels through to be able to get you the content. For example, if I am having some issues where I am only able to sometimes connect to silverflag.net, I would traceroute silverflag.net to be able to see if there is an issue where if it is trying to route through a specific broken server that is causing the issue and get more information about how it is connecting to be able to debug it further.


Text Editting

Text editting is something that you are going to have to do a lot when you are working with a Unix system, because you have to go in and change configuration files, make code or scripts to be able to do things quickly, and a large number of other things. The terminal based text edittor that I would recommend for anyone that is new to the terminal is Nano. It is designed to be easier than competing terminal based text editors because it is able to tell you all of the commands that you might need on the bottom, in the competitors like eMacs and Vim, you need to know all of the keybinds to be able to save and exit the editor, which is difficult even for many experienced terminal users. In nano, when you want to open a file, you wil run the nano *filename* to open the file in nano, if there is any message about not having permissions, about the file being a directory, or any other mistake that you have made, then you should ctrl or control + x to exit. If the error is about permissions, then you could try sudo nano *filename* which uses sudo or "super user do" to open the file with high permission levels. Once you have finished editting your file, you can ctrl or control + s to save the changes, and then ctrl or control + x to exit the exitor.


Executing Code

When you are working in the terminal and you want to run a program, you are going to have to know the command to do so. We wont get into running a web page because that involves setting up an HTTP server like Apache2 or NGINX but I will go into detail about how you are able to mark bash scripts to allow execution of them and how to run a python file.

Python is really simple, you are just telling the python interpreter to run on a script. You do this like the following:

python3 script.py

For other code like bash scripts you are going to need to mark it as executable first so that you are able to run it.

There is an easier and a harder way to do this. I will show you the easier and then the harder:

chmod +x file.sh

Or, the harder one:

chmod 755 file.sh

To understand this better, look at this image pulled from Google.

This one is able to help you get a better understanding on more of the numbers. It helps if you know how to convert base 2 (binary) into base 10.


Other tips and tricks for the terminal.

Like I mentioned at the top, clear is very useful if you just ran a command and it spat out a lot of output, you are able to clear the terminal to be able to remove the clutter

Unix and Unix-like systems support operands in the terminal commands, for example, if I am updating my system on Ubuntu/Debian and I want to run the two commands required, I could issue both to run in one command using double ampersand: &&. The command would looke like sudo apt update && sudo apt upgrade. This will have the system run sudo apt update, wait for the command to finish, and then run the second command which is sudo apt upgrade.

Another useful operang on Unix-like systems is the | or the pipe. This is able to bring the output of one command and use it as an input to another command. This is usefull like in the curl example, or catting out a file, getting the output of the cat being the file and telling sh to use it, meaning sh runs it. It just litterally pipes the output into another command.

If you want something to run in the background, then you are able to add a single ampersand to the end of the command, like sudo apt update & the output will still be going into the terminal that you are using but it is able to allow you to run other commands while it is running that command. View commands running in background using jobs. If you need to stop on of the running jobs, look at its id, and run sudo kill %number with number being the id of the job.