Using the Terminal

1 What is the terminal?

The terminal is a text-based interface for your computer. Instead of clicking on icons, you type commands and press Enter to run them.

This might feel unfamiliar at first, especially if you are used to R with RStudio, which has a graphical interface. But the terminal is the main way bioinformaticians interact with computing clusters, and after a little practice the commands become second nature.

This page is a follow-along exercise. Work through the steps in order and run each command yourself.

Tip

Every command on this page is safe to run. You will not break anything by following these steps.


2 Open a terminal

On macOS: Open the Terminal app. Find it in Applications → Utilities, or use Spotlight: press ⌘ + Space, type “Terminal”, press Enter.

On Windows: Open Windows Terminal or PowerShell.

You should see a prompt that looks something like:

your_username@your-computer ~ %

The terminal is waiting for you to type a command. Press Enter after each command to run it.


3 Make the terminal easier to read (macOS only)

Before jumping into commands, run this one-time setup to add colors and syntax highlighting to your terminal. Colors make it much easier to tell files from folders, spot typos, and read output at a glance.

Note

This section is for macOS only. If you are on Windows, skip ahead to the exercise.

3.1 Color the prompt

The prompt is the line that appears every time the terminal is waiting for a command:

(base) your_username@your-computer ~ %

By default it is plain white on a dark background or plain black on a light background. This line makes it display in white so it stands out clearly:

echo 'PROMPT="%F{white}%n@%m %~ %% %f"' >> ~/.zshrc

What each part means:

Part Meaning
%F{white} Start printing in white
%n Your username
@ The literal @ symbol
%m Your computer name
%~ Your current directory (using ~ shorthand for home)
%% The % prompt character
%f Stop the color (reset to default)
Note

If conda is installed, it automatically adds (base) in front of your prompt when the base environment is active. That prefix will still appear, and the rest of the prompt will be white.

3.2 Add colors to ls

By default, ls lists everything in plain white text. These two lines turn on color coding so folders, files, and special items each appear in a different color:

echo 'export CLICOLOR=1' >> ~/.zshrc
echo 'export LSCOLORS=GxFxCxDxBxegedabagaced' >> ~/.zshrc

3.3 Add syntax highlighting

Syntax highlighting colors your commands as you type. Recognized commands appear in green, unknown commands in red, and file paths in a different color. This makes it easy to catch typos before you press Enter.

First, install the highlighting package using Homebrew:

brew install zsh-syntax-highlighting

Then tell your terminal to load it every time it starts:

echo 'source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh' >> ~/.zshrc

3.4 Apply the changes

source ~/.zshrc

This reloads your terminal settings without closing and reopening the window. You should immediately see colors when you run ls or start typing commands.

3.5 What are hidden files?

When you ran echo '...' >> ~/.zshrc, you were writing into a hidden file. Any file or folder whose name starts with a . is hidden. Hidden files do not appear in Finder or in a plain ls because they are configuration files you do not usually need to touch day to day.

Common hidden files you will encounter:

File What it is
~/.zshrc Terminal configuration, runs every time you open a new window
~/.bash_profile Same idea, but for the older bash shell
~/.gitignore Tells Git which files to ignore
~/.ssh/ Folder containing SSH keys used to connect to remote servers

To see hidden files in the terminal, add the -a flag to ls:

ls -a

To see hidden files with full details:

ls -lha

You will now see entries starting with . that were invisible before, including . (the current folder itself) and .. (the folder above it).

Note

On macOS Finder, you can toggle hidden files with Cmd + Shift + . and they will appear greyed out.


4 Follow-along exercise

4.1 Step 1: Find out where you are

pwd

pwd stands for print working directory. It shows your full location in the filesystem.

You should see something like:

/Users/your_username

Your path will show your own username. This is your home directory, the folder that belongs to you.


4.2 Step 2: Move to the Desktop

cd Desktop

cd stands for change directory. It moves you into a different folder.

Confirm you moved:

pwd

You should see:

/Users/your_username/Desktop

4.3 Step 3: Create a new folder

mkdir practice

mkdir stands for make directory. This creates a new folder called practice on your Desktop. You should be able to see it appear there.

Move into the new folder:

cd practice

Confirm your location:

pwd

You should see:

/Users/your_username/Desktop/practice

4.4 Step 4: Navigate up and down, and understand file paths

You are currently inside ~/Desktop/practice. Move up one level to the Desktop:

cd ..

.. always means “the folder above where I currently am”. Confirm:

pwd

