bash:cd:没有这样的文件或目录

时间:2017-04-10 03:51:06

标签: linux bash function alias ls

我正在编写一个bash函数来跳转到我上一个编辑过的文件夹。 在我的示例中,最后编辑的文件夹标题为“daniel'。

bash功能看起来很好。

>>:~$ echo $(ls -d -1dt -- */ | head -n 1)
daniel/

我可以手动进入目录。

>>:~$ cd daniel
>>:~/daniel$ 

但我无法使用bash功能进入目录。

>>:~$ cd $(ls -d -1dt -- */ | head -n 1)
bash: cd: daniel/: No such file or directory

2 个答案:

答案 0 :(得分:0)

原来有人将alias ls=ls --color添加到此服务器的bashrc中。删除别名后,我的功能才起作用。 - Daniel Tan

答案 1 :(得分:0)

当您输入不存在的路径时,通常会抛出此错误。见-bash: cd: Desktop: No such file or directory

但是 $(ls -d -1dt -- */ | head -n 1) 在输出中没有错误。所以一定是那个时候sh和bash的用法不同。

就我而言,当我使用 bash 访问文件夹时,我的 docker 容器出现了该错误。容器坏了,因为我在 cols.extend(["extend"]) 之后强行关闭了它,这不起作用。之后,在现有容器上,我只能使用 sh,而不能使用 bash。我发现这个是因为OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: "bash": executable file not found in $PATH": unknown。我猜 bash 的加载时间晚于 sh,并且在容器启动的早期错误中,只有 sh 被加载。

这很合适,因为您在 sh 中,这可以从 >> 中看到。使用 sh,一切都会按预期进行。但是表达式被bash解决了。可能由于某种原因没有加载。


在 docker 中,使用 docker-compose,我也有类似的错误,说 docker-compose up。这可以通过使用

在服务中安装所需的卷来解决
sh: 1: cd: can't cd to /root/MYPROJECT

参见 Mount a volume in docker-compose. How is it done?How to mount a host directory with docker-compose?official docs