Changing Directories in Linux

Changing Directories in Linux

Files are used to store data such as text, graphics and programs. Directories are a type of file used to store other files–they provide a hierarchical organizational structure. The image below shows an abbreviated version of the filesystem structure on the virtual machines.

When you start a fresh virtual machine, either by opening the course or after using the reset button, you are logged in as the sysadmin user in your home directory, highlighted below:

To navigate the filesystem structure, use the cd (change directory) command to change directories.

cd [options] [path]

If you look back at the graphic above, you will see the Documents directory is located within the home directory, where you are currently located. To move to the Downloads directory, use it as argument to the cd command:

Directories are equivalent to folders on Windows and Mac OS. Like these more popular operating systems, a Linux directory structure has a top level. It is not called “My Computer”, but rather the root directory and it is represented by the / character. To move to the root directory, use the / character as the argument to the cd command.

Filesystem hierarchy with highlighted root directory at the top. The argument to the cd command is more than just the name of a directory, it is actually a path. A path is a list of directories separated by the / character. For example, /home/sysadmin is the path to your home directory:

If you think of the filesystem as a map, paths are the step-by-step directions; they can be used to indicate the location of any file within the filesystem. There are two types of paths: absolute and relative. Absolute paths start at the root of the filesystem, relative paths start from your current location.

Absolute Paths

An absolute path allows you to specify the exact location of a directory. It always starts at the root directory, therefore it always begins with the / character. The path to the home directory /home/sysadmin is an absolute path. The path begins at the root / directory, moves into the home directory, and then into the sysadmin directory.
Use this path as an argument to the cd command to move back into the home directory for the sysadmin user. No output means the command succeeded. Go ahead and confirm this using the pwd command.

Relative Paths

A relative path gives directions to a file relative to your current location in the filesystem. Relative paths do not start with the / character, they start with the name of a directory. Take another look at the first cd command example. The argument is an example of the simplest relative path: the name of a directory in your current location.

A relative path begins in from with the current directory, however you don’t include it in the path. The first step would be to move into the School directory, and then move into the Art directory. Use the / character to separate the directory names and the result School/Art is a relative path from the Documents directory to the Art directory:

Leave a Reply
Your email address will not be published. *

This site uses Akismet to reduce spam. Learn how your comment data is processed.