我面临着一种非常令人困惑的行为,ls
我无法搜索。它显示目录中有内容,但仅当我位于创建这些内容的目录中时。
我来给你展示:
\nciprian Documents $ pwd\n/Users/ciprian/Documents\n\nciprian Documents $ ls ../Downloads/rss22/\n22rss-USB/\n\nciprian Documents $ ls ../Downloads/rss22/22rss-USB/\nHTML/\n\nciprian Documents $ cd ../Downloads/rss22\n\nciprian rss22 $ ls\n\nciprian rss22 $ ls 22rss-USB/\ngls: cannot access \'22rss-USB/\': No such file or directory\n
Run Code Online (Sandbox Code Playgroud)\n我cd
ed to后../Downloads/rss22
,其内容显示为空。cd ~/Desktop
如果 I然后 I ,它也会显示为空ls ../Downloads/rss22/
,就像这里的第一种情况一样。
对我来说,这表明可能有一个名为../Downloads/rss22
inside Documents
的文件夹。但我不知道如何显示它。ls -a ~/Documents
不显示与这些文件夹相关的任何内容。
到底是怎么回事?
\n这些文件是通过尝试从存档中部分提取来创建的:unzip 22rss-USB.zip "22rss-USB/HTML/**/*" -d ../Downloads/rss22/
作为参考,我使用的是macOS,尽管我认为这无关紧要(它是 Unix,对吧?)。我正在使用 Bash 5.1.16(从默认的 zsh 更改)。
\n: pwd的输出type pwd
是 shell 内置命令。
事实证明,如果我cd -P ../Downloads/igarss22/
这样做,它就会显示我期望的内容。我在哪里可以看到更多相关信息?man cd
没有显示任何关于 的信息-P
。
现在,之后cd -P ../Downloads/igarss22/
ciprian Documents $ cd -P ../Downloads/igarss22\n\nciprian igarss22 $ pwd\n/Users/ciprian/Library/CloudStorage/OneDrive/Downloads/igarss22\n
Run Code Online (Sandbox Code Playgroud)\n正确的。所以我忘记了这一点;my~/Documents
是指向我的 OneDrive 下的文件夹的符号链接:
$ ll ~/ | grep Doc\nlrwxr-xr-x 1 ciprian 38 May 19 2022 Documents -> /Users/ciprian/OneDrive/Documents\n
Run Code Online (Sandbox Code Playgroud)\n由于macOS\xc2\xa0v12 (Monterey) 中的一些魔法和变化,它实际上位于/Users/ciprian/Library/CloudStorage/OneDrive
.
我仍然不确定到底发生了什么
\ntel*_*coM 29
区别在于物理处理与逻辑处理..
:
您显然有两个单独的Downloads
目录:
/Users/ciprian/Downloads
/Users/ciprian/Library/CloudStorage/OneDrive/Downloads
某些 shell(包括bash
所有 POSIX shell)可以选择..
在cd
命令中将其视为“完全按照用户表达的方式获取当前路径,切断最右侧的元素并更改为结果路径”。通过符号链接,这将作为逻辑“返回到我们来自的地方”,从/Users/ciprian/Library/CloudStorage/OneDrive/Documents
(符号链接到,并由用户称为~/Documents
)返回到/Users/ciprian
(因此此时的新路径只是~
),然后从那里返回到/Users/ciprian/Downloads
。如果您使用cd -L
,这就是您将得到的明确行为。
其他程序通常不会这样做:它们将遵循物理路径,因此对于它们来说,将目录更改为../Downloads
从开始/Users/ciprian/Library/CloudStorage/OneDrive/Documents
将始终意味着更改为/Users/ciprian/Library/CloudStorage/OneDrive/Downloads
. 这也是您将得到的cd -P
。
如果您不喜欢 shell 的这个(错误)功能,可能有一种特定于 shell 的方法可以禁用它。对于bash
shell,将set -P
或添加set -o physical
到您的~/.bashrc
或类似的 shell 启动脚本将使所有命令的bash
行为都具有该选项,除非显式使用该选项。cd
-P
-L
笔记。的文档-P
位于man bash
(SHELL BUILTIN COMMANDS 部分),因为它cd
是 Bash 的内部命令。