The cd command in Linux: a complete guide to navigating between directories

Introduction to the cd command

The cd (change directory) command is one of the most fundamental tools any Linux user encounters when working in the terminal. Its purpose is to change the current working directory, allowing navigation through the system’s file tree without needing a graphical interface. Although its syntax appears simple, mastering its variants and understanding how it interprets paths is essential for working efficiently, avoiding errors, and making the most of the command line’s power.

Basic syntax and simple examples

The most basic form is cd path, where path can be a directory name relative to the current one or an absolute path that starts with /. If no path is given, cd without arguments takes the user to the home directory defined by the variable $HOME. On the other hand, cd - toggles between the current directory and the last one visited, which is useful when you need to move back and forth between two locations.

Absolute vs relative paths

An absolute path starts from the filesystem root (/) and specifies the full path to the destination, for example cd /var/log/apache2. This method is independent of the current directory and always leads to the same place. In contrast, a relative path is interpreted starting from the directory we are in; for example, if we are in /home/usuario and run cd documentos/informes, the shell will look for /home/usuario/documentos/informes. Understanding the difference avoids confusion when working with deep directory structures.

Using the . and .. symbols

The single dot (.) represents the current directory, while the double dot (..) refers to the parent directory. These symbols allow building relative paths without having to write the full name. For example, from /etc/nginx/conf.d the command cd .. takes us to /etc/nginx, and cd ../.. takes us to /etc. Likewise, cd ./scripts is equivalent to cd scripts when the scripts subdirectory is inside the current one.

The tilde ~ and environment variables

The tilde character (~) is a shortcut that expands to the user’s home directory ($HOME). Thus, cd ~/Downloads is identical to cd /home/user/Downloads. Moreover, any environment variable containing a path can be used; for example, if we define PROJECT=/opt/myapp, then cd $PROJECT takes us directly there. This feature is very useful in scripts and repetitive workflows.

Changing to the home directory and going back with cd –

Running cd without arguments or cd ~ takes the user to their personal folder, which is practical for starting a new work session from a known point. The command cd -, on the other hand, changes to the last directory that was active before the current one and prints its path to standard output. Toggling between two directories with cd - saves time when comparing files, reviewing logs, or compiling code in different branches of a project.

Tricks, good practices, and useful shortcuts

  • Use the Tab key to autocomplete directory names and reduce typing errors.
  • Combine cd with && to chain commands, for example cd /var/www && ls -l.
  • In scripts, prefer absolute paths or well-defined variables to avoid behavior depending on the user’s working directory who runs it.
  • If you need to move up several levels, use cd ../../.. or consider pushd and popd for a directory stack.
  • Verify that the directory exists before changing to it with a test like [ -d 'path' ] && cd 'path' or by using cd -P to follow symbolic links physically.

Common mistakes and how to fix them

One of the most frequent mistakes is trying to access a directory that does not exist, which produces the message No such file or directory. In such cases, check the spelling, verify that the path is correct, and ensure that no intermediate component is missing. Another typical error occurs when using relative paths without realizing that the current directory has changed, leading to unexpected locations; using pwd before cd helps confirm the starting point. Finally, remember that cd only affects the current shell; if you run it inside a script or a subshell, the change does not persist once the process ends.

This post is also available in ESPAÑOL.

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .