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 tac Command: Print Lines in Reverse Order

Linux tac Command: Print Lines in Reverse Order

Learn how Linux tac prints records from last to first, handles multiple files and custom separators, and helps inspect recent log entries in reverse order.

Linux install Command: Copy Files and Set Attributes

Linux install Command: Copy Files and Set Attributes

Learn how to use the Linux install command to copy files while setting modes, owners, groups, timestamps, and destination directories in deployment scripts.

Linux gunzip Command: Decompress gzip Files

Linux gunzip Command: Decompress gzip Files

Learn how to decompress gzip Files with the Linux gunzip command, including practical examples, key options, and important precautions.

Linux passwd Command: Change a User Password

Linux passwd Command: Change a User Password

Learn how to change a User Password with the Linux passwd command, including practical examples, key options, and important precautions.

Linux resolvectl Command: Inspect systemd DNS Resolution

Linux resolvectl Command: Inspect systemd DNS Resolution

Learn how to inspect systemd DNS Resolution with the Linux resolvectl command, including practical examples, key options, and important precautions.

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Learn how to find Block-Device UUIDs and Filesystem Types with the Linux blkid command, including practical examples, key options, and important precautions.

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 unxz Command: Decompress xz Files

Linux unxz Command: Decompress xz Files

Learn how to decompress xz Files with the Linux unxz command, including practical examples, key options, and important precautions.

Linux du Command: Check File and Directory Disk Usage

Linux du Command: Check File and Directory Disk Usage

Learn how to use the Linux du command to measure and summarize disk space used by files and directories, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.