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 filesCopy-Item [source] [destination] # Copy a file or directory from source to destination
Move-Item [source] [destination] # Move a file or directory from source to destination
Set-Location [directory] # Change the current directory to the specified one
Get-Location # Print the current working directory
Get-ChildItem # List the contents of the current directory
Get-ChildItem -Force # List the contents of the current directory, including hidden filescopy [source] [destination] # Copy a file or directory from source to destination
move [source] [destination] # Move a file or directory from source to destination
cd [directory] # Change the current directory to the specified one
cd # Print the current working directory
dir # List the contents of the current directory
dir /a # List the contents of the current directory, including hidden filesCreating 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 contentsNew-Item [file] # Create an empty file
New-Item -ItemType Directory [directory] # Create a new directory
Remove-Item [file] # Delete a file
Remove-Item -Recurse [directory] # Delete a directory and all of its contentstype nul > [file] # Create an empty file
mkdir [directory] # Create a new directory
del [file] # Delete a file
rmdir /s [directory] # Delete a directory and all of its contentsViewing 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 editorGet-Content [file] # Print the contents of a file to the terminal
Get-Content [file] | more # View the contents of a file one page at a time
Get-Content [file] | select -first 10 # Print the first 10 lines of a file
Get-Content [file] | select -last 10 # Print the last 10 lines of a file
notepad [file] # Open a file in the Notepad text editortype [file] # Print the contents of a file to the terminal
more < [file] # View the contents of a file one page at a time
findstr /N "^" [file] | findstr "^1^ ^2^ ^3^ ^4^ ^5^ ^6^ ^7^ ^8^ ^9^ ^10^" # Print the first 10 lines of a file
more +10 [file] # Print the last 10 lines of a file (starting from line 10)
notepad [file] # Open a file in the Notepad text editorSSH Secure Shell
ssh [username]@[server] # Connect to a remote serverssh [username]@[server] # Connect to a remote serverssh [username]@[server] # Connect to a remote serverSCP 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 destinationscp [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 destinationscp [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 destinationLook up History and Command information
history # List the last 1000 commands you have run
man [command] # View the manual page for a commandGet-History # List the last 1000 commands you have run
Get-Help [command] # View the manual page for a commanddoskey /history # List the last 1000 commands you have run
help [command] # View the manual page for a commandChange Permissions
chmod [permissions] [file] # Change the permissions of a fileGet-ACL [file] | Set-ACL -Path [file] # Change the permissions of a fileicacls [file] /grant [username]:F # Change the permissions of a fileTerminal 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-tutorialNew-Item -ItemType Directory terminal-tutorialmkdir terminal-tutorialYou 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
pwdSet-Location terminal-tutorial
Get-Locationcd terminal-tutorial
cdHere 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.txtNew-Item example.txttype nul > example.txtYou 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.notepad example.txt
# Inside Notepad, type "Hello World" on the first line.
# When you're done editing, click "File" > "Save" to save the changes.
# Close Notepad.notepad example.txt
# Inside Notepad, type "Hello World" on the first line.
# When you're done editing, click "File" > "Save" to save the changes.
# Close Notepad.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.txtGet-Content example.txttype example.txtThis 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.txtCopy-Item example.txt example_backup.txtcopy example.txt example_backup.txtThis 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.txtRemove-Item example_backup.txtdel example_backup.txt8. List Directory Contents
List the contents of the terminal-tutorial directory.
lsGet-ChildItemdirThese 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.txtGet-Item example.txt | Set-ItemProperty -Name IsExecutable -Value $trueicacls example.txt /grant *S-1-1-0:(RX)10. Change to the Parent Directory
Change the current directory to the parent directory of terminal-tutorial.
cd ..Set-Location ..cd ..This command moves you up one directory.
11. Remove the Directory
Remove the terminal-tutorial directory and its contents.
rm -r terminal-tutorialRemove-Item terminal-tutorial -Recursermdir /s /q terminal-tutorialCongratulations! 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 ssh -Y daq@daq3.physics.isu.edu ssh -Y daq@daq3.physics.isu.eduYou 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 scp [local_file_path]/terminal-tutorial/example.txt daq@daq3.physics.isu.edu:/home/daq scp [local_file_path]/terminal-tutorial/example.txt daq@daq3.physics.isu.edu:/home/daqYou 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 scp daq@daq3.physics.isu.edu:/home/daq/example.txt [local_file_path]/terminal-tutorial scp daq@daq3.physics.isu.edu:/home/daq/example.txt [local_file_path]/terminal-tutorialYou 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 scp -r [local_directory_path]/terminal-tutorial daq@daq3.physics.isu.edu:/home/daq scp -r [local_directory_path]/terminal-tutorial daq@daq3.physics.isu.edu:/home/daqCopy a Directory from a Remote Server to Your Local Machine
scp -r daq@daq3.physics.isu.edu:/home/daq/terminal-tutorial [local_directory_path] scp -r daq@daq3.physics.isu.edu:/home/daq/terminal-tutorial [local_directory_path] 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.
