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.
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):
% emacsNow, 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).
Here is the File pulldown menu:
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:
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
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
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:
What we have done is to give a path or pathname,% cat /boston/food/cajun Cajun Galore Cajuntabulous Dixie Kitchen The Good Cajun Guar-run-teeed good food ...
/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
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:
I know you like cajun, sis, so here is a list of restaurants:The first thing you need to do is restart emacs to edit your letter to sis:Cajun Galore Cajuntabulous Dixie Kitchen The Good Cajun Guar-run-teeed good food ...
% emacs lettosisThis 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
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:
The reason why we use the Save Buffer command this time, instead ofSave 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 withSave Buffer As .
With the final version of your letter stored in the file lettosis, you send it off to your sister via e-mail.
% mkdir lettersNow 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 lettosisbecause lettosis is no longer in your current directory. You can display the file, however, by doing:
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.,% cat letters/lettosis Cajun Galore Cajuntabulous Dixie Kitchen The Good Cajun Guar-run-teeed good food ...
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).
Remember that~is shorthand for your home directory. So, something like~/resumerefers 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.
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:
Pictorally, I can draw the fact that your current directory is your home directory as follows:% pwd /u/your_login_name
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.
% cd lettersIf you were now to use the pwd command, it might report:
(assuming you home directory was% pwd /u/your_login_name/letters
/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:
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.% ls lettosis
First, we create another subdirectory:
% mkdir sisSince 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/lettosis1To confirm that the lettosis file was moved properly, you can use ls:
After moving the file, your home tree looks like this:% ls sis lettosis1
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 sisYou 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: