The Unix File System
The purpose of this lesson is to introduce you to how files and directories are handled in Unix.
Unix keeps track of files and directories of files using a file system. When you log in to your Unix account, you are placed in your "home" directory. Your home directory thus becomes your "present working directory" when you log in. In your home directory, you can create files and subdirectories. And in the subdirectories you create, you can create more subdirectories.
The commands that you issue at the Unix prompt relate to the files and folders and resources available from your present working directory. You certainly use and can refer to resources outside of your current working directory. To understand how this works, you need to know how the Unix file system is structured.
The filesystem tree
You can visualize the Unix file system as an upside down tree. At the very top of the tree is the root directory, named "/". This special directory is maintained by the Unix system administrator. Under the root directory, subdirectories organize the files and subdirectories on the system. The names of these subdirectories might be any name at all. Here is a tree diagram of a typical Unix system.
In the filesystem in this diagram, the root directory / has four subdirectories: bin, tmp, users, and src. In the users subdirectory, there are two subdirectories john and mary. Let's say I am the user john and my home directory is the one labeled john in the diagram. When I log in, I can issue the pwd command:
$ pwd /users/john $
Now, if I want to change my present working directory to the directory above my home directory, users, I use the cd command followed by ".." (two periods right together, no spaces). This puts me in the "users" subdirectory.
$ pwd /users/john $ cd .. $ pwd /users $
I can still go "up" further to the root directory (if the system administrator allows it -- it is possible to set the permissions so that I can't "visit" any directory I want):
$ pwd /users $ cd .. $ pwd / $
I now can't go any higher in the tree. Now, I could issue the change directory commands like this to go back to my home directory:
$ pwd / $ cd users $ pwd /users $ cd john $ pwd /users/john/ $
But there is a shortcut to going back home. Just enter the cd command without anything after it.
$ pwd /users/john/ $ cd .. $ pwd /users $ cd .. $ pwd / $ cd $ pwd /users/john $
Now, if I am in my home directory, and I want to quickly go to the root directory, I can string together the two periods ".." separated by slashes "/" to go up two levels to the root directory. Like this:
$ pwd /users/john/ $ cd ../../ $ pwd / $
I can string together the two periods "..", the slash marks "/" that separate the directory names, and directory names themselves, to quickly navigate the directory tree. Here is how I can go from my home directory to the tmp directory and back home again:
$ pwd /users/john/ $ cd ../../tmp $ pwd /tmp $ cd ../users/john $ pwd /users/john/ $
If the user mary allows it (if she has set her permissions so that I can "visit" her directory), I can go over there like this:
$ pwd /users/john $ cd ../mary $ pwd /users/mary $
Creating a directory
Now, this is all fine and good, but what if I want to organize my files in some subdirectory of my own. I can create a subdirectory in my home directory using the mkdir command.
$ pwd /users/john $ mkdir portfolio $ cd portfolio $ pwd /users/john/portfolio $
In this case, I've altered the tree diagram of the file system to be like this:
Relative and Absolute Pathnames
The use of the ".." notation allows me to navigate the directory tree structure. The ".." symbol means "parent directory." Names with ".." in them are relative names because their meaning depends on where they are issued (the present working directory). I can string together several ".." symbols, separated by the / symbol and other directory names, to change directories. For example, if I am in portfolio and want to change to mary, I can do this with a cd command followed by the relative pathname between portfolio and mary like this (first using pwd to show where I am):
$ pwd /users/john/portfolio $ cd ../../mary $ pwd /users/mary $
Directory or file references starting with .. are relative pathnames.
Directory or file references starting with a directory name are also relative pathnames. For example, if I am in the users directory, the directory reference john/portfolio is a relative pathname:
$ pwd /users $ cd john/portfolio $ pwd /users/john/portfolio $
If I string together the unique name of all the intervening subdirectories in the file system to a particular subdirectory, I've created the absolute pathname for that directory. The absolute pathname allows me to switch to a directory no matter what my present working directory is. Absolute pathnames always start with a "/". I can navigate the file system by using absolute pathnames. So I could do something like this:
$ pwd /users/john $ cd /users/mary $ pwd /users/mary $ cd /tmp $ pwd /tmp $ cd /users/john/portfolio $ pwd /users/john/portfolio $
Every directory or file on the file system has a unique absolute pathname. Although john may create a file called "test.txt" in his home directory and mary may create a file called test.txt in her home directory, the absolute pathnames of these files are different. John's is called /users/john/test.txt, and Mary's is /users/mary/test.txt.
Exercise: Write out your absolute pathnames
Explore your file system and write out the absolute pathname to your home and some other directories on your system that you can find.
Then write out the relative pathnames from your home directory to these same directories.