3

We know that the cd command is used to change directories. For example

 cd dirname 

valid dirnames include . and .. as well. The purpose of cd .. is clear. From the manpage:

A null directory name in CDPATH is the same as the current directory, i.e., ''.''.

But then what is the purpose of cd .?

1

2 Answers 2

6

The effect of cd . is to update the shell with information about what the current working directory is. This could be useful in circumstances when the (pathname of the) current directory changes by other means than through cd.

$ pwd /tmp/shell-bash.OoVntBAM $ mkdir dir $ cd dir $ pwd /tmp/shell-bash.OoVntBAM/dir 

So, we're currently some place under /tmp, in a directory called dir. Let's rename the directory we're currently in.

$ mv ../dir ../new-dir 

So, where are we now?

$ pwd /tmp/shell-bash.OoVntBAM/dir 

Really? Why, that's odd. Let's make sure the shell knows were we are.

$ cd . $ pwd /tmp/shell-bash.OoVntBAM/new-dir 

In a script, cd . could possibly be used to test whether the user still as access to the current directory:

if ! cd . 2>/dev/null; then echo lost access to cwd >&2 exit 1 fi 

Or, the other way around, to verify that the current directory is no longer accessible after some sort of operation to revoke access for the current user.

Reasons for loosing access to the current directory includes having the directory removed, or someone removing one's execute permissions on it.


Another example is using cd ., but with the -P option, i.e. cd -P .. This would put you in the "physical" directory if the current directory was otherwise accessed through a symbolic link:

$ pwd /tmp/shell-bash.OoVntBAM $ mkdir dir $ ln -s dir link-dir $ ls -l total 2 drwxr-xr-x 3 kk wheel 512 May 8 00:48 dir lrwxr-xr-x 1 kk wheel 3 May 8 00:46 link-dir -> dir 

So now we have a directory, dir, and a symbolic link to that directory. Let's enter the directory through the symbolic link:

$ cd link-dir $ pwd /tmp/shell-bash.OoVntBAM/link-dir 

Then...

$ cd -P . $ pwd /tmp/shell-bash.OoVntBAM/dir 
0

The correct quote for cd .. should be:

`..' is processed by removing the immediately previous pathname component back to a slash or the beginning of DIR.

And, yes, cd . shouldn't change the current directory, and, yes it doesn't.
But the name of the present directory might have changed:

$ mkdir thisdir $ cd thisdir $ cd . $ pwd /home/isaac/thisdir $ mv ../thisdir ../xxxx $ cd . $ pwd /home/isaac/xxxx 

Or the present directory contains a symlink or its physical directory name has changed.

$ cd ~/ttt $ pwd /home/isaac/ttt $ cd -P . $ pwd /home/isaac/me/init/basicdirs.d/ttt 

So, cd . is used as any other cd DIR, to change the pwd to DIR. It may appear to be the same directory, but, even being the same directory, DIR might have a different name or path.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.