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 [directory]

PARAMETERS

directory
    The path to the directory to change to. Can be absolute or relative. If omitted, defaults to the user's home directory.

-
    Change to the previous working directory.

..
    Move up one level in the directory hierarchy.

DESCRIPTION

The cd command is a fundamental command-line utility used to navigate the file system in Linux and other Unix-like operating systems. Its primary function is to change the current working directory, which is the directory in which the shell (like Bash or Zsh) operates and from which relative file paths are resolved. cd simplifies navigating complex directory structures, enabling users to quickly move between different locations.

Using cd without arguments will return the user to their home directory. The user can navigate to specific directories by using its name or path. The path can be relative to the current working directory or absolute (starting with '/'). Using '..' moves the user up one level in the directory hierarchy. Correct usage of cd is crucial for effectively managing files and running commands from the intended location. It's an essential tool for any Linux user and understanding how to utilize it efficiently enhances command-line productivity. cd is often used in scripts to ensure commands are executed in the correct context.

CAVEATS

The cd command relies on the existence and accessibility of the specified directory. If the directory does not exist or the user lacks sufficient permissions to access it, the cd command will fail and return an error.

RETURN CODES

The cd command typically returns 0 on success and a non-zero value on failure. Failure can occur for various reasons, such as the directory not existing or insufficient permissions to access it.

SHELL BUILTIN

cd is typically implemented as a shell builtin command rather than a standalone executable. This allows the shell to directly manage the current working directory, ensuring that changes persist across commands executed within the same shell session.

HISTORY

The cd command is one of the oldest and most fundamental Unix commands. It has been present since the early days of Unix, providing a basic way to navigate the file system. Its core functionality has remained consistent throughout Unix and Linux history, although shells have added extensions like tab completion and automatic pushd/popd functionality.

SEE ALSO

pwd(1), ls(1), pushd(1), popd(1)

Copied to clipboard