Breadcrumbs 
Learning >> Documentation >> File System >> Traversing directories
 
Recent News
We Want You!
Come and get involved! (And Christmas Party!
[ more ]
Make your own Xbox games!
Learn to make Xbox 360 games
[ more ]
New Committee
A new committee has been appointed.
[ more ]
Traversing Directories
Traversing Directories

In Linux, we get from directory to directory as follows:

cd [directory name]

So "cd public_html" will get you into your public_html directory, which holds your website.

roryd@frink:~$ cd public_html/
roryd@frink:~/public_html$

If we now use the "pwd" command we can see we are one directory lower:

roryd@frink:~/public_html$ pwd
/home/users/roryd/public_html
roryd@frink:~/public_html$

You can then use "ls" along with its different options in this new directory to list all your files. You can then "cd" into these new directories. What if you want to get back to your home directory?

roryd@frink:~/public_html$ cd
roryd@frink:~$

Without a directory name, "cd" on its own will return you to your home directory. As "~" is a shortcut for your home directory, you can also type:

roryd@frink:~/public_html$ cd ~
roryd@frink:~$

Final thing of interest with the "cd" command is how to move up one directory.

defaultuser@frink:~/public_html$ ls -a
.
cgi-bin compsoc mozilla
.. projects index.php

Here we use a slight variation of the "ls -A" command which lists hidden files. Here we use "ls -a" which lists hidden files and also these wierd dots. So what do "." and ".." mean? They are simply shortcuts. "." stands for the current directory and ".." means the the parent directory (the directory one level higher then the current). They are always present in any directory.

roryd@frink:~/public_html/myprograms/text$ cd ..
roryd@frink:~/public_html/myprograms$

Here we see that "cd .." brings us up one directory.

roryd@frink:~/public_html/myprograms/text$ cd ../../
roryd@frink:~/public_html$

Here we moved up two directories. You'll probably never need to move up more than two directories at a time, as you have the "~" shortcut to get back to your home directory.

roryd@frink:~/public_html/myprograms$ cd .
roryd@frink:~/public_html/myprograms$

Here we see that "cd ." keeps us in the current directory. What use is that? Well sometimes you need the current directory shortcut for running your own programs. Lets say your wrote a quick program called "printdate" in your myprograms directory. Lets try and run it:

roryd@frink:~/public_html/myprograms$ ls
printdate
roryd@frink:~/public_html/myprograms$ printdate
bash: printdate: command not found

We are getting an error as Linux only looks in the main system directories for commands you type in (dark places like /usr/bin/), not your personal ones. This is to protect you from running a command accidentally. So how do we run it? Simply tell Linux the command is in your current directory using the "." shortcut:

roryd@frink:~/public_html/myprograms$ ./printdate
Today is Tuesday August 19th 2003

We'll find out later how to set some preferences and have Linux search these directories by default.

As a final note, you can use "." and ".." as part of a path name in virtually any command in Linux. Ok lets copy some files.