📜 ⬆️ ⬇️

Two points

screenshot of the console that rips the template

In the picture above you can see how ls thinks that linkylink / .. is not the same as the current directory. At the same time cd seems to disagree with him.

I will begin the story with all the familiar web addresses that are similar to the system paths.
')

Two points in the URI paths (on the web)


Interpretation of points is described in section 5.2.4 of RFC 3986 .
It works like this: each segment of two points destroys the previous segment:

  /a/b/c/../../g <=> / a / g 

Moreover, if there is nothing to destroy, two points are ignored:

  example.com/../../../etc/passwd <=> example.com/etc/passwd 

The rules were designed so that relative paths (../img/pic.png) can be converted to absolute prefixes from the uri context:

  1. /a/css/index.css refers to ../img/pic.png
  2. in /a/css/index.css everything is destroyed after the last slash => / a / css /
  3. ../img/pic.png is added to / a / css / => /a/css/../img/pic.png
  4. points are interpreted => /a/img/pic.png

These operations are usually done by the browser when it converts relative uri to absolute. Also, according to the standard , the operation of destroying points should be performed when uri is reduced to a canonical form — normalization — including absolute uri.

Web servers in the wild do not encounter requests containing points and each processes them in its own way.

In general, based on the normalization rule, it follows that in the uri of the form 'http://example.com/a/b/../c' 'b' it is not obliged to exist.

Two points in the shell


The cd shell command behaves in a similar way: two points always lead one segment of the path above, as if canceling the previous transition to the subdirectory. But, unlike uri, the shell checks for the existence of intermediate directories.

If you consider the first natural, then this publication is for you. In fact, the shell emulates this cd behavior: in all other places * nix ".." works differently.


Even the source command (or its synonym ".") Has excellent behavior from cd

The difference manifests itself in symbolic links to directories: for cd, the transition to such a link is reversible through "..", while the rest of the system will perceive ".." as the physical parent of the directory to which the link points.

Differently, the first behavior is called logical (-L for pwd and cd), as opposed to physical (-P).

Two points in the file system


In * nix, ".." is a real subdirectory, the only physical parent independent of symbolic links. If you look inside the ext2 file system, then the description of the ".." and "." Subdirectories nothing will differ from others, except that they are listed at the beginning.

In some cases (for example, in FreeBSD ) this fact is even used to calculate the path to the working directory (which is not stored separately there) through successive transitions through "..".

Going along a path is the same as moving to subdirectories.

It is easy to notice that if we hit a certain directory by a symbolic link, then we will not have information left to return: ".." will indicate to the physical parent. But how then does cd work with logical paths that include symlinks? For this, the shell remembers the way it came to the directory. The logical path built by the shell is accessible through the $ PWD environment variable or via pwd [-L].


Two points require attention, especially in scripts.

PS: There are a lot of symbolic links to directories in sysfs:

 bug @ earth / sys / class / net / lo% ls
 addr_assign_type flags phys_port_id
 address gro_flush_timeout phys_switch_id
 addr_len ifalias power
 ...
 bug @ earth / sys / class / net / lo% ls ..
 lo
 bug @ earth / sys / class / net / lo% pwd -P;  pwd -l
 / sys / devices / virtual / net / lo
 / sys / class / net / lo
 bug @ earth / sys / class / net / lo% cd ..;  ls
 eth0 lo

Source: https://habr.com/ru/post/253049/


All Articles