Recent Changes - Search:
 Welcome to the Cisco Academy for Vision Impaired Linux Wiki.

PmWiki

edit SideBar

UnderstandingLinuxPathStructure

A path tells Linux where a file is located. Paths work similarly to paths in DOS/Windows, but not exactly in the same way, so watch out for surprises. Just as a street address has a house number, street, city and province to indicate where a person live, a path is an exact specification of where an individual file lives.

In paths, alphanumeric characters (letters and numbers) give the file's names. Since directories are also files, their names are also composed of alphanumeric characters. Some punctuation marks -- the underline is an example -- can also be used in filenames, but it is wise to stay away from punctuation when naming files.

In paths, the slash character separates components of a path.

The period character indicates the current directory, or depending on context, it can also be part of the alphanumeric string that names a file. Two periods indicate the parent directory. Though it is possible to put two consecutive periods in a filename, it's not a good idea!

The root of the directory tree is a slash. If you see a / character at the start of a pathname, you will know that the path is beginning at the root of the tree. However a slash in the middle of a pathname simply indicates that what comes before it is a directory and what comes after is either another directory or a filename. A slash at the end of a pathname indicates that the last item in this path is a directory.

In DOS and Windows you have drive letters. Linux has no such concept. The colon is used for another purpose. In DOS and Windows, you have filename extensions. In Linux, there is no such concept. Linux will retain the dot and the extension but it won't see that as anything special in a filename. In Dos and Windows, you use the backslash to separate items in the path. In Linux, you use a slash, also refered to as a forward slash.

Branches extend from the root. They are directories. The terms directory and subdirectory are synonymous, though people often refer to a sub-directory to indicate that it is below a parent directory. The parent directory is any directory above your current directory in the tree hierarchy.

So a path like documents/reports/today/ points to a file in the documents directory, which is in the reports directory under documents and is named today. We have no idea from this "relative" pathname where the documents directory is located.

However, a pathname like /letters/work/myletter/ points to a file called myletter, which is in the work directory which is in the letters directory which is located at the system root. It is called an "absolute" path because it points absolutely to where a file lives.

The advantage of an absolute path is its lack of ambiguity, and wherever you are located on a system, the absolute path will point to the exact file intended. The advantage of a relative path is the flexibility it gives a user. From your current directory, you can back up some files in another storage area of the system without detailing absolutely where they are located. A relative path speeds typing too, but it is easier to make a mistake. A relative path also works well when writing scripts because they need not be specific to a particular system setup.

For example, if you were to back up every day's security camera video to a directory called yesterday in your parent directory you could simply specify the path for yesterday as ../yesterday. You could set up this backup to automatically take place for a variety of cameras in a number of different buildings.

Because pathnames can also contain indicators for the current directory, a single dot, or parent directory, two consecutive periods, you can express some complex destinations. For example reports/../yesterday/ traverses from reports, up through its parent directory and down again to yesterday, which may be a file or directory. Linux doesn't care, since files and directories are technically the same. And in this example relative path, we didn't need to know the name of the parent directory.

For an even more interesting example, specifying ../.. describes the "grandparent", the directory above the parent directory. It is quicker often to type cd ../../.. to move up the tree by three levels than key in all the exact directory names.

Using the . and .. shortcuts also enable you to request that commands recurse both ways through the directory tree. Without these shortcuts you could walk down (descend) in to directories, under directories, but you could not issue a generic command to work your way back up. You can think of the .. as a breadcrumb, marking the directory you came from.

Relative paths are very important in web design, because if items are moved and they were refered to by absolute paths in the content management system, the site would break.

You also use . to refer to your current directory when you wish to be generic. ./draft is a draft in your current directory. ../draft is a draft in the parent directory. ../yesterday/draft is a draft in a directory called yesterday that is beneath your parent directory.

It is also common to begin with ./ when attempting to run a script in your current directory. This is necessary, because unlike Windows, , the current directory isn't part of your search path and a script needs to know from which directory it is running. Suppose you attempt to execute a script named install.sh and you try to run it by typing install.sh, it will not run, because a script needs to know which directory it is running from. So instead, you'd need to type ./install.sh to get it to work.

In DOS and Windows, a pathname must contain a drive letter or double

 backslashes (to indicate a network resource) to be absolute. Because Linux has no drive letters, a pathname merely needs to start at the root to be absolute. In Linux, if data is physically elsewhere, such as on a network, or another drive, that drive will be "mounted" under the root in a directory so that it can fall below the root in the hierarchy. So /dev/cd1/ might be the absolute path for the second DVD drive, and /mnt/Harold/USB/ might be the absolute path for Harold's USB flash drive.

For another example, if you record television at home, using Linux. If you record Star Trek episodes, and someone else on the local network wants to access them with Linux in their home office, You need to mount the Star Trek directory from the remote system (yes your home is remote!) so that it becomes a branch off their office system's tree.

Windows tends to treat the desktop as the root, but that's just to avoid confusing users. So, in Linux, be careful to avoid confusing the root directory with your home directory. Because Linux is multi-user, multiple users each have their very own home directories, an depending on how administrators set things up, home directories could potentially appear anywhere on a system. Typically, all users home directories are under /home, but that's not always the case. On a college's system, /staff hold all the faculty home directories, and /student holdshome directories for each of the students. In reality, /staff and /student are symbolic links to external drives, enabling our administrator to separately and conveniently back up everyone's data.

A user can refer to his own home directory with the tilde ~ character. So a path like ~/reports refers to that users reports whereas the pathname /reports refers to some reports directory directly off the system root. And a path like ../reports refers to reports that are not in the current directory but off the parent directory.

~NAME where NAME is a valid username expands to the path for that particular user. ~Tom might print /home/Tom. If Tom were staff at our college, it would print /staff/Tom but if Tom were a student at our college it would print as /student/Tom.

Note the difference between ~/miller and ~miller. If there is a slash before Miller, then Linux looks for a Miller directory or file called "miller" in your own user directory. If, on the other hand you omit the slash, instead typing ~miller, then it expands to the user Miller's home directory. Note also that the terms home directory and user directory are synonymous.

Because the tree is actually up-side-down, with the root at the top, and people moving "up" the tree are traversing closer to the root, some users find it less complex to think in terms of parents and siblings. Continuing with this analogy, the parent of everyone is the directory named slash. Under the slash directory, all directories and files are children of slash. Children beget other children, and the cycle continues indefinitely, or until the system runs out of space. If you wish therefore to refer to your sibling, you'd start your path with .. to return to your parent directory.

A path like ../reports finds reports off of your parent directory. Again, this is handy because it is not necessary to know or specify the parent directory. A path like ./reports finds reports off your current directory, again not naming your current directory at all. A path like ~/reports, finds reports off your user directory. A path like /reports finds reports off the root directory. A path like ~henry/reports locates reports under Henry's user directory.

The system uses a file called /etc/passwd to look up user names and their corresponding home directories. All users can read this file, but they do not have permission to change it. Take a look at the passwd manpage

 man 5 passwd

for details.

Edit - History - Print - Recent Changes - Search
Page last modified on May 25, 2020, at 10:29 PM