UNIX Pathnames and Emacs

WARNING: Some Emacs stuff may be out of date due to new version.

Motivation

File Hierarchy

At this point we have seen a little about the UNIX file hierarchy. It looks something like the following if we draw it as a tree, where your files and directories form a subtree of the big tree:

When you log in to the UNIX system you are placed in your home directory. If you only wanted to use files in your home directory then things would be very simple. However, if you want to be able organize files into subdirectories and to access files that are elsewhere in the file hierarchy, then you must be able to refer to files that are not in your home directory and move around in the hierarchy.

Emacs

Emacs is an editor that we can use to create text files. A text file is different from, for example, a word-processed file. A word-processed file stores not only the text of a document, but information about spacing, e.g., single- or double-spaced, margins, styles such as bold or underlined, etc. In contrast, a text file just contains the text.

An Example

Let's go through an example where we create a file using Emacs and place it in a subdirectory of your home directory. Suppose that your part of the file hierarchy looks like this:

It's pretty boring, just your home directory and a single file that contains your resume.

What you want to do is to write a letter to your sister. She is coming to Boston for a conference and you want to tell her a little about what there is to do here. Since you are going to send this letter by e-mail, you just need to create a text file....Emacs can do this.

You start up Emacs by typing at the prompt (the % represents the prompt):

% emacs
Now, if you are on one of the big-screened computers, you will see a new window on the screen, like:

where you can type in your letter. If you are using a plain text terminal (with no mouse or fancy windows on the screen), then you may see something like this:

In Emacs, you compose your letter to your sister and now want to store it in a file (to be later e-mailed).

Emacs Menubar

If you are running Emacs in its own window (i.e., not on a text terminal), then you can perform several functions using the menubar at the top of the Emacs window. The menubar has menus named: Buffers, File, Edit, and Help. If you wanted to perform some operation, like saving the text to a file, you could do so using the menubar. For example, to activate the Save Buffer As operation from the menubar:
  1. Place the mouse pointer over the word File in the menubar and click on it keeping the mouse button depressed;
  2. While still holding down the mouse button, drag the mouse pointer until it is over Save Buffer As, which will be surrounded by a rectangle;
  3. Finally, by releasing the mouse button, you will activate that function.

    Here is the File pulldown menu:

    Key Commands

    If you are working on a text terminal, you will not have pulldown menus. Nonetheless, commands in Emacs, such as saving a file, can be activated through a set of key commands. Therefore, if you wanted to save a new file while running Emacs in a text terminal, you can use the C-x C-w command (the C is short for the Ctrl key).

    Emacs uses a lot of Ctrl-key combinations to perform commands. Whenever you need to perform a Ctrl-key combination, you do so in the following manner:

    1. Press and hold down the Ctrl key;
    2. Press the second key;
    3. Release both keys.
    So, for the C-x C-w command there are two sets of Ctrl-key combinations that must be performed one after another. Another type of key combination that Emacs uses is ESC-key combinations. For these, you must press and release the ESC key and then press and release the other key.

    Using Menubar or Key Commands

    In the case of Emacs running in a window, you can use either the menubar or key commands, although there will be functions that can be activated by key commands, but that are not present under any of the menus.

    Actually Save the File...

    Now that we have gone through a long digression about ways to activate Emacs commands, remember we wanted to save the letter to your sister. So, activate the Save Buffer As command via the File menu or by pressing C-x C-w. You will be prompted to enter the name to give the file at the bottom of the screen. Let's call this letter, lettosis, so type that in and press RETURN.

    You are done using Emacs for now, so let's exit Emacs. You may do so from the File menu or via the key command C-x C-c.

    Now we can type the ls command at the prompt to look at the files in our current directory and make sure our letter is there:

    % ls
    lettosis	resume
    
    

    Accessing Files Elsewhere In the File Hierarchy

    You have written your letter to your sister and placed it in a file, you are about to mail it when you remember that your sister likes cajun food a real lot, so you should probably tell her what cajun restaurants are available in Boston. Problem is: you don't like cajun food, so you don't know where to find good restaurants.

    But aha you think, there is a list of cajun restaurants on this system that I can access. In the file hierarchy, this list is in a file called cajun:

    You want to take a look at the restaurants listed in this file, so you use the cat command to display the file on the screen:

    % cat cajun
    

    Hmmm, the cat commands complains and it doesn't work.

    The reason why you can't access this file is because it is not in your current directory. We must tell cat where this file exists in the file hierarchy. Let's start at the top of the hierarchy, the directory named /, and list each directory we must pass through to reach cajun:

    Now we can use cat to display this file as follows:

    % cat /boston/food/cajun
    Cajun Galore
    Cajuntabulous
    Dixie Kitchen
    The Good Cajun
    Guar-run-teeed good food
    ...
    
    What we have done is to give a path or pathname, /boston/food, along with the filename, cajun. This tells UNIX how to find the file. This path starts in the root directory, /, goes to the boston directory, the food directory, and then finally reaches the cajun file. Thus, we write the path beginning with a /(for the root directory), then list each of the directories we traverse with /'s between them, giving a path of:
    /boston/food
    and finally the path is separated from the filename with a slash, giving:
    /boston/food/cajun

    Notes:
    When a path is used in this way, i.e., it starts from the root (/) directory, we call it a full path (since it starts from the very top of the tree--the root).

    Be aware that some people include the filename in what they call a path, i.e., /boston/food/cajun, and some people do not, i.e., /boston/food.


    Here are the parts of the path for the cajun file:

    Notice that the slash takes on 2 purposes:

    1. When used at the beginning of the path, it refers to the root directory.
    2. It is also used to separate directories in the path and to separate the path from the filename.
    File and directory names cannot contain slashes, since the slash is used for these special purposes.

    Re-editing our letter

    After looking at the list of cajun restaurants, you decide that there are too many to pick and choose from, so you just want to amend your letter to your sister as follows:
    I know you like cajun, sis, so here is a list of restaurants:
    Cajun Galore
    Cajuntabulous
    Dixie Kitchen
    The Good Cajun
    Guar-run-teeed good food
    ...
    
    The first thing you need to do is restart emacs to edit your letter to sis:
    % emacs lettosis
    This will start up emacs and automatically load the file lettosis into the emacs window. Now you need to move to the appropriate place in your letter and type in:
    I know you like cajun, sis, so here is a list of restaurants:
    You need to load the list of cajun restaurants into your letter--you can do this in emacs. Use the C-x i command; it will ask you what file to insert at the bottom of the screen by prompting you with:
    Insert file: ~/
    The Insert file: part is the prompt, the ~/ part is a default path. That just means that if you typed in a filename like lettosis, it would load the file ~/lettosis, i.e., the file named lettosis with a path of ~. The tilde (~) is just shorthand for your home directory.

    However, you do not want to load a file in your home directory, you want the cajun file so:

    1. Erase the ~/;
    2. Type in /boston/food/cajun, which is the full path for the file you want to insert.
    The cajun file will be insert into your letter. You sign the letter by typing your name at the bottom of the letter and then save the edited letter. If you are using emacs in a window, use the Save Buffer command from the File menu or use the key command C-x C-s. When running in a text terminal, you must use C-x C-\ to save instead of C-x C-s.


    Note:
    The reason why we use the Save Buffer command this time, instead of Save Buffer As is because our file already has a name, lettosis; when you have a file that doesn't already have a name, you should save it with Save Buffer As.

    With the final version of your letter stored in the file lettosis, you send it off to your sister via e-mail.

    Organizing Files in a Directory

    You decide at this point that you write a lot of letters on the computer and you want to organize them together. What you want to do is create a subdirectory called letters in your home directory. You use the mkdir command to create this subdirectory:
    % mkdir letters
    Now to move your letter into this directory, you can use the move command, mv. You move the file lettosis into the letters subdirectory:
    % mv lettosis letters

    Your part of the file hierarchy should now look like this:

    To make sure that the file was moved, you can list the files in the new letters subdirectory with ls:

    % ls letters
    lettosis
    

    If you want to display your lettosis now, you can no longer just use:

    cat lettosis
    because lettosis is no longer in your current directory. You can display the file, however, by doing:
    % cat letters/lettosis
    Cajun Galore
    Cajuntabulous
    Dixie Kitchen
    The Good Cajun
    Guar-run-teeed good food
    ...
    
    Here we specify the path, letters, for the file, lettosis. Why does this path not begin with a slash? Because this path does not start from the root of the directory hierarchy; instead, it starts from the current directory, i.e.,
    letters/lettosis
    means "in the current directory, there is a subdirectory called letters, in it is the file named lettosis."

    This type of path that starts from the current directory instead of the root directory is called a relative path. You can distinguish a full path from a relative path because full paths start with a slash (for the root directory) and relative paths do not.

    Relative paths are dependent on where you are in the file hierarchy. For example, the fact that letters/lettosis refers to the lettosis file in the letters subdirectory depends on the fact that you are currently in your home directory... if you were in some other directory it would not work (we discuss moving to another directory below).


    Note:
    Remember that ~ is shorthand for your home directory. So, something like ~/resume refers to the resume file in your home directory. Paths that start with ~ are actually full paths even though they do not start with a slash because ~ is shorthand for the full path of your home directory.

    Current Directory

    As mentioned previously, whenever you are logged into the machine you reside in a particular directory, i.e., your current directory. Originally, you could refer to the file lettosis without having to use a path, because lettosis was in the same directory you were currently in. Once we moved lettosis to a subdirectory, we had to use a path, i.e., letters/lettosis.

    When you log in, you start out in your home directory. While you are in this directory, you can find out its path by using the pwd (present working directory--just another name for current directory). For this example, let us suppose the full path of your home directory is:

    /u/your_login_name
    so the pwd command returns:
    % pwd
    /u/your_login_name
    
    Pictorally, I can draw the fact that your current directory is your home directory as follows:

    I.e., your home directory is where you currently are in the directory tree (little stick person). Notice that you are in your_login_name, your home directory, and in that directory you can see the file resume and the subdirectory letters.

    Moving to Another Directory

    Suppose you want to go into the letters directory. You can do so by using the cd, change directory, command. You do so by typing:
    % cd letters
    If you were now to use the pwd command, it might report:
    % pwd
    /u/your_login_name/letters
    
    (assuming you home directory was /u/your_login_name, which I doubt it is).

    Here is what the picture looks like now:

    If I now use ls to see what files are in this directory is may come as no suprise that I see:

    % ls
    lettosis
    
    Remember, since our current directory is now the letters directory, ls shows the files in that directory by default. Now, what if you write a lot of letters to your sister. You want them in this letters directory, but you want them separated from other letter, for example, those to your parents. What you can do is to have another subdirectory, called sis, inside your letters subdirectory, and put your letter to sis in there.

    First, we create another subdirectory:

    % mkdir sis
    Since we are currently in the letters directory, this new subdirectory, sis is made under it:

    Then, we want to place lettosis in that directory, but since you will write many letters to your sister, you want to call it lettosis1. Well, we can use the mv command like before, to put it in the subdirectory sis, but we can also use it to rename lettosis at the same time. Here is the command:

    % mv lettosis sis/lettosis1
    To confirm that the lettosis file was moved properly, you can use ls:
    % ls sis
    lettosis1
    
    After moving the file, your home tree looks like this:

    Now, suppose that you want to go to the sis subdirectory, since you are already in the letters directory, and sis is only one level below, you can do:

    % cd sis
    You are now in the sis subdirectory:

    Now, suppose we want to go back up the directory tree into the letters subdirectory. We can do:

    % cd ..
    and we are back in the letters directory:

    Why? Because .. (dot dot) always refers to the directory one above. So, if we do:

    % cd ..
    we will be back in our home directory:


    BU CAS CS - UNIX Pathnames and Emacs
    Copyright © 1993-2000 by Robert I. Pitts <rip@bu.edu> All Rights Reserved.