请教一个关于find命令的问题

只要开始永远不晚 2014-08-19 10:19:22
问题描述,我想使用find 命令找到shell所在的目录并切换到此目录(shell)下
我想使用find -exec这个办法
我的shell目录在/home/test/shell

以下是我遇到的问题:

现象1:这样做不可以

find /home -name 'shell' -ok cd {} \;
< cd ... /home/test/shell > ? Y
find: `cd': No such file or directory

现象2:这样可以

find /home -name 'shell' -ok ls {} \;
< ls ... /home/test/shell > ? y
这样可以显示shell目录下的文件

疑惑为什么find的结果不能cd
...全文
98 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
thanks cd `find / -name 'shell'`可解决我的想法。 cd不能被find的exec开启的子脚本识别
  • 打赏
  • 举报
回复
The current directory is always context sensitive. When u completed the execution of a shell program such as find command, the current directory reverts to whatever was in effect when you started the program. So, in your case, the effects of 'cd' will not be seen once that -exec (which starts a shell) is complete.
  • 打赏
  • 举报
回复
cd "$(dirname "$(find . -name 'shell' 2> /dev/null)")"

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