ProgramGuru

Navigate Paths Before Changing Files

Navigate Paths Before Changing Files

Imagine the school keeps a term1 folder inside a larger school folder. Before opening a mark sheet, you need to know which folder you are standing in. The shell has a current working directory, which is the starting point it uses for relative paths. Changing that starting point changes what a short name can refer to.

A path is the sequence of names that leads to an entry. An absolute path begins at /, the filesystem root. A relative path begins at your current directory. This sounds small, but it explains a very common beginner problem: a file exists, yet a command cannot find it because the command is looking from somewhere else.

What you’ll learn

  • Explain the operation and choose it for the stated school task.
  • Read the command arguments and check the result rather than relying on silence alone.
  • Apply the worked task and recognize this boundary: An absolute path starts at /; a relative path starts at the current directory. .. selects the parent. A directory's execute/search permissions affect traversal even when its name is known.

The same name can refer to different places

  1. Before cd

    The current directory determines where a relative name starts.

    practice/
  2. Change directory

    cd school makes school the new starting point.

    practice/school/
  3. Resolve a name

    term1 now refers to the child of school, not a directory at filesystem root.

    school/term1
A filename is not a complete location on its own.

Enter the school folder and check its term directory

Prepare the example

Start in a fresh practice folder you own. The first command below will create both the school folder and its term folder. Run the lines in the same shell, because cd changes that shell's current directory. You can use pwd before or after the example to ask the shell to print your current location.

Bash · Linux terminal

mkdir -p school/term1
cd school
test -d term1 && printf 'Inside school; term1 exists\n'

Expected result

Inside school; term1 exists

Read mkdir -p school/term1 as 'create this directory path, including missing parents.' The next line, cd school, moves this shell's starting point into the school folder. It does not move the folder itself. From there, the short name term1 refers to the child we just created.

The last line asks test whether that child is a directory. The && joins two operations: the message is printed only if the check succeeds. Notice that /term1 would ask a different question. Its leading slash starts from the filesystem root, so it does not refer to this school's child folder.

Try it, then check the worked answer

Your task: From inside school, which relative path refers to its term1 subdirectory?

Start in a fresh practice folder you own. The first command below will create both the school folder and its term folder. Run the lines in the same shell, because cd changes that shell's current directory. You can use pwd before or after the example to ask the shell to print your current location.

Bash · complete example for this question

mkdir -p school/term1
cd school
test -d term1 && printf 'Inside school; term1 exists\n'

Expected result

Inside school; term1 exists

Worked answer

term1

You are already inside school, so the relative name term1 is enough. Adding a leading slash changes the starting point to the root; adding .. first changes it to the parent of school. Before choosing a path, say its starting point out loud. That habit makes file operations much easier to reason about.

Check your decision

From inside school, which relative path refers to its term1 subdirectory?
Choose one answer. You can try again.

Answer and explanation

term1

You are already inside school, so the relative name term1 is enough. Adding a leading slash changes the starting point to the root; adding .. first changes it to the parent of school. Before choosing a path, say its starting point out loud. That habit makes file operations much easier to reason about.

Resolve the path before changing a file

A filename is not a complete location on its own. Interpret it from the shell's current directory unless it begins with /. When a file appears to be missing, check your location and the path before recreating it or changing permissions. The same habit matters even more before moving or deleting anything.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Keep Filenames in One Argument →