CD: Change Directory in Linux

How to change directories using the terminal in Linux.

1257 views

Edited: 2019-12-03 17:42

Linux tutorial

Changing the directory in Linux can be done with the CD command. The command is the same as that in DOS, and also works with relative paths.

To change to your www directory, if you are running a web server, you can simply type:

cd /var/www

When you are logged in as your user, the default directory will be your home directory. To move back to home after changing directory, you can simply type either of the below commands:

cd
cd ~
cd /home/MyUser

If your location is in some subdirectory, you may also use relative path to change directory. For instance, assuming your current location is /var/www/MySite, using the below relative path would change your location to /var/log:

cd ../../log

If you attempt to change to a directory you do not have permissions to view, you will get an error like the below:

bash: cd: /root: Permission denied

To avoid this error you need to use sudo or su, alternatively you can also change to the root user.

CD reference

CommandExplanation
cdMoves you to the Login directory (/home/MyUser).
cd ~Login Directory (/home/MyUser)
cd /home/MyUserChanges directory using absolute path.
cd ../../homeChanges directory using relative path.

See also: Absolute and Relative Paths

Tell us what you think:

  1. Understanding file permissions in Unix / Linux based systems, and how to make files immutable.
  2. How to search a directory and subdirectories for a given string of text in Linux with the grep command.
  3. Worth knowing in order to make a bootable USB memory stick with Windows on from Linux.
  4. This is why I decided to encrypt my new Flash Drive, and why I think you should too.
  5. About the problem with using sudo with graphical programs in Linux.

More in: Linux Tutorials