28

ホーム フォルダーにフォルダー ~/a/b があり、フォルダー b には「symlink」という名前の「..」へのシンボリック リンクが含まれているとします。次に、bash で次のアクションを実行します。

hm@mach:~$ cd a/b/symlink
hm@mach:~/a/b/symlink$ pwd -P
/home/hm/a
hm@mach:~/a/b/symlink$ cd ..
hm@mach:~/a/b$ pwd -P
/home/hm/a/b

pwd -P は、現在の作業ディレクトリを出力し、すべてのシンボリック リンクを逆参照します。/home/hm ではなく、作業ディレクトリ /home/hm/a/b が最後にあるのはなぜですか?

4

2 に答える 2

30

によるとhelp cd

  Options:
      -L        force symbolic links to be followed: resolve symbolic
                links in DIR after processing instances of `..'
      -P        use the physical directory structure without following
                symbolic links: resolve symbolic links in DIR before
                processing instances of `..'

つまり、論理構造-Lを使用することを意味しますが、実際には物理的なディレクトリ構造を使用します。-P

論理構造はこんな感じです。

$ tree a
a
└── b
    └── symlink -> ..

実際に行った時の物理的な構造a/b/symlinkは、

a

real を使用する場合は..、次も使用する必要がありますcd -P

          The -P option says to use the physical directory
          structure instead of following symbolic links (see
          also the -P option to the set builtin command);
          the -L option forces symbolic links to be followed.

例、

$ cd
$ cd a/b/symlink   # physical location is at a/
$ cd ..            # now is at a/b
$ cd symlink       # goes back to a/b/symlink
$ cd -P ..         # follow physical path (resolve all symlinks)
$ pwd -P           # -P is optional here to show effect of cd ..
/home/sarnold
$ 
于 2012-05-04T22:22:15.930 に答える
5

bashプロンプトに示されているように、論理的な現在のディレクトリパスを追跡し、cd ..それに応じて解釈します。cdこれにより、 (または) でのみそのようなパスを使用する場合、物事の一貫性が少し高まりpushdますが、コマンド引数 (またはコマンド内) のパスで同じことが起こると予想され、独自の構成可能なルールがある場合、予期しないことが起こるという犠牲を払いemacsますvim。シンボリックリンクの処理用ですが、ほとんどのコマンドはそれを処理するためにカーネルに依存しています)。

于 2012-05-04T22:20:50.320 に答える