44

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?

3 Answers 3

94

Can you try using this simple loop which loops in all sub-directories at one level deep and execute commands on it,

for d in ./*/ ; do (cd "$d" && ls -al); done

(cmd1 && cmd2) opens a sub-shell to run the commands. Since it is a child shell, the parent shell (the shell from which you're running this command) retains its current folder and other environment variables.

Wrap it around in a function in a proper zsh script as

#!/bin/zsh

function runCommand() {
    for d in ./*/ ; do /bin/zsh -c "(cd "$d" && "$@")"; done
}

runCommand "ls -al"

should work just fine for you.

5
  • 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.
    – Zahymaka
    Jan 9, 2017 at 23:31
  • 1
    @Zahymaka: I have added a more generic way of doing this, by adding a function. Don't forget to accept/upvote it once you find it solving your problem.
    – Inian
    Jan 10, 2017 at 18:29
  • 2
    This function doesn't work if there's a quote in the directory name (zsh:1: unmatched ')
    – wilsotc
    Jan 10, 2017 at 20:17
  • Nice. Seeing your quotes prompted me to try running my original mexec with quotes. What do you know, it worked!
    – Zahymaka
    Jan 10, 2017 at 21:22
  • Good. If anyone needs to execute commands in all sub-directories at two levels deep, you can simply use ./*/*/
    – Henry S.
    Jul 10, 2022 at 3:21
5
#!/bin/zsh
# A simple script with a function...

mexec()
{ 
  export THE_COMMAND=$@
  find . -type d -maxdepth 1 -mindepth 1 -print0 | xargs -0 -I{} zsh -c 'cd "{}" && echo "{}" && echo "$('$THE_COMMAND')" && echo -e'
}

mexec ls -al
5
  • This one should work even with directories with quotes and other strange characters
    – wilsotc
    Jan 10, 2017 at 19:52
  • The two echos are a hack. Not sure why -e wouldn't work on the first one.
    – wilsotc
    Jan 10, 2017 at 20:13
  • 1
    I just realized my original works if I call mexec 'ls -al' with the command in quotes. So does this. Thanks!
    – Zahymaka
    Jan 10, 2017 at 21:21
  • 1
    Just remember your method still won't work with directories containing many directory legal characters. Your script will give an error "xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option" or similar depending on the characters used.
    – wilsotc
    Jan 10, 2017 at 23:10
  • 1
    Got it. Very unlikely I'll have directories with quotes in them.
    – Zahymaka
    Jan 11, 2017 at 20:02
2

using https://github.com/sharkdp/fd but you could as well use plain old find instead of fdfind

function inDirs() { fdfind --type d --max-depth 1 --exec bash -c "x={} && echo && echo \$x && echo \${x//?/=} && cd {} && echo '-> '$* && $*" ; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.