Search code examples
bashshcdworking-directory

BASH: alternative for cd or execute a script in the directory he stand


I would like a command like

    cd ./test/zen/

but working with the name of a file in it, like that

command ./test/zen/rename.sh

and go in /test/zen

There is a way? I can't really use pipe cause I need it for a line like

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

Or there is a solution to execute with this last command a script in the directory where he stand and not in directory where I execute my last command...

Any ideas? Thanks ^_^


Solution

  • Find already supports it:

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

    from man find:

       -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.