Thursday 17 May 2018

Change Directory (cd) Examples | Unix and Linux Command

Unix and Linux Command, LPI Tutorials and Materials, LPI Guides, LPI Learning

The Change directory (cd) command is one of the simple commands in Unix (or Linux) and it is very easy to use. The cd command is used to change from the current directory to another directory. The syntax of cd command is

cd [directory]
Here directory is the name of the directory where you wish to go.

CD Command Examples


1. Write a unix/linux cd command to change to home directory?


Just simply type cd command on the unix terminal and then press the enter key. This will change your directory to home directory.

> pwd
/usr/local/bin

Now i am in the /usr/local/bin directory. After typing the cd command and unix window, you will go to your home directory.

> cd
> pwd
/home/matt

Here pwd command displays the present working directory.

2. Write a unix/linux cd command to go back to one directory?


The cd .. changes the directory to its parent directory by going back one level. The space between the cd and .. is must.

> pwd
/var/tmp
> cd ..
> pwd
/var

3. Write a unix/linux cd command to go back to two directories?


The cd ../../ takes you back to two directories. You can extend this cd command to go back to n number of directories.

> pwd
/usr/local/bin
> cd ../../
> pwd
/usr

4. Write a unix/linux cd command to change the directory using the absolute path?


In case of changing directory using absolute path you have to specify the full directory path. Absolute path directories always start with a slash (/). An example is changing your directory to /usr/bin from your home directory.

> cd /usr/bin

5. Write a unix/linux cd command to change the directory using the relative path?


In relative path, you have to specify the directory path relative to your current directory. For example, you are in /var/tmp directory and you want to go to /var/lib directory, then you can use the relative path.

> pwd
/var/tmp
> cd ../lib
> pwd
/var/lib

Here the cd ../lib, first takes you to the parent directory which is /var and then changes the directory to the lib.

6. Write a unix/linux cd command to change back to previous directory.


As an example, i am in the directory /home/matt/documents and i changed to a new directory /home/matt/backup. Now i want to go back to my previous directory /home/matt/documents. In this case, you can use the cd - command to go back to the previous directory.

> pwd
/home/matt/documents
> cd /home/matt/backup
>pwd
/home/matt/backup
> cd -
> pwd
/home/matt/documents

Related Posts

0 comments:

Post a Comment