You should see:

/Users/your_username/Desktop

Now go back into practice using its full path:

cd ~/Desktop/practice

Confirm:

pwd

You should see:

/Users/your_username/Desktop/practice

You just navigated the same way using two different approaches. This is the difference between relative paths and absolute paths.

Relative paths describe a location relative to where you currently are. They do not start with / or ~:

cd practice      # only works if you are already in ~/Desktop
cd ..            # always means one folder up from your current location

Absolute paths describe the full location starting from the top of the filesystem. They always start with / or ~ (which is shorthand for your home directory):

cd ~/Desktop/practice                      # works from anywhere
cd /Users/your_username/Desktop/practice   # exactly the same, written out in full
Tip

When you are unsure where you are, use an absolute path. It works from anywhere and never depends on your current location.


4.5 Step 5: Create a text file with nano

nano is a simple text editor that runs inside the terminal. Open a new file called notes.txt:

nano notes.txt

The screen changes, you are now inside the editor. You can type normally. The bottom of the screen shows keyboard shortcuts:

^O  Write Out (save)       ^X  Exit

The ^ symbol means Ctrl. So ^O means Ctrl + O.

Type the following 30 lines into the editor. On macOS you can paste with Cmd + V:

Hello, this is a terminal tutorial.
I am practicing using the command line.
The terminal is a text-based interface for my computer.
Commands are typed and run by pressing Enter.
pwd shows my current location.
cd moves me to a different folder.
mkdir creates a new folder.
nano opens a text file for editing.
cat shows the contents of a file.
head shows the first 10 lines of a file.
tail shows the last 10 lines of a file.
mv moves or renames a file.
cp copies a file.
ls lists files in a folder.
The terminal is case-sensitive.
File and folder names matter exactly.
Avoid spaces in names and use underscores instead.
Press Tab to autocomplete a file or folder name.
Press the up arrow key to repeat a previous command.
Press Ctrl+C to cancel a running command.
Servers and clusters are accessed through the terminal.
Bioinformatics tools often run on remote computers.
Scripts automate repetitive tasks.
Checking logs helps diagnose problems.
Temporary storage is used for large files during analysis.
Keeping organised folders makes research easier.
Version control tracks changes in your scripts over time.
Containers package software so it runs the same everywhere.
Pipelines automate multi-step analyses.
The terminal is one of the most powerful tools in bioinformatics.

To save: press Ctrl + O, then press Enter to confirm the filename.

To exit nano: press Ctrl + X.

You are back at the regular terminal prompt.


4.6 Step 6: List files

Now that you have created a file, list what is in the current folder:

ls

You should see:

notes.txt

Now run it with flags for more detail:

ls -lh

You should see something like:

-rw-r--r--  1 your_username  staff   1.1K Aug  7 14:23 notes.txt

Here is what each part means:

Part Example Meaning
Type and permissions -rw-r--r-- What kind of item it is and who can read, write, or run it
Links 1 Number of hard links (usually 1 for files)
Owner your_username The user who owns the file
Group staff The group associated with the file
Size 1.1K File size in human-readable units (K/M/G instead of raw bytes)
Date Aug 7 14:23 When the file was last modified
Name notes.txt The file or folder name

Reading the permissions string

The first column (-rw-r--r--) is 10 characters long:

- rw- r-- r--
| |   |   |
| |   |   other users (everyone else): read only
| |   group: read only
| owner (you): read and write
file (-) or directory (d)
  • r means read permission (can open and view)
  • w means write permission (can edit or delete)
  • x means execute permission (can run as a program)
  • - means that permission is not granted

A text file typically shows rw- for the owner and r-- for everyone else. A folder shows d at the start instead of -.


4.7 Step 7: Read the file

Print the entire file to the terminal:

cat notes.txt

All 30 lines will appear.

Show just the first 10 lines:

head notes.txt

Show just the last 10 lines:

tail notes.txt
Tip

head and tail are useful when a file is very large, too large to print the whole thing at once. A quick head or tail gives you a preview without waiting for everything to scroll past.


4.8 Step 8: Rename a file

mv stands for move, but it is also how you rename a file. When you point it at two names in the same folder, it renames the file in place:

mv ~/Desktop/practice/notes.txt ~/Desktop/practice/notes_backup.txt

Confirm the rename worked:

ls ~/Desktop/practice/

You should see:

notes_backup.txt

The original notes.txt is gone and notes_backup.txt is in its place. The file contents are unchanged.


4.9 Step 9: Move a file

