OSBash.002: Commands and Paths — pwd, ls, cd, Absolute Paths, Relative Paths, and PATH

Realistic color-pencil style laptop workspace showing Bash pwd, ls, and cd commands and a project path in a Linux terminal

Key Takeaways

  • pwd tells you where you are.
  • ls shows what is in a directory.
  • cd changes your current working directory.
  • An absolute path starts from the filesystem root. A relative path starts from your current directory.
  • . means the current directory, .. means the parent directory, and ~ commonly expands to your home directory.
  • PATH controls where Bash searches for command names that do not contain a slash.

OSBash.001 introduced Bash as a shell and scripting language, along with shebangs, executable scripts, and the meaning of ./script.sh. This lesson slows down and focuses on the navigation ideas underneath those examples: commands, directories, paths, and command lookup.

Most beginner Bash mistakes become easier to diagnose once you can answer two questions: “Where am I?” and “How is Bash finding the command I typed?” The first is about the current working directory. The second is about explicit paths and the PATH environment variable.

Engineering Digest walks through the core Linux navigation commands ls, pwd, and cd for beginners.

Your Shell Always Has a Current Working Directory

Every interactive Bash session has a current working directory. Many relative operations begin there. If you create a file named notes.txt without giving a longer path, the shell and the program you launch interpret that name relative to the current directory unless the program defines different behavior.

pwd

pwd means print working directory. A typical result might be:

/home/user/projects

The leading slash means this is an absolute path beginning at the filesystem root.

Fedora GNOME Terminal screenshot showing Bash commands including pwd, cd, ls, yum, and ping
A real Bash session in Fedora GNOME Terminal showing commands including pwd, cd, and ls. Source: Wikimedia Commons.

ls Lists Directory Contents

The basic ls command lists entries in a directory.

ls

You can also give ls a path:

ls /etc
ls ~/Downloads
ls ../

A common inspection form is ls -la. The -l option requests a long listing, while -a includes entries whose names begin with a dot. This is often useful when troubleshooting shell configuration files such as .bashrc.

cd Changes the Current Directory

cd /var/log

After that command, pwd should report /var/log. You can also move using a relative path:

cd projects
cd app
pwd

If your starting directory was /home/user, those commands move into /home/user/projects/app.

Absolute Paths Start at /

An absolute path identifies a location from the filesystem root, represented by /.

/home/user/projects/app
/etc/ssh/sshd_config
/usr/bin/bash

Absolute paths do not depend on your current working directory. If /home/user/projects/app exists, that path names the same location whether your shell is currently in /tmp, /var/log, or your home directory.

Relative Paths Start Where You Are

A relative path is interpreted from the current working directory. If pwd prints /home/user/projects, then:

cd app

means “enter /home/user/projects/app.” The shorter spelling is convenient because your current location supplies the missing beginning of the path.

Qube demonstrates ls, pwd, cd, and the difference between absolute and relative pathnames.

Three Path Shortcuts You Need Immediately

SymbolMeaningExample
.Current directory./script.sh
..Parent directorycd ..
~Home-directory expansion in common Bash usagecd ~/projects

These shortcuts are small but fundamental. ./tool explicitly names a program in the current directory. ../file.txt names a file one directory above you. ~/Downloads expands from your home directory.

cd With No Argument Goes Home

cd

In normal Bash usage, cd with no directory argument moves to your home directory. You can confirm with pwd. Bash also supports cd - to switch back to the previous working directory, which is useful when moving between two locations.

A Bash community discussion about shortening directory navigation highlights aliases, tab completion, symlinks, and directory-oriented shell habits.

Paths With Spaces Must Be Protected

Bash normally treats spaces as separators between words. If a path contains spaces, quote it or escape the spaces.

cd "My Projects"
cd My\ Projects

Quoting is usually easier to read, especially when paths come from variables. A later Bash lesson will go deeper into quoting and expansion rules.

A Command Name Is Not Always a Path

When you type a command such as:

python
ssh
git

you normally are not giving Bash a path containing a slash. Bash must decide what that command name refers to. The GNU Bash manual documents the shell’s command-search process, including functions, builtins, hashed commands, and the directories listed in PATH.

Our earlier PATH explainer covers the same idea across Windows and Linux: PATH is an ordered list of directories searched for executable command names.

Inspect PATH

echo "$PATH"

