Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

To navigate Linux directories, use pwd to check where you are, ls to inspect a directory, and cd to move. tree can show the hierarchy when you need a broader view. This lesson combines the four commands so you can see how absolute and relative paths affect each move.

Check your current working directory first

A relative path entered in a terminal is interpreted from the current working directory. Your prompt might show an abbreviated location or be customized, so use pwd when you need the exact path.

pwd

For example, if your account is named learner and you are in its home directory, the output might be:

/home/learner

The actual home path depends on account configuration. pwd does not change your location. If you entered a directory through a symbolic link, its logical path and physical path may differ. In Bash, pwd -P displays the physically resolved path.

Inspect a directory's contents

ls lists file and directory names. Without an argument, it lists the current directory; with a path, it lists that location. These commands inspect your current directory and then /etc:

ls
ls /etc

The ls /etc output might contain names like these. This is an abbreviated illustration, not a complete listing; names and layout vary by system.

hosts  passwd  ssh

Names beginning with a dot are normally omitted from the default listing. Add -a to include hidden entries. Use -l to see details such as permissions, owner, size, and modification time.

ls -a
ls -l /etc

The first character in an ls -l row indicates the file-system type: d commonly means a directory, - a regular file, and l a symbolic link. The file-types lesson explains the rest of the row. If you need details about a directory itself rather than its contents, use ls -ld path.

Move using absolute and relative paths

cd changes the working directory of the current shell. An absolute path such as /etc takes you to the same location regardless of where you start. Follow it with pwd to verify the result.

cd /etc
pwd

The pwd output will normally be:

/etc

While you are in /etc, cd .. goes up to its parent, /. cd - returns to the previous working directory; Bash also prints the directory it returns to. Run the following commands in the same shell and in this order for the described result.

cd ..
pwd
cd -
pwd

Here the first pwd prints /; cd - and the final pwd show /etc. If you have moved elsewhere in between, cd - will return to a different directory.

Running cd without an argument goes to your home directory. In Bash, cd ~ is another common way to reach it. Because a home directory is not necessarily under /home, confirm the result with pwd.

cd
pwd

View the directory hierarchy

tree displays a hierarchy using branches and indentation. Where ls is useful for inspecting one directory, tree helps you see how directories fit together. Limit the depth at first to avoid a very long listing.

tree -d -L 1 /etc

Follow the branches in this abbreviated illustration to see which directories are immediately beneath /etc. Actual directory names and counts vary.

/etc
├── ssh
└── systemd

-d displays only directories, and -L 1 limits the displayed depth to one level. If tree is not installed, you might see command not found. You can use ls for now and install tree later with your distribution's package manager. The remaining exercises do not require it.

Practice navigating paths

Follow these steps and predict the current directory before each pwd. It is fine if your /etc listing differs from the example; this exercise does not create or change files.

  1. Run pwd to check your starting directory.
  2. Run cd /etc, then use pwd and ls to check your location and its contents.
  3. Run cd .. and use pwd to confirm that you are at /.
  4. Use cd - to return to /etc; finally, run cd to return home.

If you see cd: ...: No such file or directory, check the spelling and letter case of the path. Permission denied may mean that you lack permission to enter it. When unsure of your location, start again with pwd rather than guessing.

For more options and edge cases, read the individual guides to pwd, ls, cd, and tree.

Official documentation

See the GNU Bash built-ins documentation for cd and Bash's pwd, the GNU Coreutils ls documentation, and the tree(1) manual for the options used here.

More in This Category
Linux jobs Command: List Jobs in the Current Shell

Linux jobs Command: List Jobs in the Current Shell

Learn how to list Jobs in the Current Shell with the Linux jobs command, including practical examples, key options, and important precautions.

Linux tree Command: Display a Directory Tree

Linux tree Command: Display a Directory Tree

Learn how to use the Linux tree command to display directory contents as a readable hierarchy and control depth and filtering, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux locate Command: Search Indexed File Paths Quickly

Linux locate Command: Search Indexed File Paths Quickly

Learn how Linux locate searches a filename database, how updatedb affects freshness, how to limit and filter matches, and when find is the better tool.

Linux tr Command: Translate and Delete Characters

Linux tr Command: Translate and Delete Characters

Learn how to translate and Delete Characters with the Linux tr command, including practical examples, key options, and important precautions.

Linux printenv Command: Print Environment Variable Values

Linux printenv Command: Print Environment Variable Values

Learn how to use the Linux printenv command to print all environment variables or selected variable values, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux lscpu Command: Inspect CPU Architecture and Cores

Linux lscpu Command: Inspect CPU Architecture and Cores

Learn how to inspect CPU Architecture and Cores with the Linux lscpu command, including practical examples, key options, and important precautions.

Linux date Command: Display and Format the Current Time

Linux date Command: Display and Format the Current Time

Learn how to display and Format the Current Time with the Linux date command, including practical examples, key options, and important precautions.

Linux pgrep Command: Find Process IDs by Name or Criteria

Linux pgrep Command: Find Process IDs by Name or Criteria

Learn how to find Process IDs by Name or Criteria with the Linux pgrep command, including practical examples, key options, and important precautions.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.

Linux screen Command: Create and Manage Detachable Terminal Sessions

Linux screen Command: Create and Manage Detachable Terminal Sessions

Learn how to use the Linux screen command to keep terminal sessions running and reconnect to them later, with essential options, practical examples, output interpretation, and common troubleshooting tips.