Tutorial: Terminal Basics

Overview

The terminal, also known as the command-line interface (CLI), is a text-based interface that allows you to interact with your computer’s operating system. By entering commands in the terminal, you can perform a wide range of tasks such as file management, system administration, and software installation. In this tutorial, you’ll learn the basics of using the terminal, including some common commands to help you get started.

Tutorial Objectives

  • Learn basic terminal commands for managing files and directories
  • Understand how to connect to a remote server using SSH
  • Know how to transfer files between your local machine and a remote server using SCP

Limitations

This tutorial is intended for beginners who have little to no experience using the command line interface. It covers basic commands and concepts and is not meant to be an exhaustive guide to using the terminal.

Other Resources

Download and Installation

Terminals are built into most operating systems, so there is no need to install additional software. To access the terminal:

  • macOS: Open the Terminal app (found in the Utilities folder within the Applications folder).
  • Windows: Use Windows PowerShell or the Windows Subsystem for Linux (WSL).
  • Linux: Open the terminal emulator for your distribution (e.g., GNOME Terminal, Konsole, xterm).

Basic Commands

Copying and Moving Files

In the terminal, you can use commands to copy and move files or directories. Below are the commands for macOS/Linux, Windows PowerShell, and Windows Command Prompt:

cp [source] [destination]   # Copy a file or directory from source to destination
mv [source] [destination]   # Move a file or directory from source to destination
cd [directory]     # Change the current directory to the specified one
pwd                # Print the current working directory
ls                 # List the contents of the current directory
ls -l              # List the contents of the current directory in long format
ls -a              # List the contents of the current directory, including hidden files

Creating and Deleting Files

touch [file]       # Create an empty file
mkdir [directory]  # Create a new directory
rm [file]          # Delete a file
rm -r [directory]  # Delete a directory and all of its contents

Viewing and Editing Files

cat [file]         # Print the contents of a file to the terminal
less [file]        # View the contents of a file one page at a time
head [file]        # Print the first 10 lines of a file
tail [file]        # Print the last 10 lines of a file
nano [file]        # Open a file in the nano text editor

SSH Secure Shell

ssh [username]@[server]  # Connect to a remote server

SCP Secure Copy

scp [source] [username]@[server]:[destination]  # Copy a file or directory from source to destination on a remote server
scp [username]@[server]:[source] [destination]  # Copy a file or directory from source on a remote server to destination

Look up History and Command information

history  # List the last 1000 commands you have run
man [command]  # View the manual page for a command

Change Permissions

chmod [permissions] [file]  # Change the permissions of a file

Terminal Tutorial

In this tutorial, we will demonstrate the basic use of terminal commands. Follow the steps below to learn how to manage files and directories, view and edit files, and perform other useful tasks.

Prerequisites

Make sure you have a terminal open and ready to use. Follow the instructions in the “Download and Installation” section of the main tutorial if you are unsure how to access the terminal on your system.

Part 1:

In the first part of this tutorial, we will learn how to create, edit, and delete files and directories. We will also learn how to view the contents of files and change their permissions.

1. Create a New Directory

First, let’s create a new directory called terminal-tutorial.

mkdir terminal-tutorial

You have now created a new directory called terminal-tutorial.

2. Change to the New Directory

Now, change the current directory to the terminal-tutorial directory that you just created. And print your working directory.

cd terminal-tutorial
pwd

Here you can see you have moved to another directory and the path is printed.

3. Create an Empty File

Create an empty file named example.txt inside the terminal-tutorial directory.

touch example.txt

You have now created a new file called example.txt.

4. Edit the File

Open example.txt in a text editor, add some content, and save the changes.

nano example.txt
# Inside nano, use the arrow keys to navigate and type "Hello World" on the first line.
# When you're done editing, press Ctrl + X to exit.
# Press Y to confirm that you want to save the changes.
# Press Enter to confirm the file name.

Now you have edited the file and saved your changes. There are many editors inside and outside the terminal, and over time you will likely use many of them. But this is a simple way to start. Here is a nano cheat sheet.

5. Display File Contents

Now, let’s display the contents of example.txt.

cat example.txt

This will print the entire contents of the file to the terminal. If you want less output, you can use the head command to print the first 10 lines of the file, or the tail command to print the last 10 lines of the file.

6. Create a Backup

Make a copy of example.txt named example_backup.txt.

cp example.txt example_backup.txt

This makes a copy of the file and names it example_backup.txt.

7. Delete a File

Delete the example_backup.txt file.

rm example_backup.txt

8. List Directory Contents

List the contents of the terminal-tutorial directory.

ls

These commands will print the contents of the current directory. So, you can see all of the files and directories inside.

9. Edit the permissions of a file

Change the permissions of the example.txt file to be executable.

chmod +x example.txt

10. Change to the Parent Directory

Change the current directory to the parent directory of terminal-tutorial.

cd ..

This command moves you up one directory.

11. Remove the Directory

Remove the terminal-tutorial directory and its contents.

rm -r terminal-tutorial

Congratulations! You have now successfully performed basic file and directory operations using the terminal. Keep practicing these commands to become more proficient in using the terminal.

Part 2

In part 2, we will learn how to connect to a remote server using SSH. We will connect to the daq3 server and run some commands.

12. Connect to a Remote Server using SSH

Use SSH to securely connect to a remote server.

    ssh -Y daq@daq3.physics.isu.edu

You will now be prompted to enter your password. Enter your password and press enter. Thats it! You are now connected to the daq3 server. You can use ‘ls’ and have a look at the file structure. Then go ahead and disconnect from the server by typing exit and pressing enter.

13. Transfer Files using SCP

Use SCP (Secure Copy) to transfer files between your local machine and a remote server. Make sure you have disconnected from the daq3 server before continuing.

Copy a File from Your Local Machine to a Remote Server

First create a directory again ‘terminal-tutorial ’ on your local machine as in part 1 of this tutorial. And create a file named ‘example.txt’ in that directory. If have trouble doing this, go back to part 1 of this tutorial.

Now, you will copy the ‘example.txt’ file from your local machine to the remote server.

    scp [local_file_path]/terminal-tutorial/example.txt daq@daq3.physics.isu.edu:/home/daq

You will be promtped again for your password. Enter your password and press enter.

Copy a File from a Remote Server to Your Local Machine

Now, you will copy the ‘example.txt’ file from the remote server to your local machine.

    scp daq@daq3.physics.isu.edu:/home/daq/example.txt [local_file_path]/terminal-tutorial

You will be promtped again for your password. Enter your password and press enter.

14. Transfer a Directory using SCP

Now you will transfer an entire directory from your local machine to the remote server. To transfer a directory and its contents, use the -r option.

Copy a Directory from Your Local Machine to a Remote Server

    scp -r [local_directory_path]/terminal-tutorial daq@daq3.physics.isu.edu:/home/daq

Copy a Directory from a Remote Server to Your Local Machine

    scp -r daq@daq3.physics.isu.edu:/home/daq/terminal-tutorial [local_directory_path]

That’s it! You now know how to use SSH to connect to a remote server and use SCP to transfer files between your local machine and the remote server. With these skills, you can efficiently manage your files and directories when working with remote systems.