On Linux, the entries are commonly separated by colons. A value might look like:

/usr/local/bin:/usr/bin:/bin

If you type git, Bash can search those directories in order until it finds a suitable executable command.

Use command -v and type to See What Bash Will Run

command -v bash
command -v python
type cd
type ls

command -v is a useful portable shell-oriented way to ask how a command name resolves. Bash’s type builtin can tell you whether a name is an alias, function, builtin, or external executable.

The POSIX command specification defines the standardized command -v behavior used to describe how a command name would be interpreted.

Why ./script.sh Works When script.sh Does Not

If the current directory is not in PATH, typing script.sh asks Bash to search its command locations and may produce “command not found.” Typing ./script.sh is different: the slash tells Bash you supplied a path explicitly, so no ordinary PATH search is needed.

That connects directly to the execution model introduced in OSBash.001. It also connects to our fork() and exec() explainer, which describes the lower-level Unix process-launch path beneath shell command execution.

A Practical Navigation Session

pwd
ls
cd ~/projects
pwd
ls -la
cd app
pwd
cd ..
pwd
cd -
pwd

Do not just memorize the commands. Watch how the output changes after every cd. The goal is to build a mental map between the path printed by pwd and the relative paths you use next.

Troubleshoot “Command Not Found” in a Fixed Order

  1. Check the spelling of the command.
  2. Run command -v commandname.
  3. Inspect echo "$PATH".
  4. If it is a local file, ask whether you meant an explicit path such as ./tool.
  5. Run ls -l on the target path and confirm the file actually exists.
  6. If you changed directories, run pwd and verify you are where you think you are.

This sequence separates path problems from installation problems. A missing executable and a perfectly valid executable in the wrong directory can produce similar symptoms until you inspect the path deliberately.

Common Beginner Mistakes

  • Assuming ls changes directories: it only lists; cd changes location.
  • Forgetting to run pwd: many relative-path errors begin with an incorrect assumption about the current directory.
  • Confusing an absolute path with a relative path: leading / changes the meaning completely.
  • Forgetting quotes around spaces: Bash splits unquoted words on spaces.
  • Typing a local script name without ./: Bash may search PATH instead of the current directory.
  • Assuming PATH is one folder: it is an ordered list of directories.
  • Relying only on which: shell builtins and aliases can make command -v or type more informative about Bash’s own resolution.

Practice

  1. Run pwd and write down the absolute path.
  2. Use ls and ls -la and compare the outputs.
  3. Create or choose a directory one level below your current location and enter it with a relative path.
  4. Return to the parent directory with cd ...
  5. Move to your home directory with cd, then return with cd -.
  6. Use an absolute path to reach the same directory you previously reached with a relative path.
  7. Create a directory containing a space and enter it using quotes.
  8. Run command -v bash, command -v git, and type cd.
  9. Print PATH and identify the first three directories in the search order.
  10. Create a tiny executable script in the current directory and compare typing its filename alone with typing ./filename.

Knowledge Check + Answers

  1. What does pwd do? Prints the current working directory.
  2. What does cd do? Changes the shell’s current working directory.
  3. What makes a path absolute? It begins from the filesystem root, normally with /.
  4. What does .. mean? The parent directory.
  5. What does . mean? The current directory.
  6. What does ~ usually represent in Bash? A home-directory expansion.
  7. What is PATH? An ordered list of directories Bash can use when searching for external command names.
  8. Why can ./script.sh work when script.sh fails? ./script.sh explicitly supplies a path, while the bare name may rely on command search through PATH.
  9. Which commands can help show what Bash will run? command -v and Bash’s type builtin.

Elementary Review

Bash navigation is built around your current directory and the path you supply. Use pwd to orient yourself, ls to inspect directories, and cd to move. Absolute paths begin at /; relative paths begin where you are. When you type a bare command name, Bash may use PATH and its command-resolution rules to decide what to execute.

Next Bash Lesson

OSBash.003: Variables and Expansion will build on paths by showing how Bash stores values in variables and expands names such as $HOME and $PATH.

Editor’s Note

The featured image is a unique 1200×630 realistic color-pencil Bash workspace created specifically for OSBash.002 and is not reused in the body. The separate body image is a real Fedora Bash terminal screenshot from Wikimedia Commons.

BitcoinVersus.Tech content is provided for informational and educational purposes.

Leave a Reply