Sub topic 2
Open a Terminal and Navigate to a Folder
A terminal is a window where you can control your computer by typing. You type a command, an instruction for the computer, and press Enter to run it.
A folder holds files and can also hold other folders. In this chapter, you will use a terminal to make and open a folder. You can use that folder for your Java work later.
Open a terminal
Use the instructions for your computer:
- Windows: Press the Windows key, type
PowerShell, and open Windows PowerShell. - macOS: Press Command + Space, type
Terminal, and open Terminal. - Linux: Look for an app named Terminal. On many systems, you can also press Ctrl + Alt + T.
The terminal shows a prompt, a mark that tells you it is ready for a command. The prompt may also show a folder name. Its appearance can vary.
Type only the commands shown in the examples. Do not type the prompt. Press Enter after each command.
Find your current folder
The working directory is the folder where the terminal is currently working. A directory is another name for a folder. Many commands use the working directory unless you tell them to use a different one.
To see your working directory, type:
pwdThe name pwd means “print working directory.” The result is a path, which tells you where a folder is on your computer.
For example, a path might look like C:\Users\Sam on Windows or /home/sam on Linux. Your result will be different. PowerShell may also show a heading above the path.
Go to your home folder
Your home folder is your personal folder on the computer. To make sure you start from a known place, run:
cd ~The command cd means “change directory.” The symbol ~ represents your home folder.
Check where you are:
pwdThe result should show the path to your home folder. Changing the working directory does not move or change any files. It only tells the terminal where to work.
Make and open a practice folder
The command mkdir means “make directory.” Run this command to make a folder named JavaPractice inside your current folder:
mkdir JavaPracticeIf a folder named JavaPractice already exists there, you can use it and skip this command. If a file has that name instead, choose another folder name and use it in the later commands too.
Now open the folder in the terminal:
cd JavaPracticeCheck your location again:
pwdThe path should now end with JavaPractice. For example, it might end with \JavaPractice on Windows or /JavaPractice on macOS or Linux.
To see the folder’s contents, use ls, which means “list”:
lsA new folder is usually empty, so you may see no result. The command normally leaves out hidden items.
Move between folders
A parent folder is the folder that holds another folder. Your home folder is the parent of the JavaPractice folder you just made.
Two dots, .., mean “the parent folder.” To go up one level, run:
cd ..Run pwd to check that you are back in your home folder. Then return to your practice folder:
cd JavaPracticeThe name JavaPractice is a relative path: it tells the terminal where to go from the current working directory. The full path shown by pwd is an absolute path: it gives the location from the start of the computer’s folder structure.
Use folder names with spaces
If a folder name contains spaces, put the name in quotes. Otherwise, the terminal may treat the words as separate parts of the command.
While you are in JavaPractice, make and open a folder named My Practice:
mkdir "My Practice"
cd "My Practice"
pwdIf My Practice already exists as a folder, skip the first command. The path printed by pwd should end with My Practice. Go back to JavaPractice with:
cd ..When a command does not work
- The terminal cannot find the folder: Run
pwdto check where you are, then runlsto see the available names. Check your spelling. On many systems, capital and lowercase letters can matter. - A name with spaces causes an error: Put quotes around the whole folder name, as in
cd "My Practice". - The command says the folder already exists: You do not need to make it again. Use
cdto open it, as long as it is a folder. - You closed the terminal: Open it again and use
pwdto check your location. To return to the practice folder, runcd ~and thencd JavaPractice.
Exercise
You are in your home folder, and it contains a folder named JavaPractice. Which commands open it, check your location, and list its contents?
Show answer
Run cd JavaPractice, then pwd, then ls. Press Enter after each command.
Exercise
You are in JavaPractice. How do you return to its parent folder, and how can you check where you are?
Show answer
Run cd .., then run pwd to display the current path.