如何在 linux/unix 中找到 -exec cd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6336481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find -exec cd in linux / unix
提问by cwd
I'm searching for a config folder, and trying to change to that directory:
我正在搜索一个 config 文件夹,并尝试更改到该目录:
find . -name "config" -exec cd {} \;
There is one match, ./my-applications/config
, but after I try this it says:
有一场比赛,./my-applications/config
但在我尝试之后它说:
find: `cd': No such file or directory
What am I doing wrong?
我究竟做错了什么?
采纳答案by Jonathan Leffler
The command cd
is a shell built-in, not found in /bin
or /usr/bin
.
该命令cd
是内置的 shell,在/bin
或 中找不到/usr/bin
。
Of course, you can't change directory to a file and your search doesn't limit itself to directories. And the cd
command would only affect the executed command, not the parent shell that executes the find
command.
当然,您不能将目录更改为文件,并且您的搜索不限于目录。并且该cd
命令只会影响已执行的命令,而不会影响执行该find
命令的父 shell 。
Use:
用:
cd $(find . -name config -type d | sed 1q)
Note that if your directory is not found, you'll be back in your home directory when the command completes. (The sed 1q
ensures you only pass one directory name to cd
; the Korn shell cd
takes two values on the command and does something fairly sensible, but Bash ignores the extras.)
请注意,如果未找到您的目录,则命令完成后您将返回主目录。(这sed 1q
确保您只将一个目录名称传递给cd
;Korn shellcd
在命令上采用两个值并做一些相当明智的事情,但 Bash 忽略了额外的东西。)
回答by lhf
find
runs -exec
programs as subprocesses and subprocesses cannot affect their parent process. So, it cannot be done. You may want to try
find
-exec
作为子进程运行程序,子进程不能影响它们的父进程。所以,这是做不到的。你可能想尝试
cd `find . -name "config"`
回答by jlliagre
In case you have more than one config directory:
如果您有多个配置目录:
select config in $(find . -name config -type d)
do
cd $config
break
done