Docs:00.2 Terminal Unix Commands Cheat Sheet

This page was last edited on 19 February 2025, at 20:55.

Linux and Mac OS systems have Unix Shells that provide us a command line interface. e.g. Bash Shells or Zsh

Windows uses Power Shell by default, which has a different command language. If you are on Windows you will need a Bash shell installed for the following commands.

Much of working on the command line involves navigating around your system without the use of a Graphical User Interface (GUI). Below we have a representation of a folder (or directory) hierarchy with a plain text .txt file nested inside two folders:

rootdirectory/ 
   ├─ anotherdirectory/ 
       ├─ aFileName.txt

Which could be written as "/rootdirectory/anotherdirectory/aFileName.txt"

In reality when working on the command line your root folder/directory for your user account on your system will looking something like below (in this case the user is pubdoc), the root folder/directory is denoted by a tilde ~

user name on the command line example

Terms

A few terms we need to be comfortable with before moving forward are:

  • directory : also known as a folder
  • root directory : “highest” directory in the hierarchy. You can also think of it as the start of a particular folder structure, denoted by a tilde ~ on the command line.
  • directory path : in the example above, there would be a directory called “rootdirectory”, which has a folder called “anotherdirectory” inside of it, the path would be: /rootdirectory/anotherdirectory
  • file : a file, e.g. file.txt or index.html

Basic Commands

The following commands will help us get started, but are just the tip of the iceberg

Show the name of the currently logged in user:

whoami

Show the name of the machine is given on the network:

hostname

sudo stands for Super User DO. This command is used to allow you to run commands that only super users are allowed to run, and is often followed by the command you want to run. When you use sudo you will be prompted to enter your super user password to execute the command:

 sudo <command>

If you want to enter into sudo mode to run all commands as a super user, run:

 sudo su

(this will then prompt you to enter your password and after which you can run commands as a super user)

Print working directory that you are currently in:

pwd

Change directory to a directory using the “path” to that folder/directory:

cd /directoryname/anotherdirectory

Change directory to root :

cd

Change directory to the directory above the one you are currently in:

cd ..

List current directory contents:

ls

Make new directory. When working on the command line it is customary for anything that is variable, i.e. in the example below mkdir is our command, and you would then write the name of the folder:

mkdir directoryname

However, in instructions you might see something like this:

mkdir <directoryname>

Make new file:

touch <example.txt>

A quick way to print contents of a file to your terminal:

cat <example.txt>

Copy a text file called example.txt which is in a sub directory “exampleFolder” to a new location /path/to/folder:

cp exampleFolder/example.txt /path/to/folder

Remove a file, be careful with removing files!

rm <file.txt>

Remove an empty directory:

rmdir <directoryname>

Remove a directory and all the items inside of it:

rm -r <directoryname>

Command Line Text Editors

Nano

Edit a file using nano command line text editor:

nano <example.txt>

Simple quick start for editing a text file:

  • You will see the nano editor open. At the bottom of the window, you will find some shortcuts to use with the Nano editor. The ^ (caret) means that you must press CTRL (Windows) or control (macOS) to use the chosen command.
  • You should be able to edit and enter text in your file.
  • Press CTRL + O to save the changes made in the file and continue editing.
  • To exit from the editor, press CTRL + X. If there are changes, it will ask you whether to save them or not. Input Y for Yes, or N for No, then press Enter. If there are no changes, you will exit the editor right away.

getting started with nano, dive deeper into nano

Vim

Edit a file using vim command line text editor:

vim <example.txt>

Simple quick start for editing a text file:

  • Press i to insert, then you can make changes to your file.
  • Once you are finished changing your file press escape esc to exit out of insert mode.
  • Then you should type :wq and hit enter to write and quit (save your file and quit).
  • The command to then exit Vim is :q

vim is less beginner friendly, dive deeper into vim