How to change directory in Linux using cd command

last updated in Categories , , , ,

I recently made the switch and started using Debian Linux. How do I change directory in Linux using the cd command? How can I change a directory or folder in Linux?

Introduction – You need to use the cd command to move from your present directory to another directory. You must have execute or search permission in the specified directory. This page shows basic examples of how to change directory on Linux using the cd command.

How to change directory in Linux

The procedure changes a directory or folder in Linux as follows:

  1. Let us change to your home directory in Linux, run: cd
  2. If you want to change to the /etc/security/ directory on Linux, execute: cd /etc/security/
  3. Want to go up one level of the directory tree in Linux? Try: cd ..

Let us see some examples and usage in details.

Changes the current directory in Linux

Let us change the current working directory to the home (login) directory in Linux, run:
cd
To print the current working directory, use the pwd command:
pwd
Next change to an arbitrary directory named /etc/ufw/, type:
cd /etc/ufw
pwd

At this stage, you may want to list the directory contents. Hence, try the ls command:
ls
ls -l

Say you need to go down one level of the directory tree (say you want to change to the applications.d), run:
cd applications.d
pwd
ls -l

How do I go back to my home folder or directory in Linux?

Simply type any one of the following command:
cd
OR
cd ~
OR
cd $HOME

Change directory in Linux using cd command
Changing to another directory in Linux using the cd command

How to change a folder in Linux

Get list of directories only in the current directory using the following:
ls -d */
Once you know the directory names, change it as per your needs:
cd linux
pwd
ls

To move back to a parent directory directory, type:
cd ..
Verify it:
pwd
ls

Linux change directory using cd command
Linux change directory using the cd command

Linux change directory and symbolic links

One can force symbolic links to be followed. The syntax is:
cd -L dir1
cd -L link2

To use the physical directory structure without following symbolic links:
cd -P link2

Linux change directory and symbolic links
Force symbolic links with the -L option and he physical directory structure with the -P option

Conclusion

This page explained the cd command that allows you to change directories. For more info type the following command at the terminal:
cd --help
OR
help cd

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

4 comment

    1. Yes, the cd is an internal command. Check your shell documentation. Regardless of operating system, the syntax remains same across Linux, *BSD, macOS and Unix-like system
      cd dir
      cd ..
      cd /path/to/dir/

    2. Girl, learn to read the man pages. You can thank me later.
      type -a cd
      command cd
      man bash
      man ksh
      man csh
      help command
      help type

  1. That is all we need:
    help cd
    You see full info:

    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.

    Still, have a question? Get help on our forum!