Single and double dots in filesystem paths

Objectives
  • Explain what the single and double dots represent in filesystem paths.
  • Correctly use a single or double dot in file paths when appropriate.
Prerequisites

None.

Introduction

The single dot and double dot characters have a special meaning when used as part of file and directory paths.

Single dot (.)

A single dot, as part of a file or directory path, represents the current directory.

The following two relative paths are identical:

  • path/to/file.pdf
  • ./path/to/file.pdf
     

You can also use the single dot all by itself. 

Here's how you would copy a file from somewhere else to the current location, using the cp (copy) command:

sarah@laptop ~/backups/logs$ cp /var/log/syslog .

→ The above command copies the file located at /var/log/syslog to your current directory, ~/backups/logs


Of course you don't have to use the single dot. You can also make the target destination explicit:

sarah@laptop ~/backups/logs$ cp /var/log/syslog ~/backups/logs

→ The above command copies the file located at /var/log/syslog to the directory ~/backups/logs.

Double dot (..)

A double dot, as part of a file or directory path, represents one level up in the path. In other words: the parent directory.

Example: moving up one directory

sarah@laptop ~/Documents/research$ cd ..
sarah@laptop ~/Documents$

→ The above command moves the user from the ~/Documents/research directory to the ~/Documents directory.

Example: viewing a file in a different directory

sarah@laptop ~/Documents/research$ cat ../drafts/story1.txt
Once upon a time there was a boy.
He grew up to be a powerful king.
But the people didn't like him.
So the king was sad.
The end.
sarah@laptop ~/Documents/research$

→ The above command uses the cat command to view the story1.txt file. To find it, it looks one directory up and then looks in the drafts subdirectory.

The command does in one single step what you would otherwise need to do in multiple steps:

sarah@laptop ~/Documents/research$ cd..
sarah@laptop ~/Documents$ cd drafts
sarah@laptop ~/Documents/drafts$ cat story.txt
Once upon a time there was a boy.
He grew up to be a powerful king.
But the people didn't like him.
So the king was sad.
The end.
sarah@laptop ~/Documents/drafts$

Demo

Summary

  • A single dot (.) in filesystem paths represents the current directory.
  • A double dot (..) in filesystem paths represents the directory one level up.