Now move the file out of practice and onto the Desktop using full paths for both source and destination:

mv ~/Desktop/practice/notes_backup.txt ~/Desktop/

Confirm the file is gone from practice:

ls ~/Desktop/practice/

The folder should be empty, no output, or a blank line.

Confirm the file is now on the Desktop:

ls ~/Desktop/

You should see notes_backup.txt listed among your Desktop items.


4.10 When output won’t stop: Ctrl+C vs q

You will run into two situations where a command takes over your terminal and you need to get out.

Situation 1: cat on a huge file

If you accidentally run cat on a very large file, thousands of lines start scrolling past and will not stop on their own. Press Ctrl+C to cancel the command immediately.

cat huge_file.txt   # thousands of lines start scrolling...
# press Ctrl+C to stop

Ctrl+C cancels whatever command is currently running. Use it any time a command is doing something and you want it to stop.

Situation 2: a pager opens (less, man)

Some commands open an interactive viewer called a pager instead of dumping everything to the screen at once. You will see this with less and man. The pager lets you scroll up and down through the content.

To open a file in the pager:

less ~/Desktop/notes_backup.txt

Inside less:

  • Arrow keys or j / k to scroll line by line
  • Space to jump down a full page
  • q to quit and return to the terminal

The key difference: if the terminal is waiting for a command, use Ctrl+C. If a pager or interactive program is open, use q.

Note

A quick way to tell which situation you are in: if you can see a : or (END) at the very bottom of the screen, you are inside a pager. Press q to exit. If the terminal prompt ($ or %) is not visible and output is still printing, press Ctrl+C.


5 You are done

You have practiced the core terminal commands you will use throughout this tutorial:

What you did Command
Checked your location pwd
Moved into a folder cd folder_name
Moved up one folder cd ..
Navigated using a full path cd ~/Desktop/practice
Created a folder mkdir
Created and edited a file nano
Listed files ls
Listed files with details and permissions ls -lh
Saw hidden files ls -a or ls -lha
Printed a whole file cat
Previewed the start of a file head
Previewed the end of a file tail
Renamed a file mv old_name new_name
Moved a file mv /full/path/file /destination/
Deleted a file rm file.txt
Deleted a folder and everything in it rm -rf folder/
Stopped a running command Ctrl+C
Exited a pager q

6 What is a shell script?

If you have written code in R, you already understand the idea of a script: a text file containing commands that run in order from top to bottom.

A shell script is exactly the same thing, except the commands are terminal commands instead of R commands. When you run a shell script, the computer reads it from top to bottom and executes each line, just like R runs a .R file line by line.

Shell scripts usually end with .sh. For example:

#!/bin/bash
mkdir -p results/raw
echo "Folders created."

That is a three-line shell script. The first line (#!/bin/bash) tells the computer which program to use to interpret the commands, think of it like a library() call in R that sets up the environment. The next lines are commands you would type manually, saved so they run automatically.

Tip

The .sh extension is a convention, not a requirement. Both work, the extension is just a label to remind you what the file does.


7 Quick command reference

Goal Command
See where you are pwd
List files ls
List files with sizes and permissions ls -lh
List all files including hidden ls -a or ls -lha
Move into a folder cd folder_name
Move up one folder cd ..
Go to home directory cd ~
Navigate using full path cd ~/Desktop/practice
Create a folder mkdir folder_name
Create nested folders mkdir -p parent/child
Create or edit a file nano file.txt
Print a file cat file.txt
Preview the start head file.txt
Preview the end tail file.txt
Watch a file update live tail -f file.txt
Scroll through a large file less file.txt
Rename a file mv old_name.txt new_name.txt
Move a file mv /source/file.txt /destination/
Delete a file rm file.txt
Delete a folder and everything in it rm -rf folder/
Copy a file cp source destination
Copy a folder cp -r source/ destination/
Check folder size du -sh folder/
Check disk space df -h .
Cancel a running command Ctrl+C
Delete the entire current line Ctrl+K
Exit a pager q

8 Tips for beginners

  • File and folder names are case-sensitive. Results and results are two different folders.
  • Avoid spaces in file names, use underscores _ or hyphens - instead.
  • Press Tab to autocomplete a file or folder name. Press it twice to see all matches if there are several.
  • Use the ↑ arrow key to scroll back through previous commands, no need to retype.
  • Read error messages carefully, they usually tell you exactly what went wrong.
  • If a command seems stuck, press Ctrl+C to cancel it.