Home > Articles > Open Source > Linux/UNIX/Open Source Operating Systems

  • Print
  • + Share This
This chapter is from the book

The Difference Between Relative and Absolute Filenames

Specifying the exact location of a file in a hierarchy to ensure that the filename is unique is known in Unix parlance as specifying its absolute filename. That is, regardless of where you are within the file system, the absolute filename always specifies a particular file. By contrast, relative filenames are not unique descriptors.

To understand, consider the files shown in Figure 3.3.

Figure 3.3

Figure 3.3 A simple hierarchy of files.

If you are currently looking at the information in the Indiana directory, Bldgs uniquely describes one file: the Bldgs file in the Indiana directory. That same name, however, refers to a different file if you are in the California or Washington directories. Similarly, the directory Personnel leaves you with three possible choices until you also specify which state you’re interested in.

As a possible scenario, imagine you’re reading through the Bldgs file for Washington and some people come into your office, interrupting your work. After a few minutes of talk, they comment about an entry in the Bldgs file in California. You turn to your Unix system and bring up the Bldgs file, and it’s the wrong file. Why? You’re still in the Washington directory.

These problems arise because of the lack of specificity of relative filenames. Relative filenames describe files that are referenced relative to an assumed position in the file system. In Figure 3.3, even Personnel/Taylor,D. isn’t unique because that can be found in both Indiana and Washington.

To avoid these problems, you can apply the technique you learned earlier, specifying all elements of the directory path from the top down. To look at the Bldgs file for California, you could simply specify /California/Bldgs. To check the Taylor,D. employee in Indiana, you’d use /Indiana/Personnel/Taylor,D., which is different, you’ll notice, from /Washington/Personnel/Taylor,D..

Learning the difference between these two notations is crucial to surviving the complexity of the hierarchical file system used with Unix. Without it, you’ll spend half your time verifying that you are where you think you are, or, worse, not moving about at all, not taking advantage of the organizational capabilities.

If you’re ever in doubt as to where you are or what file you’re working with in Unix, simply specify its absolute filename. You always can differentiate between the two by looking at the very first character: If it’s a slash, you’ve got an absolute filename (because the filename is rooted to the very top level of the file system). If you don’t have a slash as the first character, the filename’s a relative filename.

Earlier I told you that in the /home directory at the top level of Unix, I have a home directory called taylor. In absolute filename terms, I’d properly say that I have /home/taylor as a unique directory.

As you learn more about Unix, particularly about how to navigate in the file system, you’ll find that a clear understanding of the difference between a relative and absolute filename proves invaluable. The rule of thumb is that if a filename begins with /, it’s absolute.

Task 3.2: Hidden Files in Unix

One of the best aspects of living in an area for a long time, frequenting the same shops and visiting the same restaurants, is that the people who work at each place learn your name and preferences. Many Unix applications can perform the same trick, remembering your preferred style of interaction, what files you last worked with, which lines you’ve edited, and more, through preference files.

On the Macintosh, there’s a folder within each user’s home directory called Library. Within that there’s another folder called Preferences, which is a central storage place for preference files, organized by application. On my Macintosh, for example, I have about 75 different preference files in this directory, enabling me to have all my programs remember the defaults I prefer.

Unix must support many users at once, so Unix preference files can’t be stored in a central spot in the file system. Otherwise, how would the system distinguish between your preferences and those of your colleagues? To avoid this problem, all Unix applications store their preference files in your home directory.

Programs want to be able to keep their own internal preferences and status stored in your directory, but these aren’t for you to work with or alter. If you use DOS, you’re probably familiar with how Windows solves this problem: Certain files are hidden and do not show up when you use DIR, in DOS, or the File Manager to list files in a directory.

Macintosh people don’t realize it, but the Macintosh also has lots of hidden files. On the topmost level of the Macintosh file system, for example, the following files are present, albeit hidden from normal display: .DS_Store, Desktop DB, .VolumeIcon.icns, NavMac8000QSFile, and my personal favorite .symSchedScanLockxz. Displaying hidden files on the Macintosh is very difficult, as it is with Windows.

Fortunately, the Unix rule for hiding files is much easier than that for either the Mac or PC. No secret status flag reminds the system not to display the file when listing directories. Instead, the rule is simple, any filename starting with a dot (.) is called a hidden file.

