-->
Previous Table of Contents Next


Your Home Directory

When you set up a user account in Chapter 2, you also created a home directory for the user (in this case, you). You can think of your home directory as a base for operations. When you login the system, you’re automatically placed in this directory, and default files for important applications (such as emacs) have been automatically been copied to this directory. Generally, it’s a good idea to name the directory the same as the login name of the user; in Chapter 2, for example, the home directory was named kevinr. The absolute filename of this home directory is /home/kevinr.

You should keep all your files in your home directory. In fact, the default Linux installation gives you no choice other than to store your files in this directory, as file permissions don’t allow you to write to any other directories. (The root user, on the other hand, can do anything to any directory.) You can create subdirectories, however, to better help you organize the many files that you’ll inevitably create as a result of your Linux usage.

You can always use the tilde character (~) as a shortcut for the home directory, as you’ll see in the following commands.

Moving Between Directories with Cd

At any given time, you can be placed in only one directory, which is your current or working directory. If you visualize the directory scheme as a hierarchy, you can also visualize moving between various parts of that hierarchy. The Linux command that allows you to move between directories is cd. You can use to the cd command to point to a specific directory:


     gilbert:/$ cd /usr

     gilbert:/usr$


NOTE:  The Bourne Again SHell, or bash, is set up by default on Linux systems. Bash is designed to show the name of the machine on a prompt (in this instance, gilbert), as well as the current directory. (A colon is used to separate the machine name and the current directory.) As you can see in the previous example, the first line shows that the current directory is /, or the root directory. In the second line—after running the cd command—the current directory is /usr.


NOTE:  We’re getting ahead of ourselves here a bit, diving into UNIX commands without every really describing them. For now, suffice it to say that a command is a direct instruction to the Linux system.

The cd command can be used in many different ways. You can use it to make the root directory your current directory:


     gilbert:/usr$ cd /

     gilbert:/$

You can also use it to move up a single directory in the hierarchy. In the next example, your current directory is /usr/doc and you want to make the /usr directory your current directory. To do this, you’ll need to know that Linux always represents the current directory with a period (.) and the parent directory with two periods (..). The following command line, then, would move your current directory to the parent directory:


     gilbert:/usr/doc$ cd ..

     gilbert:/usr$

The explanation probably made this example seem more complex than it is.

You can also use cd to make a subdirectory your current directory. The trick here is knowing that you’ll want to move to a directory relative to your current directory. Knowing that doc is a subdirectory of the current directory /usr, you would move to the doc directory with the following command line:


     gilbert:/usr$ cd doc

     gilbert:/usr/doc$

However, if you used the following command line, you’d experience failure:


     gilbert:/usr$ cd /doc

     bash: /doc: No such file or directory

You’re generating this error message because doc and /doc would be two different directories—doc exists as a subdirectory of the current directory, while /doc would need to be a subdirectory of the root directory (hence the leading slash). Beginners are sometimes confused by this point.

Another command line that would generate a failure is:


     gilbert:~$ cd..

     bash: cd..: not found

Without the space between the cd command and the notation for the higher-level command, the shell doesn’t understand your request.

You can also move to your home directory at any time, no matter what the current directory is, with the following command:


     gilbert:/usr$ cd ~

     gilbert:~$

The tilde (~) symbol can be used at any time and in other commands as shorthand for your home directory. In addition, using cd without a new directory specification will automatically lead you to your home directory:


     gilbert:/usr$ cd

     gilbert:~$


WARNING:  There’s really only one restriction to the cd command: You must have execute permission for the directory you’re switching to.

If you decide to go with another Linux shell that doesn’t list the current directory (see “Linux Shells,” later in this chapter) at the beginning of the prompt, you’ll need to use the pwd (short for print working directory) command to print the name of the current working directory:


     gilbert:/usr$ pwd

     /usr


Previous Table of Contents Next