Changing directories is easy as long as you know your current directory and how that location relates to where you want to go.
To change directories, use the cd command. Typing this command by itself will always return you to your home directory; moving to any other directory requires a pathname.
You can use absolute or relative pathnames. Absolute paths start at the top with / (referred to as the root directory) and then look down through the file system from there for the requested directory; relative paths look in your current directory and then down from there. The following directory tree illustrates how cd operates.
/ /directory1 /directory1/directory2 /directory1/directory2/directory3
If you are currently in directory3 and you want to switch to directory1, you need to move up in the directory tree.
Executing the command
while you are in directory3 will thus present you with an error message explaining that there is no such directory. This is because there is no directory1 within directory3.
To move up to directory1 you’ll have to type the absolute pathname, as follows:
This is an example of an absolute path; it tells Linux to start at the top (/) and look down until it finds directory1. A path is absolute if the first character is a /. Otherwise, it is a relative path.
Absolute paths will take you to any directory from any directory. Relative paths will only take you to directories below your current one.
Use the following exercise to test what you have learned so far regarding absolute and relative paths.
From your home directory, type the relative path:
After using the full command in the example, you should be in the directory X11, which is where you will find configuration files and directories related to the X Window System.
Take a look at your last cd command. You told your system to:
Go up one level to your login directory's parent directory (probably /home).
Then go up to that directory's parent (which is the root, or /, directory).
Then go down to the etc directory.
Finally, go to the X11 directory.
Conversely, using an absolute path would get you to the /etc/X11 directory more quickly. For example:
Absolute paths start from the root directory (/) and move down to the directory you specify.
Always make sure you know which working directory you are in before you state the relative path to the directory or file you want to get to. You do not have to worry about your position in the file system, though, when you state the absolute path to another directory or file. If you are not sure, type pwd and your current working directory will be displayed.
Now that you are starting to understand how to change directories, see what happens when you change to root's login directory (the superuser account). Type:
If you are not logged in as root, you are denied permission to access that directory. Denying access to the root and other users' accounts (or login directories) is one way your Linux system prevents accidental or malicious tampering. See the “Ownership and Permissions” section later in this chapter.
To change to the root login and root directory, use the su command. For example:
[sam@halloween sam]$su Password:your_root_password [root@halloween sam]#cd /root [root@halloween /root]#
The su command means switch users and allows you to temporarily log in as another user. When you type su by itself, you become root (also called the superuser) while still inside your login shell (your user's home directory). Typing su - makes you become root with root's login shell as if you had logged in as root originally.
As soon as you give the root password, you will see the changes in your command prompt to show your new, superuser status, the root account designation at the front of the prompt and "#" at the end (as shown in the prior example).
When you are done working as root, type exit at the prompt and you will return to your user account.
[root@halloween /root]#exit exit [sam@halloween sam]$