CIS120 Linux Fundamentals by Scott Shaper

pwd, cd and tree Commands

The cd, pwd, and tree commands are essential tools for navigating the Linux file system efficiently. The cd (change directory) command allows users to move between directories, making it possible to access files and organize the system effectively. The pwd (print working directory) command helps users confirm their current location within the file system, which is particularly useful when working with deeply nested directories. The tree command provides a structured view of the directory hierarchy, displaying files and subdirectories in an easy-to-read format. Together, these commands enable users to move through the file system, verify their position, and visualize the directory structure, improving workflow and organization.

Using the examples in this lesson: The examples below use the directories created by the course setup.sh script.

Quick Reference

Command Description Common Use
pwd Print working directory Show current location
cd Change directory Navigate between directories
tree Display directory structure View folder hierarchy

pwd Command

The pwd command in Linux stands for "print working directory." It displays the full path of your current directory—that is, the path starting from the root of the file system. The root (written as /) is the top-level directory on a Linux system: all other folders and files live somewhere under it, so a full path might look like /home/username/playground/chapter1. (Some shells show your home directory as ~ instead of /home/username.) pwd is especially useful for verifying your location within the file system, which can help avoid confusion when navigating complex directory structures.

When to Use pwd

The pwd command is particularly useful in these scenarios:

Command Options

Option Description
-P Display the physical path (resolves symbolic links)
-L Display the logical path (default behavior)

When do they differ? Most of the time pwd -L and pwd -P show the same path. They differ only when your current directory was reached by following a symbolic link (a shortcut that points to another folder). In that case, -L shows the path that includes the link name (the way you got there), and -P shows the real location on disk with the link “resolved.” You will learn about symbolic links later in this book; until then, either option is fine and the output will usually be the same. In most cases, you will not need to use these options and the default behavior of pwd will suffice.

# Basic usage
pwd

# Show physical path (resolves symlinks discussed later in this book)
pwd -P

# Show logical path (default)
pwd -L

Common Use Cases

# Verify current location
pwd

# Check path after navigation (using setup directories)
cd ~/playground/chapter1
pwd

cd Command

The cd command in Linux is used to change the current working directory. This command is fundamental for navigating the file system. Understanding how to use cd effectively will help you move between directories quickly and efficiently.

Path Types

Understanding the difference between relative and absolute paths is crucial for effective navigation in the Linux file system. Think of it like giving directions: absolute paths are like giving someone your full address, while relative paths are like saying "go two blocks down from where you are now." Though I am adding this to the cd command section, absolute and relative paths are also used with other commands, such as ls, mv, cp, rm, mkdir, rmdir, touch, cat, less, grep, chmod, chown, find, and tar. These commands can all use either relative paths (like ./file.txt or ../folder/) or absolute paths (like /home/user/file.txt). You will learn more about these commands in later sections.

Absolute Path

An absolute path is the complete path from the root directory to the desired directory or file. It always starts with a forward slash /, which represents the root of the file system.

# Examples of absolute paths (using your home directory and setup):
cd ~/playground/chapter1
cd ~/playground/chapter2
cd /var/log
cd /usr/local/bin

These commands use absolute paths to navigate to specific locations, regardless of where you currently are in the file system. It's like saying "go to 123 Main Street" - it works from anywhere!

Relative Path

A relative path specifies a location relative to your current directory. It does not start with a forward slash /. Think of it as giving directions from where you are standing.

# If you're in ~ (your home directory):
cd playground/chapter1     # Goes to ~/playground/chapter1
cd ../chapter2             # Goes to ~/playground/chapter2 (from chapter1)
cd ../../playground/chapter2   # From playground/chapter1, goes to ~/playground/chapter2
cd ./playground/chapter1   # From ~, goes to ~/playground/chapter1

Special characters in relative paths:

Real-world analogy: If you're in your living room and want to go to your bedroom, you could either:

Both methods work, but relative paths are often shorter and more convenient when you're already close to your destination!

Common Path Shortcuts

Here is a table of some of the most common path shortcuts you can use with the cd command and their descriptions:

Shortcut Description
.. or ../ Move up one directory level (parent directory)
~ Change to your home directory. Important: While ~ takes you directly to your personal home directory (like /home/username), /home takes you to the main directory that contains all users' home folders. When you log in, you're automatically taken to your home directory, which is why ~ is the default shortcut for it. Don't ever go to /home unless you're a system administrator.
- Change to the previous directory
/ Change to the root directory
. or ./ Stay in the current directory

Common Examples

Here are some practical examples of using the cd command:

Basic Navigation
# Change to chapter 1 playground (created by setup)
cd ~/playground/chapter1

# Move up one directory level (e.g. from chapter1 to playground)
cd ..

# Change to home directory
cd ~

# Change to root directory
cd /

# Return to previous directory
cd -

# Stay in current directory
cd .
Combining Commands
# Change directory and verify location (using setup paths)
cd ~/playground/chapter1 && pwd

# Navigate up two levels (e.g. from chapter1 to home)
cd ../..

# Change to a chapter subdirectory of home
cd ~/playground/chapter2

# Change to another chapter (created by setup)
cd ~/playground/chapter3

# Navigate using environment variables
cd $HOME/playground/chapter1
Common Errors and Solutions
# Error: No such file or directory
cd nonexistent_folder
# Solution: Check spelling and verify directory exists

# Error: Permission denied
cd /root
# Solution: Use sudo or check permissions

# Error: Not a directory
cd file.txt
# Solution: Verify target is a directory, not a file

# Error: Too many arguments
cd dir1 dir2
# Solution: Use only one directory path at a time

tree Command

The tree command in Linux is used to display the directory structure in a hierarchical format, making it easier to visualize the organization of files and subdirectories. By default, it starts from the current directory and recursively lists all files and directories in a tree-like structure. Users can modify its behavior with options such as -L to limit the depth of recursion or -d to show only directories.

When to Use tree

Use tree when you need to:

Common Options

The tree command supports several options to customize its output:

Option Description
-L N Limits the depth of directory traversal to N levels
-d Displays only directories, excluding files
-a Shows all files, including hidden ones
-f Prints the full path for each file and directory
-h Displays file sizes in a human-readable format
--du Shows the cumulative disk usage of each directory
--dirsfirst Lists directories before files in the output
-v Sorts the output alphabetically

Example Usage

Here are some practical examples of using the tree command:

# Basic usage (run from ~/playground/chapter1 to see the cat practice files)
tree

# Show only directories
tree -d

# Show 2 levels deep (try from ~/playground)
tree -L 2

# Show hidden files
tree -a

# Show with full paths
tree -f

# Show with human-readable sizes
tree -h

# Show specific directory (using setup path)
tree ~/playground/chapter1

# Show with directories first
tree --dirsfirst

# Show with disk usage
tree --du

Interpreting Output

The tree command output uses specific symbols to represent the directory structure:

Sample Output

Below is a sample of what you might see when you run tree from ~/playground/chapter1 (after running the course setup). The exact files come from the setup script:

.
├── body.txt
├── debug.txt
├── file1.txt
├── file2.txt
├── footer.txt
├── header.txt
├── hello.txt
├── mixed.txt
├── special.txt
└── spaces.txt

Best Practices