简体   繁体   English

bash: cd: 没有那个文件或目录

[英]bash: cd: No such file or directory

I'm writing a bash function to jump into my last editted folder.我正在编写一个 bash 函数来跳转到我最后编辑的文件夹。 In my example, the last edited folder is titled 'daniel'.在我的示例中,最后编辑的文件夹名为“daniel”。

The bash function looks fine. bash 函数看起来不错。

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

And I can manually cd into the directory.我可以手动 cd 进入目录。

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

But I can't use the bash function to cd into the directory.但是我无法使用 bash 函数 cd 到目录中。

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

Turns out someone added alias ls=ls --color to the bashrc of this server.原来有人在该服务器的 bashrc 中添加了alias ls=ls --color My function works once the alias was removed.一旦别名被删除,我的函数就可以工作。 – Daniel Tan – 丹尼尔·谭

This error is usually thrown when you enter a path that does not exist.当您输入不存在的路径时,通常会抛出此错误。 See -bash: cd: Desktop: No such file or directory .请参阅-bash: cd: Desktop: No such file or directory

But the $(ls -d -1dt -- */ | head -n 1) is not wrong in the output.但是 $(ls -d -1dt -- */ | head -n 1) 在输出中没有错误。 Thus the reason must be the different usage of sh and bash in that moment.因此,原因一定是当时 sh 和 bash 的不同用法。

In my case, I had a docker container with that error when I accessed the folder with bash.就我而言,当我使用 bash 访问文件夹时,我有一个 docker 容器出现该错误。 The container was broken since I had force-closed it after docker-compose up which did not work.容器坏了,因为我在docker-compose up之后强行关闭了它,但它不起作用。 After that, on the existing containers, I could only use sh, not bash.之后,在现有容器上,我只能使用 sh,而不能使用 bash。 I found this because of OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: "bash": executable file not found in $PATH": unknown .我发现这是因为OCI 运行时 exec failed: exec failed: container_linux.go:348: starting container process cause "exec: "bash": executable file not found in $PATH": unknown I guess that bash is loaded later than sh, and that at an early error at the start of the container, only sh gets loaded.我猜 bash 的加载时间晚于 sh,并且在容器开始的早期错误中,只有 sh 被加载。

That would fit since you are in the sh, which can be seen from >>.这很合适,因为您在 sh 中,可以从 >> 中看到。 Using sh, everything will work as expected.使用 sh,一切都会按预期进行。 But the expression gets solved by bash.但是表达式被bash解决了。 Which is probably not loaded for whatever reason.无论出于何种原因,这可能没有加载。


In docker, using docker-compose, I also had a similar error saying sh: 1: cd: can't cd to /root/MYPROJECT .在 docker 中,使用 docker-compose,我也有类似的错误说sh: 1: cd: can't cd to /root/MYPROJECT That could be solved by mounting the needed volumes in the services using这可以通过使用在服务中安装所需的卷来解决

services:
  host:
    volumes:
      - ~/MYPROJECT:/MYPROJECT # ~/path/on/host:/path/on/container

See Mount a volume in docker-compose.请参阅在 docker-compose 中安装卷。 How is it done? 它是如何完成的? and How to mount a host directory with docker-compose?以及如何使用 docker-compose 挂载主机目录? or the official docs .官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM