Red Hat Enterprise Linux 3: Red Hat Enterprise Linux Step By Step Guide | ||
---|---|---|
Prev | Chapter 3. Shell Prompt Basics | Next |
Changing directories is easy as long as you know where you are (your current directory) and how that relates to where you want to go.
To change directories, use the cd command. Typing this command by itself returns 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 of the file system with / (referred to as root) and then look down for the requested directory; relative paths look down from your current directory, wherever that may be. 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
cd directory1 |
while you are in directory3, presents you with an error message explaining that there is no such directory. This is because there is no directory1 below directory3.
To move up to directory1, type:
cd /directory1 |
This is an example of an absolute path. It tells Linux to start at the top of the directory tree (/) and change to directory1. A path is absolute if the first character is a /. Otherwise, it is a relative path.
Using absolute paths allows you to change to a directory from the / directory, which requires you to know and type the complete path. Using relative paths allows you to change to a directory relative to the directory you are currently in, which can be convenient if you are changing to a subdirectory within your current directory.
The command cd .. tells your system to go up to the directory immediately above the one in which you are currently working. To go up two directories, use the cd ../.. command.
Use the following exercise to test what you have learned regarding absolute and relative paths. From your home directory, type the relative path:
cd ../../etc/X11 |
After using the full command in the example, you should be in the directory X11, which is where configuration files and directories related to the X Window System are available.
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 moves you to the /etc/X11/ directory more quickly. For example:
cd /etc/X11 |
Absolute paths start from the root directory (/) and move down to the directory you specify.
Note | |
---|---|
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 is displayed, which can be your guide for moving up and down directories using relative pathnames. |
Command | Function |
---|---|
cd | Returns you to your login directory |
cd ~ | Also returns you to your login directory |
cd / | Takes you to the entire system's root directory |
cd /root | Takes you to the home directory of the root, or superuser, account created at installation; you must be the root user to access this directory |
cd /home | Takes you to the home directory, where user login directories are usually stored |
cd .. | Moves you up one directory |
cd ~otheruser | Takes you to otheruser's login directory, if otheruser has granted you permission |
cd /dir1/subdirfoo | Regardless of which directory you are in, this absolute path takes you directly to subdirfoo, a subdirectory of dir1 |
cd ../../dir3/dir2 | This relative path takes you up two directories, then to dir3, then to the dir2 directory |
Table 3-1. cd Options
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:
cd /root |
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. Refer to Section 3.13 Ownership and Permissions.
To change to the root login and root directory, use the su - command.
su - |
Tip | |
---|---|
The command su means
substitute users and it allows you to log in as another user
temporarily. When you type su by itself and press
|
As soon as you give the root password, the prompt changes to superuser status. For example:
[root@localhost root]$ |
When you are done working as root, type exit at the prompt; you are returned to your user account.