I'm trying to add a shell function (zsh) mexec
to execute the same command in all immediate subdirectories e.g. with the following structure
~
-- folder1
-- folder2
mexec pwd
would show for example
/home/me/folder1
/home/me/folder2
I'm using find
to pull the immediate subdirectories. The problem is getting the passed in command to execute. Here's my first function defintion:
mexec() {
find . -mindepth 1 -maxdepth 1 -type d | xargs -I'{}' \
/bin/zsh -c "cd {} && $@;";
}
only executes the command itself but doesn't pass in the arguments i.e. mexec ls -al
behaves exactly like ls
Changing the second line to /bin/zsh -c "(cd {} && $@);"
, mexec
works for just mexec ls
but shows this error for mexec ls -al
:
zsh:1: parse error near `ls'
Going the exec route with find
find . -mindepth 1 -maxdepth 1 -type d -exec /bin/zsh -c "(cd {} && $@)" \;
Gives me the same thing which leads me to believe there's a problem with how I'm passing the arguments to zsh. This also seems to be a problem if I use bash: the error shown is:
-a);: -c: line 1: syntax error: unexpected end of file
What would be a good way to achieve this?
ls -al
in this case is just an example command I'm running in every sub-directory. I'd want it to be free-form, regardless of what command I use. I'll try the for loop. – Pone