CD: Change Directory in Linux
How to change directories using the terminal in Linux.
Edited: 2019-12-03 17:42
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
Command | Explanation |
---|---|
cd | Moves you to the Login directory (/home/MyUser). |
cd ~ | Login Directory (/home/MyUser) |
cd /home/MyUser | Changes directory using absolute path. |
cd ../../home | Changes directory using relative path. |
See also: Absolute and Relative Paths
Tell us what you think: