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

问题描述 投票:0回答:2

我想要一个命令

    cd ./test/zen/

但是要使用其中的文件名,就像那样

command ./test/zen/rename.sh

并进入/ test / zen

有一种方法?我不能真正使用管道因为我需要它像一条线

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

或者有一个解决方案,用最后一个命令执行一个脚本,在他所站的目录中,而不是在我执行上一个命令的目录中...

有任何想法吗?谢谢^ _ ^

bash sh cd working-directory
2个回答
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.

0
投票

您可以为此目的使用函数:

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

缺少错误控制,参数检查等,但你明白了。

© www.soinside.com 2019 - 2024. All rights reserved.