How to Change Direcotry in Linux

This post will guide you how to change directories in Linux Operating systems using the cd command. How do I change the current directory to a particular directory in the Linux terminal.

Cd Command


Cd command can be used to change the current directory to DIR. The default DIR is the value of the HOME shell variable.

The Syntax of the cd command is as followss:

cd [ directory]

Print Name of the Current Working Directory


If you want to print the full filename of the current working directory, and you can use the pwd command, type:

$ pwd

outputs:

devops@devops:~$ pwd
/home/devops

Change Direcotry


If you want to change to the current working directory to the /etc/ direcotry on your Linux, and you can type the following cd command:

$ cd /etc/

Outputs:

devops@devops:~$ cd /etc/
devops@devops:/etc$

You can print the name of the current working directory by issuing the following command:

$ pwd

outputs:

devops@devops:/etc$ pwd
/etc

Change the Current Directory to the Home Directory


If you want to change the current working diectory to the Home direcotry in your Linux, and you can use one of the following commands:

$ cd

or

$ cd ~

Outputs:

devops@devops:/etc$ pwd
/etc
devops@devops:/etc$ cd ~
devops@devops:~$ pwd
/home/devops

If you want to know more information about the cd command, just type the following command:

$ cd --help

Outputs:

devops@devops:~$ cd --help
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.

Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.

The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.

If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.

Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of `..'
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of `..'
-e if the -P option is supplied, and the current working
directory cannot be determined successfully, exit with
a non-zero status
-@ on systems that support it, present a file with extended
attributes as a directory containing the file attributes

The default is to follow symbolic links, as if `-L' were specified.
`..' is processed by removing the immediately previous pathname component
back to a slash or the beginning of DIR.

Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.

Conclusion


You should know that how to use cd command to change the current working directory to a given directory in CentOS or RHEL or Ubuntu Linux server.

You might also like:

Sidebar



back to top