Concepts / Directories and the File System

Directories and the File System

A path name gives both a file's name and the directory where the file is located, which is needed whenever the file is not in the current directory.

  • Programming

Why a Bare File Name Can Be Ambiguous

Imagine a program that is asked to open a file called data.dat. Before it can do anything useful, it needs to know which data.dat is meant. Many computers store large numbers of files with identical names in different folders, so the name by itself does not settle the question. The fix is to describe the file's location as well as its name. Working out how that description is written, and how it is read, is the subject of this article.

What do you think happens?

A program opens the bare name data.dat with no directory given. Which file does the system use?

  • The data.dat file in the current directory

  • The data.dat file that was created most recently anywhere on the disk

  • The data.dat file stored in the root directory

Reveal answer
Answer:

The data.dat file in the current directory

A simple file name is interpreted relative to the current directory, also called the default or working directory. A bare name carries no directory information, so the system silently assumes the file sits in that current directory. Any file outside it cannot be reached with a bare name.

The Two Parts of a File Location

A complete location for a file has two parts: the name of the file itself, and the directory in which that file is stored. The combined text that carries both pieces is called a path name. A path name therefore gives both a file's name and the directory where the file is located. It is needed whenever the file is not in the current directory.

The current directory works as a reference point. The system silently applies it to every name that does not spell out a location. Because a user or a program can change the current directory, the same bare name can refer to different files at different times. That is why a bare name is only safe when the file really sits in the directory the system is currently using.

Following Directories as a Tree

Directories nest inside one another, so a file system is best pictured as a tree. Each directory can hold files and further directories. A path name is simply a written route through that tree. The diagram below shows a small slice of such a tree, starting from the top and descending to the file data.dat, which sits inside examples, which sits inside java, and so on up to the root.

/ (root)homeeckjavaexamplesdata.datcontainscontainscontainscontainscontains
Which directory contains which, and how following the chain from the root leads to data.dat

Read the diagram from the top. The root contains home, home contains eck, eck contains java, java contains examples, and examples contains data.dat. Following that chain of directories, one step at a time, is exactly what a full path name asks the system to do.

Absolute Path Names

An absolute path name starts from the top of the file system and identifies a file uniquely. On a UNIX system, including Linux and macOS, the example /home/eck/java/examples/data.dat walks down from the root through home, eck, java, and examples before naming data.dat. The leading slash marks the root, so the path is anchored there no matter what the current directory happens to be.

On a Windows computer, the same location is written C:\eck\java\examples\data.dat. It begins with the drive letter C: and then lists the directories in the same order. Here the path is anchored to the drive rather than to a single slash-marked root.

Relative Path Names

A relative path name starts from the current directory instead of the top of the file system. Consider examples/data.dat under UNIX. Read it as: from the current directory, step into the subdirectory named examples, and then find the file data.dat there. The Windows equivalent is examples\data.dat, which uses the backslash in place of the slash.

Edge case

A relative path only means something relative to the directory it is read from. Written as examples/data.dat, it points to one file when the current directory is /home/eck/java, and to a different file, or to nothing, when the current directory is somewhere else. The same relative text can therefore resolve to different locations.

The Double Dot Component

The double-dot component introduces a movement upward. In general, .. means go up one directory. In UNIX, ../examples/data.dat asks the system to move to the directory that contains the current directory, then enter a directory named examples inside it, and look there for data.dat. Windows uses the same idea with backslashes: ..\examples\data.dat.

/home/eck/javacurrent directoryMove up one levelhandles the .. componentEnter examplesLook for data.dat/home/eck/examples/data.dat..examples/data.datfound
When a path contains .., which directory do you move to, and how does the path resolve step by step?

The diagram traces the steps. Starting from /home/eck/java, the .. moves up to /home/eck, the examples component enters the directory named examples inside that one, and data.dat is found there. Each component of the path is applied in order, from left to right, so the result depends on where the walk begins.

Worked Path Resolution

Worked example

Resolving a relative path with ..

The current directory is /home/eck/java. Resolve the relative path ../examples/data.dat to an absolute path.

  1. 1
    Identify the starting point

    A relative path is anchored to the current directory, which is /home/eck/java.

  2. 2
    Apply the first component

    The component .. means go up one directory, from /home/eck/java to /home/eck.

  3. 3
    Apply the next component

    The component examples means enter the directory named examples, giving /home/eck/examples.

  4. 4
    Apply the file name

    The final component data.dat names the file inside that directory.

Result

/home/eck/examples/data.dat

UNIX and Windows Path Syntax

The two families differ in their opening piece as well as in their separator. UNIX begins at a single root marked by a slash, while Windows begins at a drive letter followed by a backslash. The strip below breaks the UNIX path into its components so you can see what each piece contributes.

/root marker and separatorhomefirst directory under rooteckdirectoryjavadirectoryexamplesdirectorydata.datfile name
What each token in a UNIX-style absolute path contributes to the final location
FeatureUNIX (Linux, macOS)Windows
Opening anchorA single root marked by a slashA drive letter such as C: followed by a backslash
SeparatorForward slash /Backslash \
Absolute example/home/eck/java/examples/data.datC:\eck\java\examples\data.dat
Relative exampleexamples/data.datexamples\data.dat
Move up one directory../examples/data.dat..\examples\data.dat

The same locations written in each syntax

Mistakes Learners Make with Path Names

  • Common mistake

    Assuming a bare file name searches the whole disk

    What it looks like

    Opening data.dat when the file is actually in /home/eck/java/examples

    Why it fails

    A bare name carries no directory information, so the system assumes the file is in the current directory.

    Do this instead

    Give a relative or absolute path that names the directory, such as examples/data.dat or the full absolute path.

  • Common mistake

    Treating a relative path as if it were absolute

    What it looks like

    Reading examples/data.dat as the same file no matter where the program runs

    Why it fails

    A relative path only means something relative to the directory it is read from, so the same text can point to different files.

    Do this instead

    Check the current directory first, or switch to an absolute path when the location must be reliable.

  • Common mistake

    Reading .. as the end of the path

    What it looks like

    Thinking ../examples/data.dat refers to a file named examples inside the parent directory's data

    Why it fails

    Within a path, .. means go up one directory, and the remaining components are still applied in order.

    Do this instead

    Move up one level first, then enter examples, then look for data.dat.

  • Common mistake

    Mixing separators between the two systems

    What it looks like

    Writing examples\data.dat on a UNIX system

    Why it fails

    UNIX uses the forward slash, while Windows uses the backslash, so each style has its own separator.

    Do this instead

    Use / for UNIX-style paths and \ for Windows-style paths.

Choosing Between Absolute and Relative Paths

The practical trade-off is clear once both kinds of path are compared. An absolute path is reliable wherever it is used, but it is long and breaks if the directory layout changes. A relative path is shorter and moves with the current directory, which is useful when a program works inside one project, but it only means something relative to the directory it is read from.

Practice Resolving a Path

MEDIUM

The current directory is /home/eck/java. Write the absolute UNIX path that the relative path ../examples/data.dat refers to. Then say what the same relative text would mean if the current directory were /home/eck/java/examples instead.

Hints
  • Start from the current directory and apply each component from left to right.
  • The first component .. moves up one level from /home/eck/java.
  • If the current directory is /home/eck/java/examples, then .. moves up to /home/eck/java.

Key Points to Remember

  1. 1
    A path name combines a file's name with the directory where the file is stored, which is needed whenever the file is not in the current directory.
  2. 2
    An absolute path starts from the top of the file system and identifies a file uniquely, while a relative path starts from the current directory.
  3. 3
    Within a path, .. means go up one directory, and each component is applied in order from left to right.
  4. 4
    UNIX paths begin at a single slash-marked root and use forward slashes, while Windows paths begin at a drive letter and use backslashes.

Key Takeaways

  • A bare file name is interpreted relative to the current directory, so a file elsewhere needs a path name that describes its location.
  • An absolute path name is anchored to the top of the file system and identifies a file uniquely, while a relative path name is anchored to the current directory.
  • The .. component means go up one directory, and it is applied in order along with the other components of the path.
  • UNIX paths use a single root and forward slashes; Windows paths use a drive letter and backslashes.