April 22, 2019

Navigating the Linux Filesystem

Upside-down Trees

  • February 11, 2009
  • By Akkana Peck
Okay, you've been reading the articles on Linux Planet on some of the nifty tricks you can do in the shell, and you're sold. But to get anything done, you have to know how to find your way around. There are no file icons to click on -- how do you find your files?

You've probably read that files on a Linux disk are structured like a tree. Well -- at least that's true if you live in backwards-land where trees grow downward. Give computer nerds some leeway -- a lot of them don't get out much to see real live trees, so maybe they weren't clear on the fact that real trees usually start at the bottom and grow upward.

A Linux file system, drawn out like one of those upside-down trees, looks a little like Figure 1. The "leaves" of the tree, at or near the bottom, are your individual files, like .bashrc and fido.jpg.

Every file lives inside a directory. Directories can contain files, other directories, or any combination of the two. If you're coming from Windows or Mac, "directory" means the same as "folder". In Figure 1, directories have boxes around them; files don't.

The root directory

At the very top of the hierarchy is a special directory called the root directory, or / (a single forward slash). Remember how we're talking about an upside-down tree? The root is at the top.

Don't confuse the "root" directory with "root" meaning the super user. Two meanings for the same word.

You can see what's in the root directory using the ls command (short for "list" since it lists files and directories):

ls /
You'll see a lot of directories with names like boot, dev, home, usr, and etc (yes, that's really the name of a directory). Most of these are system directories: they contain installed programs, or configuration files Linux needs to do its job. But for now, let's concentrate on the directory named home: that's where your user files are.

You can refer to any directory or file on your system by what's called its "full path", starting with / for the root directory. So if you want to find out what's in that directory called home, you can refer to it this way:

$ ls /home
The slash before the directory name is important: it says that you want the directory called home that's right under the root directory. If you omitted the slash and just said ls home, you'd be asking for a directory called home starting from wherever you happen to be at the moment.

Your working directory

Wait, what does that mean -- "wherever you happen to be"?

On Linux, you always have a "current working directory". When you log in, that's usually your home directory. If you're in your home directory and try to ls home, without a leading slash, you'll probably see:

$ ls home
ls: cannot access home: No such file or directory

The pwd command ("print working directory") will tell you what your current directory is. To change your working directory, use the cd ("change directory") command:

$ cd /
will change your working directory to the root directory. Then if you ask for your working directory, you'll see this:
$ pwd

Most Popular LinuxPlanet Stories