How to change the shell working directory in Linux?


To change the shell working directory, we use the cd command in the Linux system.

cd (change directory) 

The cd command used to change the current working directory in the Linux/Unix operating system. In the Windows operating system for the same purpose the cd or chdir command available. The cd command also available in the EFI shell (Extensible Firmware Shell). By default, the current shell working directory is home directory.

Syntax

The general syntax of the cd command is as follows −

cd [-L| [-P [-e]] [-@]] [directory]

A brief description of options available in the cd command.

Sr.No.
Option & Description
1
-L
Force soft links to as followed
Resolve symbolic links in the directory after processing instances of ‘..’
2
-P
Use physical directory structure without soft links
3
-e
If the -P option is used, and the current shell working directory cannot be determined successfully, exit with a non-zero status
4
-@
On systems that support it, present a file with extended attributes as a directory containing the file attributes
5
--help
Display this help and exit

By default, the shell working directory is the home directory. To change working directory home to another directory, we use the cd command as shown below

$ cd shadow

Here, we changed the shell working directory to a specified shadow directory.

To change the shell working directory current working directory to home directory, we use the cd command with the ‘~’ sign as shown below.

$ cd ~

To check-in which directory currently we are, we use the pwd (print working directory) command as shown below.

$ pwd

If we are currently in-home directory then the output of the pwd command is as follows:

home/owner_name

To change the current directory to inside a directory from a directory, we use the cd command as shown below.

$ cd shadow/snow

Here, we have a directory shadow and snow is a directory inside the shadow directory. To navigate snow directory, we use shadow/snow with the cd command.

To change the current working directory to the parent directory, we use ‘..’ with the cd command as shown below.

$ cd ..

To check more information about the cd command, we use the cd command with --help option as shown below.

$ cd --help

Updated on: 30-Jun-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements