linux – 如何使用此名称“-2”(以连字符开头)进入cd目录?

我有一个名为的目录:
-2

我想要进入它,但CD抱怨:

bash: cd: -2: invalid option

没有成功,我试过:

cd "-2"
cd '-2'
cd \-2

有解决方案吗

编辑:服务器上没有像mc等文件浏览器.

解决方法

至少有两种方式:

>使用 – 参数.

cd -- -2

这使用GNU工具常用的约定,它不会处理之后出现的任何内容 – 作为命令行选项.

正如commenter所述,该公约也是defined in the POSIX standard

Default Behavior: When this section is listed as “None.”,it means that the implementation need not support any options. Standard utilities that do not accept options,but that do accept operands,shall recognize "--" as a first argument to be discarded.

The requirement for recognizing "--" is because conforming applications need a way to shield their operands from any arbitrary options that the implementation may provide as an extension. For example,if the standard utility foo is listed as taking no options,and the application needed to give it a pathname with a leading hyphen,it could safely do it as:

06001

and avoid any problems with -m used as an extension.

as well as

Guideline 10:
The argument -- should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands,even if they begin with the '-' character. The -- argument should not be used as an option or as an operand.

>明确指定路径:

cd ./-2

这指定了显式命名当前目录(.)作为起点的路径.

cd $(pwd)/-2
cd /absolute/path/to/-2

这些是上面的变化.任何数量的此类变化都是可能的;我将把它作为练习留给读者发现所有这些.

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...