When a command reads the wrong file or a relative path fails, the problem may be your current directory. The shell keeps track of where you are, and cd changes that location.
Start with these navigation commands
pwd # Show your current directory
cd /var/log # Go to an absolute path
cd .. # Go up one level
cd # Go to your home directory
cd - # Return to your previous directory
pwd is especially useful before running commands that use relative paths. If you’re unsure where you are, check first instead of guessing.
For example, if pwd prints /home/sam/projects/app, then:
cd src
tries to enter /home/sam/projects/app/src. The path src is relative: the shell looks for it under your current directory.
Absolute paths vs. relative paths
An absolute path starts at the filesystem root, /. It points to the same location regardless of where you currently are:
cd /etc/ssh
A relative path starts from your current directory. It can name a directory directly or use special path components:
-
.means the current directory. -
..means the parent directory. -
../..means the parent’s parent.
Suppose your current directory is /home/sam/projects/app/src. Running cd ../.. moves first to /home/sam/projects/app, then to /home/sam/projects.
At the filesystem root, /, going to the parent stays at /.
A handy pattern when switching between two places is cd -. It takes you to the directory you were in immediately before your current one. Run it again and you switch back. In common shells, it also prints the destination, so you can see where you landed.
Paths with spaces
The shell treats spaces as separators between arguments. If a directory is named Project Files, pass its name as one argument by quoting it or escaping the space:
cd "Project Files"
cd Project\ Files
Without either form, the shell splits the name into two arguments, and cd won’t interpret it as the intended path.
This applies to absolute paths too:
cd "/home/sam/Project Files"
When you’re unsure of the exact spelling, list the directory contents and check the path. Linux filenames are case-sensitive: Reports and reports may refer to different directories. If the destination doesn’t exist yet, learn how to create it with mkdir.
Why cd needs to run in your shell
cd is a shell builtin, not an ordinary standalone program. That distinction explains why you type it at the prompt: a child process cannot change the working directory of the shell that launched it. If an external program changed its own directory, that change would disappear when the program exited.
It also matters in scripts and command chains. To make later commands run from a new location, cd must execute in the same shell context as those commands. For example:
cd /var/log
pwd
The pwd command runs after the directory change in the current shell. After a successful change, pwd should show /var/log.
When cd fails
If you see an error such as “No such file or directory” or “Permission denied,” check the path before reaching for elevated privileges:
-
Confirm your current location. Run
pwd. Relative paths start there, not necessarily where you expect. - Check spelling and capitalization. Linux distinguishes uppercase and lowercase names.
-
Check whether the destination exists. Use
lsto inspect the current directory, or try a known absolute path. Thelscommand’s listing options can help you inspect names, including hidden entries. -
Quote spaces. Try
cd "Project Files"or escape the space. - Check directory access. Traversing a directory requires search (execute) permission on the directories in the path.
Don’t use sudo cd as a routine fix. Because cd changes the current shell’s directory, running it as a separate privileged command won’t change the location of your existing shell. First confirm the path and whether your user should have access.
A small workflow for remote servers
The same habits help when you’re working in an SSH session: check where the shell starts, move to the intended directory, and verify before using relative paths.
pwd
cd /var/log
pwd
Once you’re in the right place, a relative path such as nginx is interpreted under /var/log. If a command behaves as if files are missing, check pwd before assuming the files or permissions are the problem.
Keep the basic pattern in mind: use absolute paths when you want an unambiguous destination, relative paths when navigating from your current location, and pwd to verify where you ended up.
I originally published a more detailed version of this guide on the SSHFlow blog.
I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.
Top comments (0)