Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

After reading the question When you type “ls -a”, what is the significance of “.” and “..”?, I have a similar one.

Why to display .? I mean, is there any directory where cd . would not work? How is it helpful to a user to see this line every time?

share|improve this question
    
No. That command always works. It's also interesting to note that cd .. will also always work - even if you're using it from / –  Dason Oct 2 '11 at 0:18
3  
@Dason Technically 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 Oct 2 '11 at 0:34
4  
Also if the directory was deleted from disk; e.g. mkdir /tmp/foobar; cd /tmp/foobar; rmdir /tmp/foobar; cd . yields an error: ...: No such file or directory –  Arcege Oct 2 '11 at 0:40
add comment

2 Answers

up vote 15 down vote accepted

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.

share|improve this answer
add comment

ls doesn't normally display . and ... By default, ls hides all files whose name begins with a . (dot files). ls -a displays them because you asked to show everything. Some versions of ls have a slightly different option, ls -A, to show all dot files but not . or ...

Showing . can be useful if you're listing more than the names; for example ls -la reports the permissions and ownership of the current directory and its parent along those of the files in the directory.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.