If the filename or directory name begins with a dot, it won’t show up in normal listings of that directory. If the filename or directory name has any other character as the first character of the name, it lists normally.

  1. Knowing that, turn to your computer and enter the ls command to list all the files and directories in your home directory.

    % ls -F
    Archives/      Mail/          RUMORS.18Sept  mailing.lists
    InfoWorld/     News/          bin/           newlists
    LISTS          OWL/           iecc.list      src/
    %
  2. You can see that I have 12 items in my own directory, 7 directories (the directory names have a slash as the last character because of the -F, remember), and 5 files. Files have minimal rules for naming, too. Avoid slashes, spaces, and tabs, and you’ll be fine.
  3. Without an explicit direction to the contrary, Unix is going to let the hidden files remain hidden. To add the hidden files to the listing, you just need to add a -a flag to the command. Turn to your computer and try this command to see what hidden files are present in your directory. These are my results:

    % ls -aF
    ./             .gopherrc      .oldnewsrc     .sig        RUMORS.18Sep
    ../            .history*      .plan          Archives/   bin/
    .Agenda        .info          .pnewsexpert   InfoWorld/  iecc.list
    .aconfigrc     .letter        .report        LISTS       mail.lists
    .article       .login         .rm-timestamp  Mail/       newlists
    .cshrc         .mailrc        .rnlast        News/       src/
    .elm/          .newsrc        .rnsoft        OWL/
    %

    Many dot files tend to follow the format of a dot, followed by the name of the program that owns the file, with rc as the suffix. In my directory, you can see six dot files that follow this convention: .aconfigrc, .cshrc, .gopherrc, .mailrc, .newsrc, and .oldnewsrc.

Because of the particular rules of hidden files in Unix, they are often called dot files, and you can see that I have 23 dot files and directories in my directory.

Other programs create many different dot files and try to retain a consistent naming scheme. You can see that .rnlast and .rnsoft are both from the rn program, but it’s difficult to know simply from the filenames that .article, .letter, .newsrc, .oldnewsrc, and .pnewsexpert are all also referenced by the rn program. Recognizing this problem, some application authors designed their applications to create a dot directory, with all preference files neatly tucked into that one spot. The elm program does that with its .elm hidden directory.

Some files are directly named after the programs that use them: the .Agenda file is used by the agenda program, and .info is used by the info program. Those almost have a rule of their own, but it’s impossible to distinguish them from .login, from the sh program; .plan for the finger program; .rm-timestamp from a custom program of my own; and I frankly have no idea what program created the .report file!

This should give you an idea of the various ways that Unix programs name and use hidden files. As an exercise, list all the dot files in your home directory and try to figure out which program created each file. Check by looking in the index of this book to see whether a program by that name exists, if it’s a ".xxx" file. If you can’t figure out which programs created which files, you’re not alone. Keep the list handy; refer to it as you learn more about Unix while exploring Sams Teach Yourself Unix in 24 Hours, Fourth Edition, and by the time you’re done, you’ll know exactly how to find out which programs created which dot files.

Task 3.3: The Special Directories . and ..

I haven’t mentioned two dot directories, although they show up in my listing and most certainly show up in your listing too. They are dot and dot dot (. and ..), and they’re shorthand directory names that can be terrifically convenient.

The dot directory is shorthand for the current location in the directory hierarchy; the dot-dot directory moves you up one level, to the parent directory.

Consider again the list of files shown in Figure 3.3. If you were looking at the files in the California Personnel directory (best specified as /California/Personnel) and wanted to check quickly an entry in the Bldgs file for California, either you’d have to use the absolute filename and enter the lengthy ls /California/Bldgs, or, with the new shorthand directories, you could enter ls ../Bldgs.

As directories move ever deeper into the directory hierarchy, the dot-dot notation can save you much typing time. For example, what if the different states and related files were all located in my home directory /home/taylor, in a new directory called business? In that case, the absolute filename for employee Raab,M. in California would be /home/taylor/business/California/Personnel/Raab,M., which is unwieldy and a great deal to type if you want to hop up one level and check on the buildings database in Indiana!

You can use more than one dot-dot notation in a filename too, so if you’re looking at the Raab,M. file and want to check on Dunlap,L., you could save typing in the full filename by instead using ../../../Washington/Personnel/Dunlap,L.. Look at Figure 3.3 to see how that would work, tracing back one level for each dot-dot in the filename.

This explains why the dot-dot shorthand is helpful, but what about the single-dot notation that simply specifies the current directory?

I haven’t stated it explicitly yet, but you’ve probably figured out that one ramification of the Unix file system organization, with its capability to place applications anywhere in the file system, is that the system needs some way to know where to look for particular applications. Just as if you were looking for something in a public library, in Unix, having an understanding of its organization and a strategy for searching is imperative for success and speed.

Unix uses an ordered list of directories called a search path for this purpose. The search path typically lists five or six different directories on the system where the computer checks for any application you request.

The question that arises is, "What happens if your own personal copy of an application has the same name as a standard system application?" The answer is that the system always finds the standard application first, if its directory is listed earlier in the search path.

To avoid this pitfall, use the dot notation, forcing the system to look in the current directory rather than search for the application. If you wanted your own version of the ls command, for example, you’d need to be in the same directory as the command and enter ./ls to ensure that Unix uses your version rather than the standard version.

  1. Enter ./ls on your computer and watch what happens.
  2. Enter ls without the dot notation, and you’ll instantly see how the computer searches through various directories in the search path, finds the ls program, and executes it, automatically.

When you learn about cd (change directory) later in this hour, you also will learn other uses of the dot-dot directory, but the greatest value of the dot directory is that you can use it to force the system to look in the current directory and nowhere else for any file specified.

Task 3.4: The env Command

You’ve learned much about the foundations of the Unix file system and how applications remember your preferences through hidden dot files. There’s another way, however, that the system remembers specifics about you, and that’s through your user environment. The user environment is a collection of specially named variables, mnemonically named values, that have specific values.

  1. To view your environment, you can use the env command. Here’s what I see when I enter the env command on my system:

    % env HOME=/home/taylor SHELL=/bin/csh TERM=vt100 PATH=/home/taylor/bin:/bin:/usr/bin:/usr/ucb:/usr/local/bin: _/usr/unsup/bin:. MAIL=/usr/spool/mail/taylor LOGNAME=taylor TZ=EST5 %

Try it yourself and compare your values with mine. You might find that you have more defined in your environment than I do because your Unix system uses your environment to keep track of more information.

Here we see some of the standard environment variables. Table 3.1 describes what they do.

Table 3.1  Common Shell Environment Variables and What They Do

Variable

Description

HOME

The directory where you log in and store all your personal files

SHELL

The program you run as your command-line interpreter

TERM

The type of terminal emulation you need to provide cursor graphics

PATH

A list of directories searched when you enter a command

MAIL

The file where your incoming mail is stored

LOGNAME

Your login name

TZ

The time zone on your system

NOTE

Many Unix systems offer the printenv command instead of env. If you enter env and the system complains that it can’t find the env command, try using printenv instead. All examples here work with either env or printenv.

Task 3.5: PATH and HOME

The two most important values in your environment are the name of your home directory (HOME) and your search path (PATH). Your home directory (as it’s known) is the name of the directory that you always begin your Unix session within.

The PATH environment variable lists the set of directories, in left-to-right order, that the system searches to find commands and applications you request. You can see from the example that my search path tells the computer to start looking in the /home/taylor/bin directory, and then sequentially try /bin, /usr/bin, /usr/ucb, /usr/local/bin, /usr/unsup/bin, and . before concluding that it can’t find the requested command. Without a PATH, the shell wouldn’t be able to find any of the many, many Unix commands: As a minimum, you always should have /bin and /usr/bin.

  1. You can use the echo command to list specific environment variables too. Enter echo $PATH and echo $HOME. If you forget the "$" then the shell doesn’t know you are specifying that you want to know the value of the named variable. Try it, you’ll see what I mean.

    When I enter these two echo statements as shown above, I get the following results:

    % echo $PATH
    /home/taylor/bin:/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/unsup/
    _bin:.
    % echo $HOME
    /home/taylor
    %

    Your PATH value is probably similar, although certainly not identical, to mine, and your HOME is /home/accountname or similar (accountname is your account name).

Task 3.6: Find Where You Are with pwd

So far you’ve learned a lot about how the file system works but not much about how to move around in the file system. With any trip, the first and most important step is to find out your current location—that is, the directory in which you are currently working. In Unix, the command pwd tells you the present working directory.

  1. Enter pwd. The output should be identical to the output you saw when you entered env HOME because you’re still in your home directory.

    % echo $HOME
    /home/taylor
    % pwd
    /home/taylor
    %

Think of pwd as a compass, always capable of telling you where you are. It also tells you the names of all directories above you because it always lists your current location as an absolute directory name.

Task 3.7: Moving to Another Location with cd

The other half of the dynamic duo is the cd command, which is used to change directories. The format of this command is simple, too: cd new-directory (where new-directory is the name of the new directory you want).

  1. Try moving to the very top level of the file system and entering pwd to see whether the computer agrees that you’ve moved.

    % cd /
    % pwd
    /
    %
  2. Notice that cd doesn’t produce any output. Many Unix commands operate silently like this, unless an error is encountered. The system then indicates the problem. You can see what an error looks like by trying to change your location to a nonexistent directory. Try the /taylor directory to see what happens!

    % cd /taylor
    /taylor: No such file or directory
    %
  3. Enter cd without specifying a directory. What happens? I get the following result:

    % cd
    % pwd
    /home/taylor
    %
  4. Here’s where the HOME environment variable comes into play. Without any directory specified, cd moves you back to your home directory automatically. If you get lost, it’s a fast shorthand way to move to a known location without fuss.

    Remember the dot-dot notation for moving up a level in the directory hierarchy? Here’s where it also proves exceptionally useful. Use the cd command without any arguments to move to your home directory, and then use pwd to ensure that’s where you’ve ended up.

  5. Now, move up one level by using cd .. and check the results with pwd:

    % cd
    % pwd
    /home/taylor
    % cd ..
    % pwd
    /home
    %
% ls -F
armstrong/  christine/  guest/   laura/   matthewm/ shane/
bruce/      david/      higgins/ mac/     rank/     taylor/
cedric/     green/      kane/    mark/    shalini/  vicki/
%

Try using a combination of cd and pwd to move about your file system, and remember that without any arguments, cd always zips you right back to your home directory.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020