What are "." and ".." in a directory?

Based on the question: How to make using command prompt less painful, what are the . and .. entries in the most voted answer? I see it when I do a dir command but it isn't visible to the user in the form of a file.

In case you dont know what I mean here's an example:

.
..
Su.exe
Sup.txt
SuperUser.COM

Solution 1:

The . is the current directory, while .. signifies the parent directory. It makes things quicker at the command line as well so you don't need to type out full paths.

example:

go up 2 directories:

cd ..\..\

or on a UNIX based system, to run executable binaries in the current directory:

./program

A lot of UNIX scripts will also utilize . to represent the current directory, in order to scan for files for example (Perl):

#!/usr/bin/perl

opendir ( DIR, "." ) || die "Error opening current directory\n";
while( ($f = readdir(DIR))){
     print("$f\n");
}
closedir(DIR);

It is much more portable if you wish to move the script around to different directories or systems since a directory name is not hard-coded.

Solution 2:

The .. is used to navigate up the hierarchy of the file system. It's useful when you don't want to type a long path, or when writing a script/program that doesn't know where exactly it will be installed but it knows that ../media/ should hold all the images/videos/icons etc.

The single dot . is useful in linux where you want to run an executable in the current directory so you type ./a.out because the command shell by default doesn't search the current directory for executable files (for security reasons).

The single dot . is also used if you want to pass the current directory as an argument to a command.