Sub topic 2
Open a Terminal and Navigate to a Folder
A terminal is a window where you type instructions for your computer. It is useful when you want to run tools and work with files. In this chapter, you will open a terminal, find your current folder, and move to a folder for your Java practice.
A folder is a place where your computer organizes files and other folders. You may already use folders through a visual app, such as File Explorer on Windows or Finder on a Mac. A terminal gives you another way to work with them.
Open a terminal
Your operating system is the main software that manages your computer. Windows, macOS, and Linux are operating systems. Follow the steps for yours.
Windows
- Open the Start menu.
- Search for Windows Terminal or PowerShell.
- Open the app. If Windows Terminal opens, choose a PowerShell tab from its menu if one is not already open.
Use PowerShell for the Windows examples in this chapter. Windows also has a tool called Command Prompt, but its commands and output can differ.
macOS
- Press Command + Space to open Spotlight search.
- Type
Terminal. - Press Return to open the Terminal app.
Linux
Open your applications menu and search for Terminal. On many Linux computers, you can also press Ctrl + Alt + T.
Read the prompt
When the terminal opens, you will see a prompt. The prompt is the text that shows the terminal is ready for you to type.
A Windows PowerShell prompt may look like this:
PS C:\Users\Ari>
A macOS or Linux prompt may end with a symbol such as $ or %. The exact text depends on your computer and its settings.
A shell is the program inside the terminal that reads and runs your instructions. PowerShell is a shell. Common shells on macOS and Linux include Bash and Zsh.
A command is an instruction you give to the shell. To run one:
- Type the command after the prompt.
- Press Enter or Return.
- Wait for a new prompt before typing the next command.
You do not type the prompt itself. In this chapter, code blocks contain only the commands you should type. For example, type pwd, not PS C:\Users\Ari> pwd.
Find your current folder
The working directory is the folder your shell is currently using. “Directory” is another word for folder. When you ask the shell to look for a file by name, it normally starts in the working directory.
Type this command and press Enter:
pwdThe name pwd comes from “print working directory.” It shows where you are.
A Mac might show a location like /Users/ari. Linux might show /home/ari. PowerShell may show a Path heading with a location such as C:\Users\Ari underneath it. Your result will probably be different.
A path is text that describes the location of a folder or file. A path can include several folder names. Each name leads to a folder inside the one before it.
For example, in /home/ari, the folder ari is inside the folder home. On Windows, C:\Users\Ari describes a folder named Ari inside Users, on the C: drive.
See what is in the folder
To list the items in your working directory, run:
lsThe name ls is short for “list.” The output may include file names and folder names. A file is a named collection of information, such as a document.
PowerShell often shows columns with extra information. A macOS or Linux terminal may show only names. If nothing appears before the next prompt, the folder may have no visible items.
Some items are hidden by default, so a plain ls list may not show everything. You do not need to change that setting for this chapter.
Move to your home folder
Your home folder is the main personal folder for your user account. It is a useful starting place because you can usually create your own folders there.
Run this command:
cd ~The command cd means “change directory.” The ~ symbol is a shortcut for your home folder in the shells used here.
Now check your location and list its contents:
pwd
lsThese commands work in PowerShell and in common macOS and Linux shells. Your home folder may contain folders such as Documents or Downloads, but the names and locations can vary.
Changing folders in the terminal does not move or delete anything. It only changes where the shell looks when you use a name without a full path.
Create a practice folder
You will make a folder named JavaPractice inside your home folder. Keeping practice work in one folder makes it easier to find later.
First, make sure you are in your home folder:
cd ~
pwdNow create the folder:
mkdir JavaPracticeThe command mkdir means “make directory.” JavaPractice is the name of the folder to create.
If you already have a folder with that name in your home folder, you can skip the mkdir command. If you are unsure, run ls to look for it. Choose a different name if the existing item is a file or you do not want to use that folder.
Enter the practice folder:
cd JavaPracticeCheck that you are there:
pwd
lsThe path from pwd should end in JavaPractice. The ls command may show nothing if the folder is new and empty. That is fine.
The commands above use the same folder name with the same capital letters. This is a good habit. Many Linux systems treat JavaPractice and javapractice as different names. Windows and macOS often do not, but exact spelling is still safest.
Move between folders
A folder can contain another folder. The folder that contains it is called its parent folder.
Because you created JavaPractice inside your home folder, the home folder is its parent. To move up to the parent, run:
cd ..The two dots, .., mean “the parent folder.” Check where you are:
pwdYou should be back in your home folder. To return to your practice folder, run:
cd JavaPracticeRun pwd again to check. If you are not in the expected place, you can start over from your home folder:
cd ~
cd JavaPractice
pwdThe name JavaPractice works in the second command because that folder is directly inside your home folder. If you run cd JavaPractice from some other folder, the shell will look for JavaPractice inside that folder instead.
Understand relative and absolute paths
A relative path describes a location starting from your working directory. An absolute path describes a location starting from the top of the folder system, rather than from where you are now.
Here are some relative paths:
| Path | Meaning |
|---|---|
| `JavaPractice` | A folder named `JavaPractice` inside the working directory |
| `..` | The parent of the working directory |
| `.` | The working directory itself |
For example, when your working directory is your home folder, cd JavaPractice enters the practice folder. When your working directory is JavaPractice, cd .. returns to its parent.
An absolute path can look like /Users/ari/JavaPractice on a Mac, /home/ari/JavaPractice on Linux, or C:\Users\Ari\JavaPractice on Windows. These are examples, not paths to copy: your account name and folder location may be different.
On macOS and Linux, / separates parts of a path. On Windows, paths commonly use \. PowerShell also accepts / in many paths, but using the path shown by pwd can help you recognize where you are.
You can go to your practice folder from another location by using its absolute path with cd. If you try this, use the path printed by pwd on your own computer, not one of the example paths above.
Use names that contain spaces
A folder name can contain spaces. Put the name in quotation marks when you use it in a command. That tells the shell to treat the name as one piece.
Make sure you are in JavaPractice, then create a folder inside it:
cd ~
cd JavaPractice
mkdir "Meeting Notes"
cd "Meeting Notes"
pwdThe path should now end in Meeting Notes. The quotation marks are part of the command; they are not part of the folder name.
Return to the practice folder:
cd ..
pwd
lsYou should see Meeting Notes in the list. Without the quotation marks, the shell may read the words as separate pieces instead of one folder name.
Helpful terminal habits
Use completion for long names
Completion is a feature that can finish a name for you. Type the start of a folder name, then press Tab. The shell may fill in the rest.
For example, from your home folder, you can start by typing cd Java, then press Tab. If the shell finds a matching folder, it may complete the name. Press Enter only after checking the result.
Completion behavior varies between shells. If more than one name matches, you may need to type more letters or press Tab again.
Bring back an earlier command
Press the Up Arrow key to bring an earlier command back to the prompt. You can edit it before pressing Enter. This is useful when you want to run pwd or return to a folder without typing the whole command again.
Check before you press Enter
Look at the command and the current prompt before you run it. Check spaces, quotation marks, and spelling. Commands can change files, so do not run an instruction you do not understand just because you found it online.
The commands in this chapter are for viewing locations, moving between folders, and creating folders. They do not delete files.
Fix common navigation problems
The terminal says it cannot find the folder
First, check your current location and its contents:
pwd
lsMake sure you are in the folder that contains the one you want. Check the spelling and capital letters. If the name contains spaces, put it in quotation marks.
If you are trying to reach the practice folder from an unknown location, use:
cd ~
cd JavaPractice
pwdIf that still fails, check whether you created JavaPractice inside your home folder, or whether you chose a different name.
A command is not found
Check that you typed the command name correctly. For example, pwd has no spaces between its letters. Also check that you pressed Enter after the command.
On Windows, make sure you opened PowerShell rather than Command Prompt. The examples in this chapter are written for PowerShell and common macOS and Linux shells.
You cannot create a folder
If mkdir JavaPractice reports that the name already exists, run ls and check the name. You can use the existing folder if it is the one you want.
If the message says you do not have permission, you may be trying to create a folder somewhere your account cannot change. Return to your home folder with cd ~ and try there. Avoid changing system folders just to complete this practice.
You lost track of where you are
Run pwd. If the result is unfamiliar, use cd ~ to return to your home folder. From there, you can enter JavaPractice again.
Closing and reopening a terminal can also give you a fresh prompt. Your folders will still exist, but the new terminal may start in a different working directory. Use pwd to check.
Check your understanding
You have opened a terminal and practiced finding, creating, and entering folders. The key idea is that every shell command runs from a working directory. Check it with pwd whenever you are unsure.
Exercise
You are in your home folder, and JavaPractice is directly inside it. Which command enters that folder, and which command checks that you entered it?
Show answer
Run cd JavaPractice, then run pwd. The printed path should end in JavaPractice.
Exercise
You are inside JavaPractice. What command creates a folder named Project Notes, and what command enters it?
Show answer
Run mkdir "Project Notes", then run cd "Project Notes". The quotation marks keep the name together.