LinuxCommandLibrary

cd

Change the current working directory

TLDR

Go to the specified directory

$ cd [path/to/directory]
copy

Go up to the parent of the current directory
$ cd ..
copy

Go to the home directory of the current user
$ cd
copy

Go to the home directory of the specified user
$ cd ~[username]
copy

Go to the previously chosen directory
$ cd -
copy

Go to the root directory
$ cd /
copy

SYNOPSIS

cd [OPTIONS] [DIRECTORY]

PARAMETERS

DIRECTORY
    The path to the target directory. This can be an absolute path (starting from root '/') or a relative path (from the current directory).

(no argument)
    Changes to the user's home directory (e.g., /home/username).

~
    Also changes to the user's home directory. This is a common shell expansion for the home directory path.

-
    Changes to the previous working directory, stored in the OLDPWD environment variable. This is useful for toggling between two directories.

..
    Moves up one level in the directory hierarchy (to the parent directory).

.
    Refers to the current directory. Using cd . effectively does nothing, remaining in the current directory.

-L
    (Logical) Follows symbolic links. This is the default behavior. When encountering a symbolic link, the new directory path will reflect the linked path, not the physical path it points to.

-P
    (Physical) Resolves symbolic links. When encountering a symbolic link, cd -P will resolve it to its physical location, ignoring the symbolic link itself. This updates the PWD variable to the canonical physical path.

DESCRIPTION

The cd (change directory) command is a fundamental utility in Linux and Unix-like operating systems, used to navigate the file system.

It modifies the shell's current working directory, allowing users to move between different directories and access their contents. When you execute cd, it changes the internal state of your shell process, updating environment variables like PWD (current working directory) and OLDPWD (previous working directory).

Unlike many other commands, cd is almost always a shell built-in, meaning it's executed directly by your shell (e.g., Bash, Zsh) rather than being a separate executable program. This is crucial because a separate program cannot change the working directory of its parent process (your shell). Its simplicity and constant usage make it one of the most frequently typed commands in any terminal session.

CAVEATS

cd is almost universally a shell built-in. This means `which cd` will typically not find an executable, and it runs directly within your current shell process. This is essential for it to change the working directory of that specific shell session.

To successfully change into a directory, you need execute permission on all directories in the path leading to the target directory. You also need read permission on the target directory if you intend to list its contents using `ls`.

ENVIRONMENT VARIABLES

The cd command manipulates two important environment variables: PWD and OLDPWD. PWD always holds the absolute path of the current working directory, and OLDPWD stores the path of the directory you were in immediately before the last cd command. These variables are often used by other commands and scripts for path management.

TAB COMPLETION

Modern shells offer excellent tab completion for cd. By typing `cd` followed by a few letters of a directory name and pressing the Tab key, the shell will attempt to complete the directory name, making navigation much faster and less prone to typing errors.

HISTORY

The cd command has been a staple of Unix-like operating systems since their inception in the 1970s. Its fundamental role in file system navigation has ensured its consistent presence and behavior across various Unix versions and subsequent Linux distributions. Being a shell built-in from very early on was a critical design choice to enable its functionality effectively within a user's interactive session.

SEE ALSO

pwd(1), ls(1), mkdir(1), rmdir(1)

Copied to clipboard