//foo 是个什么目录?

发布于 2023-03-16 21:13:16 字数 1619 浏览 109 评论 0

在 bash 下执行下面命令看看结果是什么

cd /tmp
pwd
cd //tmp
pwd
cd ///tmp
pwd
cd ////tmp
pwd
/tmp
//tmp
/tmp
/tmp

貌似中间混入了一个奇怪的东西 //tmp,这是什么玩意?? 我们来看看它的 inode

stat //tmp
  文件://tmp
  大小:740        块:0          IO 块:4096   目录
设备:2dh/45d      Inode:12972       硬链接:17
权限:(1777/drwxrwxrwt)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-09-16 21:52:50.368212128 +0800
最近更改:2019-09-16 22:29:22.768083948 +0800
最近改动:2019-09-16 22:29:22.768083948 +0800
创建时间:-

可以看到 inode 为 12972,那么这个 inode 其实应该属于哪个文件呢?

find / -inum 12972;:
/tmp

你会发现 //tmp 其实就是 /tmp,怎么会这样呢?根据 cd规范

An implementation may further simplify curpath by removing any trailing <slash> characters that are not also leading <slash> characters, replacing multiple non-leading consecutive <slash> characters with a single <slash>, and replacing three or more leading <slash> characters with a single <slash>. If, as a result of this canonicalization, the curpath variable is null, no further steps shall be taken.

总结起来就是几点:

  • 路径最后的 / 可以被省略,即 /foo// 可以简化成 /foo
  • 非首部的多个连续 / 可以被简化成一个 / ,即 /foo//bar 可以简化成 /foo/bar
  • 首部的三个或以上连续的 / 可以被简化成一个 / ,即 ///foo 可以简化成 /foo
  • 首部是两个连续的 / 的情况没有定义,也就是可以有不同的实现方式

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

假装爱人

暂无简介

文章
评论
27 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文