File Tree and Pathnames

A file system stores your files in a hierarchical structure, like a tree. In nature, trees begin with a root and grow to leaves. In the computing world, this is the same (drawn growing down, usually).

In Unix-like operating systems, the “top level” directory (aka folder) in which everything is called the root of the filesystem tree. Within the root directory, there are usually other directories. Directories can then contain several other directories or files.

Note about the upside-down trees: Our language reflects this thinking: when we move a file, we move it “down” into a subdirectory, or “up” into a directory “above” the current one.

Tree Vocabulary

File tree
  • A tree consists of nodes and edges. In this example, A through M are nodes. The lines between nodes are edges, like the edge between (A,C).
  • In the image, the node labeled “A” is the root of the system.
  • Nodes may have child nodes (a node connected below another node). Above, nodes B, C and D are all child nodes of A.
  • Parent nodes: Nodes that have child nodes. A is the parent node of B, C, and D. 
  • Leaf nodes are nodes which have no children. C is a leaf node.

Pathnames

A tree structure for file system1

A pathname is the address of a file or folder within the file tree. Each directory and file in the file system has a specific location, specified by a pathname, on the file tree. Pathnames allow you to specify where you want to put files and help you find your location on the file tree. We refer to the root using a slash character, /, on its own. In this example, the root contains several other directories: bindataUserstmp.

On UNIX, every user normally has a home directory, serving as a personal storage locker for files and directories. Let’s assume that there is someone with the username nelle inside Users. Then nelle’s home directory is /Users/nelle. See how we use the / character to show the path, or edges, that we follow for a given pathname. If you start with the leading forward slash for writing a path, you are indicating that the path starts at the root folder /.

On UNIX, try running echo $HOME to see the absolute path to your own home directory.

Note: When / it appears at the front of a file or directory name, it refers to the root directory. When it appears inside a path, it’s just a separator.

There are both absolute and relative pathnames.

Absolute Pathnames

Each file and directory in the filesystem has an absolute path to locate it, starting with the root folder / and listing each directory on the way to the file. A slash / is placed between each level in the path name to separate each directory name from the previous level. Each file or directory has a unique path name.

Windows, rather than using /, uses names like C:. On a Windows operating system, other drives (e.g., CD-ROMs, network drives) would get their own “root” directory with names like D: and E: (with the main hard drive usually having the name C:). On Unix-based operating systems, there is only ever one filesystem hierarchy, and the top level is always /. Special devices like CD-ROM drives and network drives are mounted somewhere in the filesystem. It may be that the directory /media remains empty, for example, but when a CD-ROM is inserted, a new directory may appear inside that directory, perhaps with the absolute path /media/cdrom0, and the files on the CD-ROM will appear in that location.

~/Tilde-slash is a shortcut to your home directory. On Eos, ~ replaces the /mnt/ncsudrive/u/unityid
/Slash refers to the root directory.
Quick Reference

Relative Pathnames

Rather than specifying the full, absolute path to locate a file or directory, we can use a relative path. A relative path locates a file or directory relative to the present working directory.  For example, in the file tree below, if we are in /users/raj/work, then the relative path to report is ../report.

Typical Hierarchy in a Linux system2

Note that we have directories etc, bin, dev, users, and under users we have more directories for raj, tom, and lester. Some of these directories are parents to more directories or files. Files are leaf nodes.

UNIX/Linux systems recognize some special character combinations known as relative pathnames. These relative pathnames can be used as shortcuts to save time while accessing files and directories:

../ Dot-dot-slash refers to the directory above your current working directory (the parent directory). This can be used in conjunction with other shortcuts.
./ Dot-slash refers to the current working directory. Instead of typing the long (absolute) pathname, you can use dot. This is useful when working with files in your current working directory.

You can think of an absolute pathname like a physical, named address, such as 2610 Cates Ave, Raleigh, NC 27606 (Talley Student Union). A relative path for the same location would be something like: if I’m at Withers Hall, then cross the railroad tracks and turn right to get to Talley. The absolute path would tell me where to go from any other location, but the relative path will only work if I’m at Withers.

Linux File Names

Should not include spaces (follows the special characters rule). To separate words, it is advised to use an underscore (_), like_this

Up to 256 characters long

No special characters: | { } [ ] ( ) ! & * ? \ ^ % @ # ” ‘ `<>

FYI: If you must use a special character, precede it with a backslash (‘\’). For example, to name a file “some!file”, you may have to name it “some\!file” for it to function correctly

Cannot include the forward slash (‘/’)

References

  1. Image and following text referenced from: SW Carpentry ↩︎
  2. Reference Pressbooks ↩︎