The reason behind ls -a
showing .
and ..
has nothing to do with utility and everything to do with Unix (and Linux) filesystems.
Everything is a file.
Directories are really files that contain lists of files. "Inside" any given directory, because that is the perspective of the user, if not exactly the perspective of the filesystem, there are two special files.
- The first, called
.
, is a reference to the current working directory. This is used by people almost every day, when they do things like running a command in the current directory via ./scriptname
.
- The second, called
..
is a reference to the CWDs parent directory. Like .
, ..
is used every day by everyone who desires to change directories to the parent directory via cd ..
.
The system call getdents()
, which is the workhorse on the system side of ls
, simply enumerates all files in a directory. (remember: everything is a file. devices, directories, sockets, etc. so they all show up)
From the utility side of things, the author(s) of the ls
command have provided a number of options, and combinations of options, that allow us to filter out certain pieces or types of information, for example @Gilles commented above that ls -A
will show all dotfile, with the exceptions of .
and ..
. All of these options could be considered post-processing options. They affect what is displayed by ls
, not what is found by ls
.
There's really no reason to explicitly list .
and ..
, by default, since we all know they are there, what they are for, and when to use them.
It can definitely be helpful to be able to have some option to see them though, when you're troubleshooting strange permissions problems.
Check out the manpage for getdents()
¹. It explains a bit more about how it works, and includes the source code for a sample program that you can compile with gcc -Wall
, which lists all the files in a directory.
—
1. At least, the Ubuntu manpage for getdents()
seems to have the sample code.
cd .
can fail, e.g. if you don't have permission to change to the directory now (this can happen if the process changed to another user after changing its working directory, or if the permissions on the directory changed after the process changed its directory). – Gilles 'SO- stop being evil' Oct 02 '11 at 00:34mkdir /tmp/foobar; cd /tmp/foobar; rmdir /tmp/foobar; cd .
yields an error:...: No such file or directory
– Arcege Oct 02 '11 at 00:40