Strange behaviour of the "cd" command

Karl Auer kauer at biplane.com.au
Wed Jul 12 12:04:54 UTC 2017


On Wed, 2017-07-12 at 12:09 +0200, Dr Rainer Woitok wrote:
> By pure chance  I meanwhile happened to find a workaround.  Slightly
> changing your last two commands above yields
> 
>    $ cd ../..//.three
>    $ pwd
>    /tmp/.three

I'll see that and raise you these (skip to the last eight lines of this
sequence for the finale(s)):

$ mkdir dottest                   
$ cd dottest
$ mkdir .one
$ cd .one
$ pwd
/home/kauer/temp/dottest/.one
$ mkdir .two
$ cd .two
$ pwd
/home/kauer/temp/dottest/.one/.two
$ mkdir .three
$ cd .three
$ pwd
/home/kauer/temp/dottest/.one/.two/.three
$ touch ../../../.one/fred
$ ls ../../../.one/fred
../../../.one/fred
$ ls -la ..
total 12
drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 .
drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 ..
drwxrwxr-x 2 kauer kauer 4096 Jul 12 21:24 .three
$ ls -la ../..
total 12
drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 .
drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 ..
drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 .two
$ ls -la ../../..
total 12
drwxrwxr-x  3 kauer kauer 4096 Jul 12 21:24 .
drwxr-xr-x 22 kauer kauer 4096 Jul 12 21:24 ..
drwxrwxr-x  3 kauer kauer 4096 Jul 12 21:24 .one
$ cd ../../../.one
ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
$ cd ../../../\.one
ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
$ cd ../../../.one
ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
$ cd ../../..//.one
$ pwd
/home/kauer/temp/dottest/.one
$ cd .two/.three
$ pwd
/home/kauer/temp/dottest/.one/.two/.three
$ cd "../../../.one"  
ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
$ cd '../../../.one'
ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
$ cd ../../../..one
ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
$ cd ../../../...one
$ pwd
/home/kauer/temp/dottest/.one
$ cd .two
$ cd .three
$ cd /home/kauer/temp/dottest/.one
$ pwd
/home/kauer/temp/dottest/.one

So a double slash stops the dot being vanished by cd, starting at the
root does too, and three dots will be interpreted as one dot, but NOT,
further experimentation reveals, in all situations.

This is definitely violating the Principle of Least Astonishment in a
big way. I wonder if in the Korn Shell World (a strange and wondrous
place, little visited by me) there is a Grand Unifying Theory of Dots
that makes this all make sense.

This is from the ksh man page under "File Name Generation":

   If FIGNORE is set, then each file name component
   that matches the pattern defined by the value of
   FIGNORE is ignored when generating the matching
   filenames. The names . and .. are also ignored.
   If FIGNORE is not set, the character . at the
   start of each file name component will be
   ignored unless the first character of the
   pattern corresponding to this component is the
   character . itself.

It seems as if this could if we squint a lot) sorta kinda explain only
the dots in the last component of a path (the "file name") are ignored,
and why "..." is treated as ".", but it's still very weird.

Other areas of the man page indicate the ".." is special in some other
contexts too. Maybe those contexts are getting confused by cd?

This is interesting too:

$ x='cd ../../../.one'
$ echo $x         
cd ../../../.one
$ pwd
/home/kauer/temp/dottest/.one/.two/.three
$ cd $x
ksh: cd: bad substitution
$ cd "$x"
ksh: cd: cd ../../../.one: [No such file or directory]

Regards, K.
 
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer
http://twitter.com/kauer389

GPG fingerprint: A52E F6B9 708B 51C4 85E6 1634 0571 ADF9 3C1C 6A3A
Old fingerprint: E00D 64ED 9C6A 8605 21E0 0ED0 EE64 2BEE CBCB C38B






More information about the ubuntu-users mailing list