BASH:替代cd或在他站立的目录中执行脚本

时间:2018-09-16 20:54:20

标签: bash sh cd working-directory

我想要一个类似的命令

    cd ./test/zen/

但是使用其中的文件名

command ./test/zen/rename.sh

进入/ test / zen

有办法吗?我真的不能使用管道,因为我需要像这样的线

find -name "rename.sh" -exec sh {} \;

或者有一种解决方案,可以使用最后一条命令在他站立的目录中而不是在我执行我的最后一条命令的目录中执行脚本...

有什么想法吗? 谢谢^ _ ^

2 个答案:

答案 0 :(得分:0)

您可以为此使用功能:

cdf() {
    cd $(dirname $1)
}

缺少错误控制,参数检查等功能,但是您了解了。

答案 1 :(得分:0)

查找已支持它:

find /var -mount -name "ba*" -execdir pwd \;

从人间找到:

   -execdir command {} +
          Like  -exec, but the specified command is run from the subdirec‐
          tory containing the matched file, which is not normally the  di‐
          rectory in which you started find.  As with -exec, the {} should
          be quoted if find is being invoked from a shell.