Sharing Files in Linux and Understanding Pathnames

Going Dotty in the Linux Filesystem

February 25, 2009

In Navigating the Filesystem I talked about how pathnames work, the difference between /home and home, and using ls to see the contents of a directory. But there are a few more useful tricks you ought to know about finding your way around in the filesystem on your disk.

Getting dotty

First, an admission: when you type ls, the system is fibbing to you. There are a lot of files it's not telling you about -- so-called "hidden files". To see them, type: ls -a (a in this case stands for "all").

You'll see a lot of files starting with a period, like .bashrc, .mozilla and so forth. Most of those are configuration files or directories used by programs you run. But at the beginning, you'll also see two curious entries: . and ..

These two names act like directories -- you can ls them or cd into them -- but they take you somewhere relative to the directory you're in.

.. is the most useful: it takes you up one level. If you're in your home directory, /home/you, and you type

cd ..

you'll end up in /home (type a pwd to make sure). Another cd .. and you'll be in /, the root directory.

This is particularly useful since you can string them into longer pathnames. If you're in /usr/bin and you type cd ../lib, you'll end up in /usr/lib: from /usr/bin, .. means one level up, or /usr. It may not seem that useful just to get from /usr/bin to /usr/lib, but just suppose you were in /usr/lib/gimp/2.0/plug-ins and you wanted to get to /usr/lib/gimp/2.0/scripts. Rather than typing that whole thing out, just type cd ../scripts and you're there.

What about the single dot? . means the current directory. So cd . doesn't do anything; it just leaves you in the same place you already were.

What good is that? Sounds pretty useless, huh? But it actually does have uses. For one thing, you can use it for programs like find that insist on being told where to start. For another, you can use it when running programs. If you've ever built any programs from source, you might have seen instructions to type "./configure". That means "run the program called configure that's in the current directory, even if there's another program with that name installed somewhere else."

Even if you don't use the . a lot, it's good to know what it means; you'll see it when people talk about Linux commands.