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