The cd command in Linux: complete guide for browsing between directories

Introduction to command cd

The commandcd(change directory) is one of the most fundamental tools that any Linux user finds when working on the terminal. Its purpose is to change the current work directory, allowing you to browse the system's file tree without the need for a graphical interface. Although its syntax seems simple, master its variants and understand how it interprets the routes is essential to work efficiently, avoid errors and maximize the power of the command line.

Basic syntax and simple examples

The most elementary form iscd rutawhererutacan be a directory name relative to the current or an absolute route that starts with/. If no route is indicated,cdwithout arguments takes the user to the home directory defined by the variable$HOME. On the other hand,cd -alternates between the current directory and the last one visited, which is useful when you need to move back and forth between two locations.

Absolute routes versus relative routes

An absolute route starts from the root of the file system (/) and specifies the full path to the destination, for examplecd /var/log/apache2. This method is independent of the current directory and always leads to the same place. In contrast, a relative route is interpreted from the directory where we are; for example, if we are in/home/usuarioAnd we runcd documentos/informesThe shell will look/home/usuario/documentos/informes. Understanding the difference prevents confusion when working with deep directories structures.

Use of the symbols. And...

The simple point (.) represents the current directory, while the double point (..) refers to the parent directory. These symbols allow to build relative routes without the need to write the full name. For example, from/etc/nginx/conf.dcommandcd ..takes us to/etc/nginxandcd ../..takes us to/etc. Also,cd ./scriptsis equivalent tocd scriptswhen the subdirectoryscriptsIt's inside the current one.

The tilde ~ and environment variables

The type (~) is a shortcut that expands to the user's home directory ($HOME). So,cd ~/Descargasis identical tocd /home/usuario/Descargas. In addition, it is possible to use any environment variable containing a route, for example, if we definePROYECTO=/opt/miappthencd $PROYECTOHe takes us directly there. This feature is very useful in scripts and repetitive workflows.

Change to home directory and back with cd -

Runcdwithout arguments orcd ~takes the user to his personal folder, which is practical to start a new working session from a known point. The commandcd -, on the other hand, it changes to the last directory that was held before the current one and shows its route on the standard output. Alternate between two directories withcd -It saves time when comparing files, reviewing logs or compiling code in different branches of a project.

Tricks, good practices and useful shortcuts

  • Use the keyTabto self-complete directory names and reduce writing errors.
  • Combinecdwith&&to chain commands, for examplecd /var/www && ls -l.
  • In scripts, prefer absolute routes or well-defined variables to prevent the behavior from depending on the working directory of the user who runs it.
  • If you need to move several levels up, usecd ../../..or considerpushdandpopdfor directory pile.
  • Check that the directory exists before changing to it with a test like[ -d 'ruta' ] && cd 'ruta'or usingcd -Pto follow symbolic links physically.

Common errors and how to fix them

One of the most frequent failures is to try to access a directory that does not exist, which generates the messageNo such file or directory. In these cases, check the spelling, check that the route is correct and make sure that no intermediate component is missing. Another typical error arises when using relative routes without realizing that the current directory has changed, leading to unexpected locations; usepwdbeforecdhelps to confirm the starting point. Finally, remember thatcdonly affects the current shell; if you run it within a script or a subsshell, the change does not persist once the process ends.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish