Kahibaro
Discord Login Register

2.2.2 Navigating directories

Understanding Your Location in the Filesystem

When you work in a terminal you are always “inside” some directory. The shell keeps track of your current working directory. To display where you are, use pwd which stands for “print working directory”.

Typing:

pwd

prints the full path to your current directory, for example /home/alex or /.

It is important to remember that most commands that work with files and directories will use this current working directory as their starting point if you do not give an absolute path.

Your current working directory affects how relative paths are interpreted. Always use pwd if you are unsure where you are.

Changing Directories with `cd`

To move around the filesystem in the terminal you use the cd command. The basic pattern is:

cd PATH

Here, PATH can be an absolute path such as /etc or a relative path such as Documents.

For example, from any location, this takes you to /etc:

cd /etc

If you are in /home/alex and you run:

cd Documents

your new directory becomes /home/alex/Documents because Documents is interpreted relative to your current working directory.

If the path contains spaces you must either wrap it in quotes or escape the spaces:

cd "My Documents"
cd My\ Documents

Using Special Directory Shortcuts

Several special directory names help you move quickly without typing full paths. These names are interpreted by the shell and are not normal directory names.

The single dot, . refers to the current directory. You rarely use it with cd, but you may see it with other commands.

The double dot, .. refers to the parent directory. This is useful for moving “up” one level:

cd ..

If you are in /home/alex/Documents, after cd .. you will be in /home/alex.

You can chain .. to move up several levels in one step. From /home/alex/Documents/Projects:

cd ../..

moves you first to /home/alex/Documents then to /home/alex.

There are also important shortcuts for your home directory. The tilde character, ~, represents your home directory, for example /home/alex. You can use it on its own:

cd ~

which always returns you to your home directory, no matter where you are. You can also follow ~ with a path inside your home:

cd ~/Downloads

which takes you to /home/yourname/Downloads.

If user home directories are visible, ~username refers to that user’s home directory, for example ~root or ~alice, although this is less common for beginners.

There is also a shortcut with no argument. Typing cd by itself is the same as typing cd ~ and returns you to your home directory.

Remember these shortcuts: cd or cd ~ takes you home, cd .. moves up one level, and cd - jumps back to your previous directory.

Jumping Between Recent Locations

The shell also tracks the last directory you were in. The command:

cd -

switches between your current directory and your previous one. It is like a “back” button. For example, if you move from /home/alex to /etc, then run cd -, you go back to /home/alex. Running cd - again sends you to /etc once more.

Combining `cd` with Absolute and Relative Paths

When you give cd an absolute path, it always goes to that exact directory regardless of where you are now. For example, cd /var/log will always end in /var/log.

When you use a relative path, cd uses your current directory as the starting point. If you are in /home/alex and type:

cd Music/Rock

then the shell interprets this as /home/alex/Music/Rock.

You can mix .. with other directory names to move sideways and up at the same time. Suppose you are in /home/alex/Documents/Projects and want to go to /home/alex/Downloads. You can do:

cd ../../Downloads

This moves up from Projects to Documents, then up to alex, then into Downloads.

Seeing Where You Can Go Next

While navigating, it helps to know what directories are available. The typical workflow is to list the directory contents with a listing command, then move using cd. For example:

pwd
ls
cd some-directory
pwd

This shows your current location, lists subdirectories, and then enters one of them. When you are unsure of exact names, especially with long or complex directory names, this pattern keeps you oriented.

It is also useful to remember that directory names are case sensitive. Documents and documents are different. If cd reports that a directory does not exist, check its spelling and capitalization.

Using Tab Completion to Navigate Faster

Most shells provide tab completion to save typing and avoid mistakes. When you start typing a directory name, you can press the Tab key and the shell will try to complete the name for you.

For example, if there is a directory called Documents in your current location, you can type:

cd Doc

then press Tab. If Doc matches only one entry, the shell completes it to:

cd Documents

If there are several possible matches, pressing Tab twice often shows them all. This makes navigating directories much faster and helps avoid typos.

Practical Navigation Patterns

Over time, certain navigation patterns become very common. You may often start a terminal session somewhere and immediately jump to your home directory, for example with cd or cd ~. From there, you move into a working directory such as cd ~/Projects.

If you are working in two directories at once, for instance /var/log and /etc, you can move between them quickly with cd - instead of retyping paths each time.

When you need to reach a directory far from where you are, use an absolute path to avoid confusion, for example cd /usr/local/bin. When you are moving short distances, such as between nearby directories, relative paths with .. are often more convenient.

Mastering these small navigation commands makes work at the command line smoother. While more advanced tools build on these basics, most daily terminal use relies on a combination of pwd, cd, .., ~, and tab completion to move efficiently through the filesystem.

Views: 7

Comments

Please login to add a comment.

Don't have an account? Register now!