Javatpoint Logo
Javatpoint Logo

cd Command in Linux/Unix | Linux Change Directory

Linux cd command is used to change the current working directory ( i.e., in which the current user is working). The "cd" stands for 'change directory.' It is one of the most frequently used commands in the Linux terminal.

Syntax:

It is one of the most important and common commands in the Linux system and will be used repeatedly. With the help of this command, we can move all over our directories in our system. We can go to our previous directory or previous to the next directory, or anywhere.

Implementation of cd Command

The command is implemented in several operating systems like AmigaOS, MetaComCo TRIPOS, IBM OS/2, DOS, Unix, Linux, ReactOS, and Microsoft Windows. It is available in the two and later versions on MS-DOS. Also, the DR DOS 6.0 contains an implementation of the chdir and cd commands. Also, the command is inside the open-source MS-DOS emulator DOSbox and EFI shell. In HP MPE/iX, it is titled the chdir. The command is comparable to the Stratus OpenVOS change_current_dir command.

  • The cd command is frequently contained and built directly into the command-line interpreter.
  • It is the situation in almost every Unix shell (Bash, Bourne shell, tcsh, etc.), Windows PowerShell on Windows 7+, cmd.exe ok Microsoft Windows NT/2000+, and COMon DOS/ Microsoft Windows 3.x-9x/ME.
  • The chdir command, defined by POSIX, is the most affected command by system call in almost every operating system.
  • On Windows, command line shells utilize the Windows API to modify the current working directory.
  • On the other hand, the cd command calls the chdir POSIX C function on Unix systems.
  • It means that if the command is run, no new process is established for migrating to the other directories, as in the situation with other commands like ls.

Rather, the shell itself runs this command. It is because when a new process is established, the child process acquires the directory where the parent process was established. If the cd command acquires the process of the parent directory, the cd command's objective will never be completed.

Windows PowerShell, the scripting language and object-oriented command line shell of Microsoft, runs the cd command in the process of the shell. However, every cmdlets of PowerShell, such as rm, ls, etc., execute in the process of the shell because PowerShell is .NET Framework-based and has a unique architecture than the older shells.

Usage of cd Command

A directory can be defined as a logical part of a file system used for holding files. Also, directories may include other directories. Cd can be used to modify into a subdirectory, return to the parent directory, move every way behind the root directory, or move to a given directory.

  • DOS manages an isolated working directory for all lettered drives and has the format of a current working drive.
  • Cd can be used to modify the working directory of another lettered drive or working drive.
  • As a command, entering the drive letter on its own modifies the working drive; alternatively, cd using the /d option may be used to modify the working drive and the working directory of that drive in a single step.
  • Modern Windows versions simulate this nature for backward compatibility in EXE.
  • Remember that running the cd command from the terminal without arguments has distinct effects in different OSes.
  • For instance, if cd is run in DOS, Windows, or OS/2 without arguments, the current working directory is shown. If cd is run in Unix without arguments, the user will return to the home directory.

Running the cd command within a batch or script file also has distinct effects in different OSes. The current directory of the caller can be directly edited by the use of the batch file of this command in DOS. The current directory of the caller is not edited by the invocation of the script of the cd command in Unix. It is because the script is usually run within a subshell in Unix.

Options of cd Command

Unix-like, Unix

  • cd ~ or cd by itself will always bring the user to the home directory.
  • cd . will allow the user to stay in the similar directory they are in currently.
  • cd ~username will allow the user to stay in the home directory of the username.
  • cd dir (not with /) will allow the user to stay in a subdirectory.
  • cd .. will bring the user above one directory.
  • cd - will change the user to the old directory.

ReactOS, Windows, OS/2, DOS

No attributes show the current directory's full path.

  • -p: It prints the ultimate directory stack, like dirs.
  • -v: In this option, entries are displayed a single per line, anticipated by the stack positions.
  • -n: In this option, entries are covered before they grasp the screen's edge.
  • cd\ (Windows and DOS only) is used to return to the root directory. The cd\subdir command always consequently takes the user to the titled subdirectory in the root directory, despite where they are placed when the command is authorized.

As we are familiar with Linux directories, so, we will perform the following cd operations on directories:

  1. Change from the current directory to a new directory
  2. Change directory using an absolute path
  3. Change directory using the relative path
  4. Change to the home directory
  5. Change to the previous directory
  6. Change to Parent Directory
  7. Change to the root directory
  8. Change to another user's home directory
  9. Change to Directory having Spaces
  10. Change up to multiple Sub Directories

1) Change from the current directory to a new directory

We can change our directory from the current working directory to a specified directory. To display the current working directory, execute the command as follows:

To change our current working directory, execute the command as follows:

Consider the below output:

CD Command

From the above output, we have executed the pwd command to display the current working directory, which is "/home/sssit." Then, we have executed the 'cd' command to change our current directory and mentioned the path for the new directory as "/home/sssit/Desktop." As we can see in the output image, we are on our new directory that is Desktop. Therefore, our current working directory has changed to Desktop.

2) Change directory using an absolute path

To change the directory by using an absolute path, we have to mention the whole path starting from the root. Consider the below example:

CD Command2

From the above output, we are changing our directory to 'certs' from 'cups.' So, we have provided the whole path "/run/cups/certs" starting from the root (/). This is called an absolute path.

3) Change directory using a relative path

We can change our directory by using a relative path; a relative path is a location that is relative to the current directory. Consider the below example:


CD Command

From the above output, we are changing the directory by using a relative path. Like the above example, here too, we have changed our directory from 'cups' to 'certs,' but have not mentioned the whole path. This is the relative path.

4) Change to the home directory

To change the directory to home directory from the current working directory, execute the command as follows:

The above command will bring us to our home directory. Consider the below output:

CD Command

As we can see from the above output, we were on the Downloads directory, and the "cd ~" command has brought us to our home directory.

5) Change to the previous directory

To change to the previous directory from the current working directory, execute the command as follows:

Consider the below output:

CD Command

As we can see from the above output, we were on the "/Downloads/akash" directory. And, by executing the "cd -" command, our current working directory has been changed to its previous directory, i.e."/Downloads."

6) Change to Parent Directory

To change the directory to parent directory of the current working directory, execute the command as follows:

The above command will bring us to the parent directory of the current working directory. Consider the below output:

CD Command

As we can see from the above output, the directory 'akash' has been changed to its parent directory 'Downloads.'

7) Change to the root directory

To navigate directory to entire system's root directory from current working directory, execute the command as follows:

Consider the below output:

CD Command

From the above output, the current working directory has changed to the system's root directory.

8) Change to another user's home directory

We can change the directory from the current working directory to a user's home directory by executing the command as follows:

Consider the below output:

CD Command

From the above output, we have changed the directory to the home directory of the user 'javatpoint'.

9) Change to Directory having Spaces

To change the directory that has spaces in its name, surround the path with quotes (' ') or use the backslash (\) character with it. Execute the command as follows:

The above commands will ignore the space from the directory name. Consider the below output:

CD Command

10) Change up to multiple Sub Directories

We can change directory up to multiple sub-directories by separating directories by a forward slash (/) as follows:

Consider the below output:

CD Command





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA