Kahibaro
Discord Login Register

Navigating directories

Why “navigating directories” matters

On the command line, you move around the filesystem using text commands instead of a graphical file manager. Being comfortable with this is essential because almost every command you run depends on “where you are” in the directory tree.

This chapter assumes you already know what files and directories are and that you’ve seen basic listing commands. Here we focus specifically on moving between directories and understanding your current location.


Your current directory: `pwd`

To see where you currently are in the filesystem, use:

pwd

pwd stands for print working directory. It shows an absolute path from / (the root of the filesystem) to your current directory, for example:

$ pwd
/home/alex/projects

This tells you:

The `cd` command: change directory

cd changes your current working directory.

Basic usage:

cd PATH

Where PATH can be:

Common `cd` patterns

1. Go to your home directory

cd
# or
cd ~

No arguments (just cd) or cd ~ both take you to your home directory, e.g. /home/alex.

You can see your home directory path with:

echo $HOME

2. Go to an absolute path

cd /etc
cd /var/log
cd /usr/local/bin

These paths do not depend on where you are now.

3. Go to a relative path

If your current directory is /home/alex:

cd projects     # now /home/alex/projects
cd Documents    # now /home/alex/Documents

The shell interprets these as “inside the current directory.”

You can chain directories:

cd projects/linux-course

This means: from where I am now, go to projects, then to linux-course.

4. Move “up” with `..`

.. means “the parent directory.”

Examples:

  cd ..
  # now in /home/alex
  cd ../..
  # from /home/alex/projects -> /home

.. is relative, so its effect depends on your current directory.

5. Stay “here” with `.`

. means “the current directory.”

You’ll rarely use . directly with cd, but you’ll see it used in other commands, for example:

ls .

cd . keeps you in the same place (it’s mostly pointless by itself):

cd .
# still in the same directory

6. Jump back to the previous directory with `cd -`

cd - toggles between your current directory and your previous one.

Example:

$ pwd
/home/alex
$ cd /etc
$ pwd
/etc
$ cd -
/home/alex
$ cd -
/etc

This is useful when you’re switching back and forth between two locations.


Using `~` for home directories

~ (tilde) is a shortcut for home directories.

  cd ~        # go to /home/yourname
  cd ~root    # typically /root
  cd ~alex    # typically /home/alex

You can combine ~ with subdirectories:

cd ~/Documents
cd ~/projects/linux

This always works even if your current directory is somewhere else entirely.


Combining navigation with `ls`

To see where you are and what’s around you, use pwd and ls together:

pwd
ls

A common pattern:

  1. pwd – check where you are.
  2. ls – see what directories exist.
  3. cd – move into a directory you see listed.
  4. Repeat.

Example session:

$ pwd
/home/alex
$ ls
Documents  Downloads  projects
$ cd projects
$ pwd
/home/alex/projects
$ ls
linux-course  notes.txt
$ cd linux-course
$ pwd
/home/alex/projects/linux-course

Autocomplete to make navigation faster

Shells like Bash support tab completion, which saves typing and prevents mistakes.

Basic behavior:

Examples (in /home/alex):

cd Doc<Tab>
# becomes:
cd Documents/
cd proj<Tab>
# becomes:
cd projects/

For nested paths:

cd proj<Tab>/lin<Tab>
# becomes:
cd projects/linux-course/

If you get no completion, it likely means:

Navigating using `cd` with `..` and relative paths

You can combine .. with relative paths to move in more complex ways without typing full absolute paths.

Examples:

Assume you are in /home/alex/projects/linux-course:

  cd ../notes
  cd ../..
  cd docs
  # OR in one go:
  cd ../../docs
  cd ../../Documents
  # /home/alex/projects/linux-course -> ../.. = /home/alex -> Documents

Practice combining .. with directory names to avoid typing long absolute paths.


Shortcuts for common locations

Typical shortcuts using cd:

  cd
  cd ~
  cd /
  cd -
  cd ~/Downloads
  cd ~/Desktop

Dealing with spaces in directory names

If a directory name contains spaces (often in desktop environments), you must quote or escape the spaces.

Given a directory My Documents in your home directory:

Three equivalent ways to navigate there:

cd "My Documents"
cd 'My Documents'
cd My\ Documents

The backslash \ escapes the space so the shell treats it as part of the name.

You can combine this with ~:

cd "~/My Documents"
cd ~/My\ Documents

Tab completion helps here too: type the first few letters and press Tab; the shell will automatically insert the needed escaping.


Quick navigation examples

Assume:

Example 1: Move to a nested project directory

pwd
# /home/alex
ls
# Documents  Downloads  projects
cd projects
ls
# linux-course  notes.txt
cd linux-course
pwd
# /home/alex/projects/linux-course

Example 2: Jump to `/etc`, then back home

cd /etc
pwd
# /etc
cd
pwd
# /home/alex

Example 3: Switch between two directories

cd /var/log
pwd
# /var/log
cd /etc
pwd
# /etc
cd -
# back to /var/log
cd -
# back to /etc

Example 4: Use relative paths and `..`

Starting in /home/alex/projects/linux-course:

pwd
# /home/alex/projects/linux-course
cd ..
pwd
# /home/alex/projects
cd ../Documents
pwd
# /home/alex/Documents

Practice ideas

To get comfortable, try these tasks on your system:

  1. Start in your home directory:
    • Use cd with no arguments to get there.
    • Confirm with pwd.
  2. From your home directory:
    • Create some test directories using your file manager (e.g. test, test/sub1, test/sub2).
    • Use only cd, pwd, and ls to:
      • Go into test/sub1
      • Move back to test
      • Jump directly to test/sub2
      • Return to your home directory
  3. If you have directories with spaces in names:
    • Practice navigating into them using quotes and backslashes.
    • Use tab completion to avoid typing the whole name.

With regular use, navigating directories with cd, pwd, and tab completion will become automatic, and you’ll move around the filesystem as quickly as you would in a graphical file manager—often faster.

Views: 24

Comments

Please login to add a comment.

Don't have an account? Register now!