WebMasterCampus
WEB DEVELOPER Resources

Linux cd Command

Learn Linux cd Command with example


Linux cd Command

In Linux we use “cd” command to change directory.

cd command Syntax

>> cd <current directory> <specified directory>

# directory name either with relative or it can be complete absolute path

To change directory with cd, we have two possibility either use absolute path or relative path names. The absolute path or full path starts from the system root /, and the relative path starts from your current directory.

cd ~ command to user Home Directory

>> cd ~

# Move to session user home directory

By default, when you log into your Linux system, your current working directory is set to your home directory.

cd /bin Command to Move root level bin Directory

>> cd /bin

# Move directly to root level bin directory

cd .. Command to Parent Directory

>> cd ..

# Move to one level back in directory heirarchy or parent directory.
>> cd ../../

# Move to one level back in directory heirarchy or parent directory.

cd - to Move to the Previous Directory

>> cd -

# Move to previous working directory

Directories with Space in Their Names

If the directory you want to change to has spaces in its name, you should either surround the path with quotes or use the backslash () character to escape the space:

>> cd 'Dir name with space'

# cd 'Dir name with space'

cd Command in Linux (Documentation)

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.     
Created with love and passion.