Make your own free website on Tripod.com
Search:
Keywords:
In Association with Amazon.com

Changing Directories with cd

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. Is the directory you want above or below your current directory?

To change directories, use the cd command. 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 / (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 tree below is used only as an example for the purpose of explaining cd.

/
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.

If you type:

cd directory1 

while you are in directory3, you will get an error message, telling you that there is no such directory. That's because there is no directory1 below directory3.

To move up to directory1, you would type:

cd /directory1 

This is an absolute path. It tells Linux to start at the top and look down until it finds directory1.

Just remember that absolute paths will take you TO any directory, FROM any directory and relative paths will only take you to directories below your current one.

Figure 10-3. Absolute Pathnames State the Full Path

Tip

Is the Path Absolute or Relative?

 

A path is absolute if the first character is a /; otherwise, it's a relative path.

Figure 10-4. Relative Pathnames are Relative to Current Position

Here's an exercise involving absolute and relative paths. From your home directory, type the relative path:

cd ../../etc/X11

A cd .. tells your system to go up to the directory immediately above the one in which you are currently working. Obviously, cd ../.. tells it to go up two directories.

After using the full command in the example, you should be in the directory /X11, which is where you'll find configuration files and directories related to the X Window System.

Take a look at your last cd command. You told your system to:

  1. Go up one level to your login directory's parent directory (probably /home)

  2. Then go up to that directory's parent (which is the / directory)

  3. Then go down to the etc directory

  4. Finally, go to the /X11 directory

Using an absolute path would get you to the /X11 directory quickly. Type:

cd /etc/X11

and you're there.

Note

Be Aware of Where You Are

 

Always make sure you know which working directory you're in before you state the relative path to the directory or file you want to get to. You don't have to worry about your position in the filesystem, though, when you state the absolute path to another directory or file. If you aren't sure, type pwd.

Table 10-1. cd Options

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

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 given you permission

cd /dir1/subdirfoo

regardless of which directory you're in, this absolute path would take you straight to subdirfoo, a subdirectory of dir1

cd ../../dir3/X11

this relative path would take you up two directories to root, then to dir3, then to the X11 directory.

Now that you're starting to understand how to change directories, see what happens when you change to root's login directory (the superuser account). Type:

cd /root

You're not logged in as root, so you're "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 section called Ownership and Permissions

To change to the root login, use the su command. Type this series of commands:

[newuser@localhost newuser]$ su 
Password: your root password 
[root@localhost newuser]# cd /root 
[root@localhost /root]#

As soon as you give the root password, you'll 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 Figure 10-5).

Figure 10-5. Becoming Root

Now when you cd to root's login directory, you'll be granted access.

When you're done working as root, just type exit at the prompt.

[root@localhost /root]# exit
exit
[newuser@localhost newuser]$

Book Site Map ElectronicVideo

Amazon.com International Sites :

USA, United Kingdom, Germany, France