totn UNIX

UNIX Basic commands: cd

Description

The change directory (cd) command is built into the system shell and changes the current working directory. The cd command can be used to either change to a directory that is relative to the the location of the current working directory or to an absolute location in the filesystem.

Syntax

The syntax for the cd command is:

cd <directory>

Parameters or Aruguments

directory
The relative or absolute path of the directory to change to.
NOTE: There are many flavors of UNIX and system shells. If you experience an incompatibility with your cd command, please consult the cd manual page (see man command) on your system. This reference assumes that you have permission to enter the directory that you are attempting to change into.

Applies To

  • Most UNIX variants including: FreeBSD, OpenBSD, Solaris, Illumos, SmartOS

Type of Command

  • System Shell

Example - Use the cd command to change to a relative directory location

In this example we will use the shell cd command to change to a directory that is relative to the directory we are currently in.

To start, we will assume that our current working directory is the /home/example directory and that we will be changing to the existing /home/example/pictures directory. (If the /home/example/pictures directory does not exist you will need to refer to the mkdir command to create it.)

To use the cd command to change to the /home/example/pictures directory you will use the following cd command:

cd pictures

Some systems may not display the current or full working directory to the user on the command prompt. In these cases we will need to use the pwd command to view the path of the current working directory we are in.

pwd

The following screenshot shows that we successfully changed our working directory from /home/example to /home/example/pictures.

cd command example #1

Example - Use the cd command to change to an absolute directory location

In this example we will use the shell cd command to change to a directory using an absolute directory path.

To begin, we will assume we are currently located in the /home/example/pictures directory and will be changing to the /home/example directory.

To use the cd command to change to the /home/example directory you will use the following cd command:

cd /home/example

In the following screenshot you can see that the working directory becomes /home/example after the cd command is run.

cd command example #2