انتقل إلى المحتوى
أمين أبوزيد
منشورBEG-006-L02

المسارات: المطلق والنسبي والرئيسي و. و..

  • الدرس 2 من 8
  • 11 دقيقة قراءة
  • مبتدئ
في هذا الدرس 11 موضوعات

هذا الدرس متاح حاليًا باللغة الإنجليزية فقط، ولم تُنشر نسخته العربية بعد.

Lesson Overview

Last lesson you met the shell and its prompt. The prompt usually shows where you are. This lesson teaches how to read that, how to move, and how to point any command at any file or folder on the computer.

The assistant from Lesson 1 is literal, so "the notes file" means nothing to it. It needs an exact path: a written address. A path is like a postal address. You can give the full address, "12 Main Street, Springfield, with the country", which works from anywhere. Or you can give directions from where you stand: "two doors down, then left". Both are useful, and mixing them up is the most common beginner error in this module.

By the end you will move around the folder tree with pwd, cd, and ls/dir, choose between absolute and relative paths, use the shortcuts ~, ., and .., and handle folder names that contain spaces.

This lesson builds on "Terminal, shell, and prompt" and on Module 5's lesson about files and folders. Commands are shown for Bash (Linux, macOS) and PowerShell (Windows). Folder names and the home folder differ between computers, so treat the sample paths as examples.

Learning Objectives

By the end of this lesson you will be able to:

  • Show your current folder with pwd (Bash) or Get-Location (PowerShell).
  • Change folder with cd, and list a folder with ls, dir, or Get-ChildItem.
  • Explain the difference between an absolute path and a relative path.
  • Use ~ for the home folder, . for the current folder, and .. for the parent folder.
  • Write a path to a file from two different starting folders.
  • Quote a path that contains spaces.
  • Predict where a cd command will take you before you press Enter.

Written Explanation

The folder tree and the current folder

Files live in folders, and folders live inside other folders. The result is a tree. At the very top is the root. On Linux and macOS there is one root, written /. On Windows each drive has its own root, such as C:\.

Whenever you work in a terminal, the shell has a current folder (also called the working directory). It is the folder you are "standing in". Many commands use it silently: if you say "list files" without naming a folder, you get the files in the current folder.

Your home folder is the folder that belongs to your user account. It is where a new terminal usually starts. Typical locations are /home/ana on Linux, /Users/ana on macOS, and C:\Users\Ana on Windows. The exact place depends on your system.

Here is the tree we will use for the examples (Linux style; on Windows, imagine C:\Users\Ana in place of /home/ana):

text
/home/ana
├── Documents
│   ├── notes.txt
│   └── School Work
│       └── essay.txt
└── projects
    └── shop
        └── README.md

Where am I? pwd

pwd stands for "print working directory". It prints the current folder as a full path.

text
$ pwd
/home/ana

PowerShell's equivalent is Get-Location. It also has the alias pwd, so pwd works in both shells.

text
PS> Get-Location

Path
----
C:\Users\Ana

Make pwd a reflex. Before any command that changes or deletes things, check where you are.

What is here? ls, dir, Get-ChildItem

ls lists the contents of a folder in Bash. PowerShell's real command is Get-ChildItem; ls and dir are aliases for it, so all three work in PowerShell on Windows. (On PowerShell for Linux or macOS, which aliases exist can differ, so if ls misbehaves, use Get-ChildItem.)

text
$ ls
Documents  projects

With no argument, the command lists the current folder. Give it a path and it lists that folder instead: ls Documents. In Bash, ls -l shows a detailed list with sizes and dates; Lesson 3 uses it more.

Moving: cd

cd means "change directory". Give it a path and your current folder becomes that folder.

text
$ cd Documents
$ pwd
/home/ana/Documents

cd prints nothing on success. If the path does not exist, you get an error and stay where you were.

Absolute paths

An absolute path starts at the root and spells out every folder on the way. It means the same thing no matter where you are.

  • Linux/macOS: /home/ana/Documents/notes.txt. It begins with /.
  • Windows: C:\Users\Ana\Documents\notes.txt. It begins with a drive letter and \.

Windows separates folders with a backslash \; Linux and macOS use a forward slash /. PowerShell accepts both on Windows, but Bash accepts only /. In Bash, the backslash has a different job (it is an escape character), so do not type Windows-style paths there.

Use an absolute path when you want certainty, for example in a script that must work from any folder.

Relative paths

A relative path does not begin at the root. It starts from the current folder. If you are in /home/ana, then Documents/notes.txt means /home/ana/Documents/notes.txt. The same text typed from somewhere else would mean something different, or nothing at all.

How do you tell them apart? An absolute path starts with / (Linux, macOS) or a drive letter (Windows). Everything else is relative.

Relative paths are short and convenient inside a project, but they depend on where you are. That is why pwd matters.

The shortcuts: ~, ., and ..

Three tiny symbols do a lot of work.

SymbolMeaningExample
~Your home foldercd ~ takes you home. ~/Documents is a path inside it.
.The current folder./run.sh means "the file run.sh in this folder".
..The parent folder, one level upcd .. goes up one level. cd ../.. goes up two.

The shell (Bash) expands ~ to your home folder; PowerShell understands ~ as home as well when you use it in a path. If you are unsure in your PowerShell version, type $HOME instead, which holds the home folder.

You can chain .. to climb as many levels as you like. From /home/ana/projects/shop, the folder Documents is at ../../Documents: up to projects, up to ana, then down into Documents.

The . is easy to overlook, but it matters. You will meet it again in Lesson 6, where running a script in the current folder is written ./script.sh.

The same file, two ways

From /home/ana/projects/shop:

  • Absolute: /home/ana/Documents/notes.txt.
  • Relative: ../../Documents/notes.txt.

Both name the same file. If you move to another folder, the absolute path still works while the relative one must be rewritten.

Paths with spaces

The shell uses spaces to separate words, so this fails:

text
$ cd School Work

The shell thinks School and Work are two arguments. Wrap the path in quotation marks so it stays one word:

text
$ cd "School Work"

Single quotes also work in both shells. A second approach, in Bash, is to put a backslash before each space: School\ Work. In PowerShell, the escape character is the backtick, not the backslash. You can also press Tab to autocomplete, which usually adds the quoting or escape for you.

When you are naming your own folders, avoid spaces and use short, lowercase names such as school-work. Programmers do this to avoid the problem.

Reading an error

If the path is wrong you will see a message such as No such file or directory in Bash or Cannot find path ... because it does not exist in PowerShell. Do not guess. Run pwd and ls, and look at what is actually there. The exact wording differs between shells and versions.

Examples

Example 1: Walking down and back up (simple)

Starting in /home/ana:

text
$ pwd
/home/ana
$ cd projects
$ cd shop
$ pwd
/home/ana/projects/shop
$ cd ..
$ pwd
/home/ana/projects

Each cd with a relative name moves one step down. cd .. moves one step up. On Windows PowerShell the same commands work, with pwd answering C:\Users\Ana\projects\shop.

Example 2: One file, three ways in (practical)

Goal: list README.md from /home/ana/Documents. Here is the file's absolute path: /home/ana/projects/shop/README.md.

text
$ ls /home/ana/projects/shop/README.md      # absolute: works from anywhere
$ ls ../projects/shop/README.md             # relative: up one level, then down
$ ls ~/projects/shop/README.md              # home shortcut

All three name the same file when you are in Documents. Now suppose you are in /home/ana/projects/shop instead. The first and third commands still work, but the second now fails, because .. leads to /home/ana/projects, which has no projects folder inside it. Whether a relative path works depends entirely on where you start, so run pwd first.

Example 3: The folder with a space (realistic)

You want to open essay.txt in School Work. Both attempts below start in /home/ana/Documents:

text
$ cd School Work         # fails: two arguments
$ cd "School Work"       # works
$ pwd
/home/ana/Documents/School Work

In PowerShell, the same quoted command works. Back out with cd .., or jump straight home with cd ~.

Backend Insight

Paths are everywhere in backend work. A server's configuration file says where its logs are written. A deployment script says where the program lives. A Java program asks to open a file by path, and the same program can behave differently depending on the working directory it was started from, because relative paths start there. A common server bug is "it works on my laptop but not on the server", caused by a relative path that points somewhere else after deployment.

Real servers usually run Linux, so forward-slash paths, absolute locations like /var/log, and case-sensitive names will become familiar. Practising with both styles now will save you trouble later.

Common Mistakes

Mistake 1: Forgetting the quotes around a path with spaces

  • Incorrect: cd School Work
  • Correct: cd "School Work"

Mistake 2: Assuming a relative path works from anywhere

Documents/notes.txt only works when your current folder contains Documents. If a path "worked yesterday" and fails today, you are probably in a different folder. Run pwd.

Mistake 3: Mixing slash styles

Typing C:\Users\Ana in a Bash shell fails, because Bash treats backslashes as escape characters. In Bash use /. In PowerShell on Windows, either style works.

Mistake 4: Confusing . and ..

. is here and .. is one level up. cd . does nothing and cd .. moves. Count the dots before you press Enter.

Exercises

Try each exercise yourself before opening the solutions. Use this tree: /home/ana/Documents, /home/ana/projects/shop, /home/ana/projects/blog.

Level A: Foundation

A1. Which of these paths is absolute: projects/shop, /home/ana/projects, ../blog, ~/Documents?

أظهر الحل A1

A1. /home/ana/projects is absolute because it starts at the root. ~/Documents is a shortcut that the shell expands to the full home path (/home/ana/Documents) before the command runs, so it behaves like an absolute path. projects/shop and ../blog are relative.

A2. What does .. mean? What does . mean?

أظهر الحل A2

A2. .. is the parent folder (one level up). . is the current folder.

A3. You are in /home/ana/projects/shop. Where does cd .. take you? Where does cd ../.. take you?

أظهر الحل A3

A3. cd .. goes to /home/ana/projects. cd ../.. goes to /home/ana.

Level B: Application

B1. You are in /home/ana/projects/shop. Write a relative cd command that takes you to /home/ana/projects/blog, and an absolute one.

أظهر الحل B1

B1. Relative: cd ../blog. Absolute: cd /home/ana/projects/blog.

B2. In your own terminal, use pwd, then cd into any folder under your home, then return home using ~. Write down each command and what pwd showed.

أظهر الحل B2

B2. Answers depend on your computer. Example: pwd printed /home/ana, cd Documents, pwd printed /home/ana/Documents, cd ~, pwd printed /home/ana.

B3. Fix this command so it works in either shell: cd Photos from 2024.

أظهر الحل B3

B3. Quote it: cd "Photos from 2024".

Level C: Challenge

C1. You are in /home/ana/Documents and want to list /home/ana/projects/shop/README.md using a relative path, and again using ~. Write both commands.

أظهر الحل C1

C1. Relative: ls ../projects/shop/README.md. With home: ls ~/projects/shop/README.md.

C2. A teammate's script runs cat config/app.properties and works when started from the project folder but fails when started from the folder above it. Explain why, and suggest one fix.

أظهر الحل C2

C2. config/app.properties is a relative path, so it starts from wherever the script was run. From the folder above, config does not exist. Fix: run the script from the project folder, or use an absolute path, or have the script change into the project folder first.

Mini Challenge

Draw a map of a real folder tree on your computer, three levels deep, starting from your home folder. Then answer, with commands you actually ran:

  1. The absolute path of one file near the bottom of the tree.
  2. A relative path to that file from a different folder of your choice.
  3. The cd command that takes you from the file's folder back to your home folder, using .., and a second one using ~.

Check your work: you used pwd to confirm each starting point, and quoted any path with spaces.

Summary & Next Lesson

Key takeaways

  • The shell always has a current folder. pwd shows it, cd changes it, and ls/dir/Get-ChildItem lists it.
  • An absolute path starts at the root and works from anywhere. A relative path starts at the current folder.
  • ~ is home, . is here, .. is the parent.
  • Quote any path that contains spaces.
  • When a path fails, check pwd and ls before changing anything.

Next lesson

You can now find your way around. The next lesson, "Working with files and folders", uses paths to create, copy, move, and delete files from the terminal, and it explains why you must check your location before deleting.

Self-check

  1. What does pwd show?
  2. Which is absolute: Documents/a.txt or /home/ana/Documents/a.txt?
  3. From /home/ana/projects, what does cd ../Documents do?
  4. Why does cd My Files fail?
  5. Which slash does Bash use in paths?
أظهر الإجابات
  1. The current folder.
  2. /home/ana/Documents/a.txt.
  3. It goes up to /home/ana, then into Documents, so you end in /home/ana/Documents.
  4. The space splits it into two arguments. Use cd "My Files".
  5. The forward slash /.

References

  • man7.org, bash(1) manual page. Supports the statement that an unquoted ~ at the start of a word expands to the home folder (the value of HOME).
  • Microsoft Learn, Remove-Item. Its alias list shows that PowerShell command aliases differ between Windows and other platforms, which is why the text hedges ls and rm in PowerShell.
  • Not verified on an official page: the exact output of pwd, Get-Location, and ls, the error wording, ~ handling in every PowerShell version, that PowerShell accepts both slash styles, the Get-Location alias pwd, and typical home folder locations. These are standard behavior that I did not confirm, and the text hedges them.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

المختبر والتمارين الاختبار

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.