-->
Previous Table of Contents Next


Someone else on the system might also have a directory called novel in his or her home directory. Perhaps it even contains a file called chapter_1. In this case, you can’t distinguish the two files by using the relative filename novel/chapter_1. However, the absolute filenames will be different—for instance, /home/fido/novel/chapter_1 as opposed to /home/mary/novel/chapter_1. The novel subdirectory in /home/fido is not the same subdirectory as the novel directory in /home/mary! The two are in quite separate locations and only coincidentally do they share the same name. The two files will have completely different contents, too.

Going Places: The cd Command

The cd (change directory) command lets you change your working directory. You can think of it as moving to another directory. If you’ve worked with DOS or the DOS prompt in Windows, you’ve seen this command before (yes, it was swiped from UNIX!).

The syntax of the cd command is


cd <directory>

There must be a space between cd and the directory specification. You should specify only one directory name to be changed into. The directory specification can be an absolute or relative one. For instance, type cd .. followed by pwd:


darkstar:~$ pwd

darkstar:/home$ /home/fido

darkstar:~$ cd ..

darkstar:/home$ pwd

/home

darkstar:/home$ cd ..

darkstar:/$ pwd

/

darkstar:/$ cd ..

darkstar:/$ pwd

/

As you can see in the preceding example, we started in /home/fido (that’s the absolute path name) and then moved up one directory level with the .. command. That put us in /home. Another move to the parent and we’re in the root directory. We can’t go any higher than the root directory because there is no parent directory for the root directory. Typing cd .. when in the root directory simply leaves you in the root directory.

Note that the Linux command prompt usually shows you which directory you are currently in, so you don’t have to type pwd all the time. (We’ll continue to use pwd for clarity.) Not all Linux systems do show your current directory in the shell prompt because the system administrator may have customized the prompt for you.

Let’s suppose you want to go into a subdirectory of your home directory. We can cd back to your home directory and then cd into a subdirectory called book:


darkstar:/$ cd /home/fido

darkstar:~$ pwd

darkstar:/home$ /home/fido

darkstar:~$ cd book

darkstar:~/book$ pwd

/home/fido/book

In this case, we used cd to get back home (verified with the pwd command), then told Linux to make the subdirectory book our current directory. We know the directory book is below our home directory, so we used relative filenames to move into it. We could have specified the absolute pathname, too, but this was much easier. To avoid any confusion or mistakes, use the absolute directory names when using the cd command:


darkstar:/$ cd /usr/bin

darkstar:/usr/bin$ pwd

/usr/bin

When you type an absolute directory name, you go to that directory, no matter where you started from. When you type cd .., where you end up depends on where you started.

To see the effect of changing your working directory, type ls. The list of files is so long that the first part scrolls off your screen. The ls command shows you the contents of your current directory (as always), but now your current directory is /usr/bin, which contains many more files than your home directory.

There’s No Place Like Home

Here’s a handy trick that many UNIX and Linux users don’t use. Type cd without any directory specification:


darkstar:/usr/bin$ cd

darkstar:~$ pwd

/home/fido

Typing cd by itself always returns you to your home directory, no matter where you are when you type cd. When exploring the file system, you sometimes wind up deep in a blind alley of subdirectories. Type cd to quickly return home or type cd / to return to the root directory.

The ~ in your prompt is another special character. It stands for your home directory. There’s no reason to type cd ~ when cd works just as well, and is much easier to type! A tilde (~) by itself indicates your own home directory.

Linux also uses the ~ symbol to mean the parent directory of user directories. When you type cd~<user>, you move to that user’s home directory. This is a very useful trick, especially on large systems with many users and more complicated directory structures than the simple /home/<user> on your Linux system.

When you’re changing to a distant directory, it’s often a good idea to take several steps. If you mistype a very long directory specification, you will have to retype the entire specification. Sometimes it may not even be clear why cd gave you an error! Taking a number of shorter steps means less retyping in case of an error. Consider this example:


darkstar:~$ cd /usr/docs/faq/unix

bash: /usr/docs/faq/unix: No such file or directory

You’re pretty sure that this path is correct. Let’s change directories one step at a time:


darkstar:~$ cd /usr

darkstar:/usr$ cd docs

bash: docs: No such file or directory

There’s a problem with docs. The directory is actually named doc:


darkstar:/usr$ ls

bin/  doc/  games/  info/  man/  sbin/  spool/

darkstar:/usr$ cd doc

darkstar:/usr/doc$ cd faq/unix

darkstar:/usr/doc/faq/unix$ pwd/usr/doc/faq/unix


Previous Table of Contents Next