There’s a Lot in the Dot: Filesystem Permissions and Pathnames (Part 1)

Beginning with a brief tour of the filesystem, we take you deep into the dot (.) of your directory listing so you can get the most from your CLI.

Linux filesystem permissions and pathnames can cause lots of confusion. Users type long pathnames when a short one would do as well, and set wide-open access permissions when a more-secure setting would be easy if the concepts were more clear.

This article and the next take a unique approach: understanding the dot (.) that you’ve probably seen in directory listings.

Don’t need the introductory stuff? Skip to part two.

What’s “in” a directory

Linux filesystems hold files and other objects (e.g. symbolic links, devices like disks, and directories) grouped into directories (known as folders on other operating systems). It’s important to understand that a directory doesn’t actually contain the objects; it holds references to those objects. The references are called hard links.

To access something in the filesystem, a program looks into a directory using a system call. From there, it finds the link to the actual object on the disk (or whatever device the object is stored on). Then it locates the object through its index number, as we’ll see soon.

Each directory has at least two names. One of those names is a dot (.). Every directory on the filesystem has a name (a hard link) of dot.

Most GUI filesystem browsers don’t show these dot entries. You can see them with the ls utility and its -a (“all”) option.

By default, if you don’t specify something to list, ls lists its current directory. You can also specify a directory, like ls /bin or ls .. (to list the dot-dot entry). Adding the -i or --inode option shows the index numbers. This directory has a parent and “grandparent” directory, as we’ll see later. And pwd shows the directory’s absolute pathname:

$ # Listing I (child directory)
$ ls
bin  foo
$ ls -a
.  ..  bin  foo
$ ls -ai
5423 .   58 ..   5425 bin   5424 foo
$ ls -ai .
5423 .   58 ..   5425 bin   5424 foo
$ pwd
/home/jpeek

Notice that ls -a and ls -a . give the same result. Both list the directory named dot — the current directory. If your ls shows slashes after the names of some entries, it’s been aliased to use the option -F. For example:

$ ls -aF
./  ../  bin/  foo

The listings show the directory’s four entries (hard links): ., .., bin and foo. The first three are links to directories, and the fourth is a plain file. Each entry has its index number; for instance, this directory’s index number (listed before its dot entry) is 5423.

If you list various directories around the filesystem, you’ll see that every directory has an entry named .. They also have .. (two dots), which is a link to that directory’s parent — in other words, the directory that contains the directory. Let’s list the parent directory in two ways: by telling ls to list .., then by changing the shell’s current directory to .. and listing the current directory:

$ # Listing II (parent directory)
$ ls -ai ..
 58 .   2 ..   5423 jpeek
$ cd ..
$ ls -ai
 58 .   2 ..   5423 jpeek
$ pwd
/home

Please compare Listing II with Listing I. What do they have in common?

So, the same directory can be referred to in more than one way:

To find the parent of any directory, you can always open .. (dot dot). The filesystem’s tree structure, tied together with .. and . entries, is built automatically each time you create a new directory.

One directory in the filesystem is special because it’s its own parent. That’s the root directory. Let’s list it now (for clarity, we’ll omit a lot of the entries):

$ # Listing III (root directory)
$ ls -ai ..
 2 .   2 ..   58 home
$ cd ..
$ ls -ai
 2 .   2 ..   58 home
$ pwd
/

So, if you’re currently in the root directory and you type cd .., you’ll stay where you are.

I-number confusion

Each filesystem has its own set of index numbers. So, as you’re looking around with ls -ai, remember that there may be more than one link with, say, number 5423. You’ll also find inconsistencies where filesystems are mounted. For example, at the root directory of a system where /home is mounted on root:

$ pwd
/
$ ls -ia
 2 .   2 ..   24577 home
$ ls -ia home
 123 .   123 ..

Of course, listing a bunch of directory entries doesn’t do you much good — except to see what’s in the filesystem. If you were searching for filesystem corruption, you could check these things yourself. (That’s one of the things done by fsck, the filesystem consistency checking program.) What’s more useful is to use this knowledge so you can specify filesystem objects that you want to access. To do that, you use pathnames and, sometimes, the current directory. The next column covers pathnames in detail.

Let’s wrap up by looking at a diagram of a simplified filesystem, Figure One. Click for a larger view. (Opening in a separate window or tab may be easier.)

Figure 1: Simplified filesystem diagram
Figure 1: Simplified filesystem diagram

To make things simpler, the diagram omits details such as the system of nodes and indirection that handles metadata (like file access permissions and link counts) and the pointers to the actual disk blocks. This diagram is to help you follow where the links point.

For instance, in any directory, opening . (dot) opens the directory itself; that’s what’s meant by the arrow starting at . pointing to the directory. As another example, you can see that the entry named jpeek from directory 58 points to directory 5423, as does the entry named . in directory 5423 and the .. entry in directory 5425. All three of those entries refer to the same directory.

If this seems even a bit fuzzy, please look through the diagram and open a terminal window to try things on your own filesystem. Then you’ll be ready for part two on pathnames and permissions.

Jerry Peek is a freelance writer and instructor who has used Unix and Linux for more than 25 years. He's happy to hear from readers; see https://www.jpeek.com/contact.html.

[Read previous article] [Read next article]
[Read Jerry’s other Linux Magazine articles]