86

The ls -ai command shows that . and .. have their inodes the same as the current directory and parent directory, respectively.

What exactly are . and ..?

Are they real files or even hard links? But as I have known, it's not allowed to create a hard link to a directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cubarco
  • 981
  • 1
  • 7
  • 4
  • 1
    Where is triple dot!? By accident is just used `mv` with `...` and the relevant directory is gone! – user2023370 Oct 30 '19 at 23:51
  • 2
    Found it. It was in the same directory, named `...` and therefore hidden, as it starts with a dot :) – user2023370 Oct 30 '19 at 23:58
  • This is why a lot commands you can just pass `.` e.g. `git add .`, will stage everything in the current directory (along with its subdirectories), while `git add ..` will stage everything in the parent directory (along with its subdirectories) – mfaani Mar 30 '20 at 02:05
  • For Windows questions that get redirected here, note that "." and ".." entries (present only in non-root directories) are virtual in Windows. The kernel object manager, I/O manager and filesystem drivers do not implement them (except in the target path of relative symlinks). In a normalized path, "." and ".." components get resolved by the user-mode runtime library using rule-based path operations. If they're passed directly to kernel mode in a verbatim "\\?\" path, the filesystem handles them literally, either as reserved names (NTFS) or dysfunctional names (FAT32). – Eryk Sun Oct 11 '20 at 18:29
  • Perhaps see also [Difference between `./` and `~/`](https://stackoverflow.com/a/55342466/874188) which contains a more general exposition of how file systems are organized, as well as the differences between absolute and relative paths. – tripleee May 22 '22 at 07:22
  • 1
    @mfaani well after using git for 4 years I finally understood what the `.` was in `git add .` lol. – M.Ed Jul 01 '22 at 13:46

4 Answers4

73

. represents the directory you are in and .. represents the parent directory.

From the dot definition:

This is a short string (i.e., sequence of characters) that is added to the end of the base name (i.e., the main part of the name) of a file or directory in order to indicate the type of file or directory.

On Unix-like operating systems every directory contains, as a minimum, an object represented by a single dot and another represented by two successive dots. The former refers to the directory itself and the latter refers to its parent directory (i.e., the directory that contains it). These items are automatically created in every directory, as can be seen by using the ls command with its -a option (which instructs it to show all of its contents, including hidden items).

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
19

They are special name-inode maps which do count as hard-links (they do increase the link-count) though they aren't really hard-links, since, as you said, directories can't have hard-links. Read more here: Hard links and Unix file system nodes (inodes)

e.dan
  • 7,275
  • 1
  • 26
  • 29
8

. represents the current directory that you are using and .. represents the parent directory.

Example:

Suppose you are in the directory /etc/mysql and you wanted to move to the parent directory, i.e. /etc/. Then use cd..:

/etc/mysql> cd ..

And if you wanted to set the path of one file in the current directory bash file, use . with file name like this: ./filename

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deva
  • 1,851
  • 21
  • 22
  • What do you mean by *"in the current directory bash file"*? Can you elaborate? – Peter Mortensen Jan 02 '20 at 19:54
  • @PeterMortensen Thats mean, if you wants to set path of a file which is in current directory. – Deva Jan 03 '20 at 04:52
  • @Deva When you say "set the path" would that execute the file or would that add the absolute path of the directory that contains the file to in the PATH environment variable? – DaCruzR Jun 05 '21 at 13:11
  • @DaCruzR, Set the path is said for in bash file, if we need to access another file then time we are setting the path. Even ./filename is also used to execute the file. – Deva Jul 06 '21 at 12:37
2

They are not hard links. You can more think of it like a shorthand for this directory (.) and parent of this directory (..).

Try to remove or rename . or ... Then you understand why it is not a hard link.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nafis Ahmad
  • 2,689
  • 2
  • 26
  • 13
  • 1
    In most UNIX file systems, they _are_ hard links, however, since they are built into the file system, you cannot remove them. – yyny Jun 28 '20 at 14:58