UNIX CD COMMAND WITH EXAMPLES

UNIX-CD-COMMAND-AND-EXAMPLES

cd means ‘Change Directory’ in unix. cd command is used to change the current directory in unix session.

However if you want to check the current directory before issuing cd command, you can use ‘pwd’ command to display the current directory.

Command syntax is

 cd [-L|-P] [dir]

Here command will change the current directory to dir.

Accessing Your Home Directory

If you miss to give the variable, it will take $HOME as the default directory. Even if you issue just ‘cd’ it will treat it as ‘cd $HOME’. Check the below example for the same.

[wot@unix ~] $ echo $HOME
/home/wot
[wot@unix ~] $ pwd
/home/wot/TEST/Shells
[wot@unix ~] $ cd
[wot@unix ~] $ pwd
/home/wot
[wot@unix ~] $

In the same way, you can access home directory using ‘~’ (tilda).

[wot@unix ~] $ pwd
/home/wot/TEST/Shells
[wot@unix ~] $ cd ~
[wot@unix ~] $ pwd
/home/dinesh.p
[wot@unix ~] $

Accessing Root Directory

The root directory is represented by a single slash (“/”).

To change our working directory into the root directory, we will use the command:

cd /
[wot@unix ~] $ cd /home/wot/TEST/Shells
[wot@unix ~] $ pwd
/home/wot/TEST/Shells
[wot@unix ~] $ cd /
[wot@unix ~] $ pwd
/
[wot@unix ~] $

cd Command In Action – Change Directory

If you want to change the current directory to another directory, simply give that path after cd command.

[wot@unix ~] $ pwd
/home/wot
[wot@unix ~] $ cd /home/wot/TEST/Shells
[wot@unix ~] $ pwd
/home/wot/TEST/Shells
[wot@unix ~] $

The -P option says to use the physical directory structure instead of following symbolic links and the -L option forces symbolic links to be followed.

An argument of ‘-’ is equivalent to $OLDPWD. If you pass ‘-’ as argument to cd command, it prints the previous working directory to the standard output.

If the directory change is successful, the absolute pathname of the new working directory is written to the standard output. The return value is true if the directory was successfully changed, false otherwise.

[wot@unix ~] $ pwd
/home/wot
[wot@unix ~] $ cd /home/wot/TEST/Shells
[wot@unix ~] $ pwd
/home/wot/TEST/Shells
[wot@unix ~] $ echo $OLDPWD
/home/wot
[wot@unix ~] $ cd –
/home/wot
[wot@unix ~] $

The return value is true if the directory was changed successfully, false otherwise. You can see the below example i.e. 0 for success and 1 for failure.

[wot@unix ~] $ pwd
/home/wot
[wot@unix ~] $ cd /home/wot/TEST/Shells
[wot@unix ~] $ echo $?
0
[wot@unix ~] $ cd /hello/nodir
-bash: cd: /hello/nodir: No such file or directory
[wot@unix ~] $ echo $?
1
[wot@unix ~] $

Accessing The Current Working Directory

The current directory, regardless of which directory it is, is represented by a single dot (“.”).
So, running this command:

cd .

will change into the directory we’re already working on. In simple words, it won’t do anything.

Accessing The Parrent Working Directory

The parent directory of the current directory i.e. the directory one level up from the current directory, which contains the directory we’re in now is represented by two dots (“..”).

So, If we were in the directory /home/username/downloads, and we executed the below command:

cd ..

We would be placed in the directory /home/username.

The double-dot (“..”) directory notation can be used anywhere in a directory name to represent going up one level.

For instance, if we have two directories, /home/username/downloads and /home/username/docs, and we are currently in /home/username/downloads, If we type:

cd ../docs

and we would be navigated to /home/username/docs.

Comments

comments

Leave a Reply