Table of Contents
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/projectsThis tells you:
- You’re under
/home - In the user
alex’s home directory - Inside the
projectsdirectory
The `cd` command: change directory
cd changes your current working directory.
Basic usage:
cd PATH
Where PATH can be:
- An absolute path: starts with
/ - A relative path: does not start with
/, and is interpreted relative to your current directory - Special shortcuts like
~,.,.., and-
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 $HOME2. Go to an absolute path
cd /etc
cd /var/log
cd /usr/local/binThese 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/DocumentsThe 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:
- From
/home/alex/projects:
cd ..
# now in /home/alex- Move up two levels in one step:
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 directory6. 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 -
/etcThis is useful when you’re switching back and forth between two locations.
Using `~` for home directories
~ (tilde) is a shortcut for home directories.
~alone: your own home directory
cd ~ # go to /home/yourname~username: another user’s home directory (if it exists and permissions allow):
cd ~root # typically /root
cd ~alex # typically /home/alex
You can combine ~ with subdirectories:
cd ~/Documents
cd ~/projects/linuxThis 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
lsA common pattern:
pwd– check where you are.ls– see what directories exist.cd– move into a directory you see listed.- 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-courseAutocomplete to make navigation faster
Shells like Bash support tab completion, which saves typing and prevents mistakes.
Basic behavior:
- Type part of a path, press
Tab: - If it’s unambiguous, the shell completes it
- If there are multiple matches, press
Tabtwice to see options
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:
- The directory doesn’t exist
- You mistyped the name
- You lack permission to read it
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:
- Go to the sibling directory
/home/alex/projects/notes:
cd ../notes- Go from
/home/alex/projects/linux-course/srcto/home/alex/projects/docs:
cd ../..
cd docs
# OR in one go:
cd ../../docs- Go from
/home/alex/projects/linux-courseto/home/alex/Documents:
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:
- Home:
cd
cd ~- Root of the filesystem:
cd /- Previous directory:
cd -- Subdirectory of home (works from anywhere):
cd ~/Downloads
cd ~/DesktopDealing 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:
- Your home:
/home/alex - You start in
/home/alex
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-courseExample 2: Jump to `/etc`, then back home
cd /etc
pwd
# /etc
cd
pwd
# /home/alexExample 3: Switch between two directories
cd /var/log
pwd
# /var/log
cd /etc
pwd
# /etc
cd -
# back to /var/log
cd -
# back to /etcExample 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/DocumentsPractice ideas
To get comfortable, try these tasks on your system:
- Start in your home directory:
- Use
cdwith no arguments to get there. - Confirm with
pwd. - From your home directory:
- Create some test directories using your file manager (e.g.
test,test/sub1,test/sub2). - Use only
cd,pwd, andlsto: - Go into
test/sub1 - Move back to
test - Jump directly to
test/sub2 - Return to your home directory
- 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.